在javascript中,如何区分没有arg传递和未定义的arg传递(in javascript, how to distinguish between no arg passed and undefined arg passed)
在一个函数中,如何识别非arg和未定义的arg?
myFunc( 'first' ); var obj = { a: 123 }; myFunc( 'first', obj.b ); _or_ myFunc( 'first', undefined )
可以用arguments.length
指的是通过命名参数的参数,所以它没有帮助arguments.length
轻松解决 - 抱歉大脑放屁!function myFunc( a, b ) { // Case A: if no second arg, provide one // should be: if( arguments.length < 2 ) ... if( b === undefined ) b = anotherFunc; // Case B: if b is not resolved - passed but undefined, throw else if( b === undefined ) throw( 'INTERNAL ERROR: undefined passed' ); // Case C: if b not a function, resolve by name else if( typeof b != 'function' ) { ... } ... }
在
myFunc
捕获案例A和案例B的正确方法是什么?within a function, how to discern between a non-arg and an undefined arg?
myFunc( 'first' ); var obj = { a: 123 }; myFunc( 'first', obj.b ); _or_ myFunc( 'first', undefined )
Can be solved easily witharguments.length
refers to arguments past the named arguments, so it's no helparguments.length
- sorry about the brain fart!function myFunc( a, b ) { // Case A: if no second arg, provide one // should be: if( arguments.length < 2 ) ... if( b === undefined ) b = anotherFunc; // Case B: if b is not resolved - passed but undefined, throw else if( b === undefined ) throw( 'INTERNAL ERROR: undefined passed' ); // Case C: if b not a function, resolve by name else if( typeof b != 'function' ) { ... } ... }
What is the correct way to capture Case A and Case B in
myFunc
?
原文:https://stackoverflow.com/questions/7750577
最满意答案
虽然你可以使用
In [85]: pd.DataFrame({'test':test.tolist(), 'test2':test2.tolist()}) Out[85]: test test2 0 [1, 2] [2, 4] 1 [2, 3] [2, 5]
NumPy数组上的计算可能比在包含Python列表的Pandas DataFrame上进行的等效计算快得多。
Although you could use
In [85]: pd.DataFrame({'test':test.tolist(), 'test2':test2.tolist()}) Out[85]: test test2 0 [1, 2] [2, 4] 1 [2, 3] [2, 5]
computation on the NumPy arrays would probably be much faster than an equivalent computation done on a Pandas DataFrame whose columns contain Python lists.
相关问答
更多-
你可以简单地索引你想要的系列。 示例 - tdf['s1'] 演示 - In [24]: tdf = pd.DataFrame({'s1' : [0,1,23.4,10,23]}) In [25]: tdf['s1'] Out[25]: 0 0.0 1 1.0 2 23.4 3 10.0 4 23.0 Name: s1, dtype: float64 In [26]: tdf['s1'].shape Out[26]: (5,) 如果你想将系列中的值作为numpy数 ...
-
虽然你可以使用 In [85]: pd.DataFrame({'test':test.tolist(), 'test2':test2.tolist()}) Out[85]: test test2 0 [1, 2] [2, 4] 1 [2, 3] [2, 5] NumPy数组上的计算可能比在包含Python列表的Pandas DataFrame上进行的等效计算快得多。 Although you could use In [85]: pd.DataFrame({'test':test. ...
-
.loc同时接受行和列选择器(与.ix/.iloc FYI一样)这也是在单次传递中完成的。 In [1]: df = DataFrame(np.random.rand(4,5), columns = list('abcde')) In [2]: df Out[2]: a b c d e 0 0.669701 0.780497 0.955690 0.451573 0.232194 1 0.952762 0.5 ...
-
使用标头和dtypes将pandas数据帧转换为numpy数组(Converting pandas dataframe to numpy array with headers and dtypes)[2022-09-07]
使用df.to_records()将数据帧转换为结构化数组。 您可以传递index=False以从结果中删除索引。 import numpy as np df = pd.DataFrame(np.random.rand(10,3),columns=['Acol','Ccol','Bcol']) res = df.to_records(index=False) # rec.array([(0.12448699852020828, 0.7621451848466592, 0.095852994383143 ... -
您可以使用apply ,但这不是矢量操作 In [277]: df.val.fillna('').apply( lambda x: np.array(x.split(','), dtype=int).reshape(-1, 2) if x else []) Out[277]: 0 [] 1 [] 2 [] 3 [[71, 299], [77, 12 ...
-
将object类型的pandas数据框列转换为numpy数组(Convert a pandas dataframe column of type object to a numpy array)[2022-05-07]
我已经找到了这个问题的答案。 这非常直截了当,我只是没有看到它开始。 这是我如何获得对象数据以及numpy数组(.values :)) train_img_array = np.array([]) train_id_array = np.array([]) train_lab_array = np.array([]) train_id_array = img_train['ID'].values train_lab_array = img_train['class'].values train_img_ar ... -
我应该使用什么索引将numpy数组转换为熊猫数据框?(What index should I use to convert a numpy array into a pandas dataframe?)[2023-07-07]
你应该重塑你的输入数组: In [6]: pd.DataFrame(x.reshape(1,4), columns=nam) Out[6]: col1 col2 col3 col4 0 2 3 1 0 或者更灵活一些: In [11]: pd.DataFrame(x.reshape(len(x) // len(nam), len(nam)), columns=nam) Out[11]: col1 col2 col3 col4 0 2 ... -
这里有一种方法可以将每一行作为一个列表来为我们提供一1D列表 - In [231]: df Out[231]: 0 1 2 3 4 5 0 1622 95 1717 85.278544 1138.964373 1053.685830 1 62 328 390 75.613900 722.588235 646.974336 2 102 708 810 75.6139 ...
-
IIUC你可以使用DataFrame构造函数并apply join : import pandas as pd import numpy as np arr = np.array([[64, 22,], [58, 64], [42, 31]]) print arr [[64 22] [58 64] [42 31]] li = ['one','two','three'] df = pd.DataFrame(arr, dtype='str', index=li) print df ...
-
您可以将numpy数组直接传递给DataFrame构造函数: In [11]: a = np.random.rand(3, 5) In [12]: a Out[12]: array([[ 0.46154984, 0.08813473, 0.57746049, 0.42924157, 0.34689139], [ 0.29731858, 0.83300176, 0.15884604, 0.44753895, 0.56840054], [ 0.02479636, ...