我看了文档,但仍然不太理解doc的区别和用例item。
但是最近我发现只有这种方法item可行:
a = np.array(100) # a has shape ()!
a.item() # or a.item(0)
Run Code Online (Sandbox Code Playgroud)
这是从中获取价值的唯一方法a,a[0]不起作用。
我想得到一个价值a.如果在那里找不到,那么试着从中获取它b.如果在b两者中都找不到,则抛出异常.
a.get(key, b[key])不起作用,因为它不会懒惰评价b[key].
什么是正确的方法?
以下工作,但似乎有点冗长.
value = a.get(key, b.get(key, 'not found'))
if value == 'not found':
raise Exception()
Run Code Online (Sandbox Code Playgroud) type(np.clip(pd.Series(range(10)), 2, 3))是一个pd.Series.
type(np.clip(range(10), 2, 3)) 是一个 np.array
为什么pd.Series在numpy应用函数后可以保留其类型?
一般
julia> Base.show(io::IO, a::Int) = print(io, "xx")
show (generic function with 98 methods)
julia> show(2)
xx
Run Code Online (Sandbox Code Playgroud)
它被正确重载。
但是,如果我show在重载之前调用,它不会重载,也不会发出任何警告。
julia> show(1)
1
julia> Base.show(io::IO, a::Int) = print(io, "xx")
show (generic function with 98 methods)
julia> show(2)
2
julia> Base.show(2)
2
Run Code Online (Sandbox Code Playgroud)
这是为什么?
我有一个数据框
x c
0 0 1
1 3 2
2 1 1
3 2 1
4 3 1
5 4 1
6 1 0
7 3 1
8 2 1
9 1 2
Run Code Online (Sandbox Code Playgroud)
我想生产
c x duplicated
0 1 0 False
1 2 3 False
2 1 1 False
3 1 2 True
4 1 3 True
5 1 4 False
6 0 1 False
7 1 3 True
8 1 2 True
9 2 1 False
Run Code Online (Sandbox Code Playgroud)
即先分组c,并标记组中所有重复的行。 …
例如
foreach(var toCheck in query)
{
foreach (var againstItem in totalWeight)
{
count1++
if (getIEnumerable(toCheck,againstItem).Any(
x => { count2++; return (x == againstItem.Key); }))
{
blacklist.Add(toCheck);
break;
}
count3++;
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
count1= 166184
count2= 30620
count3= 165848
我期待count2与count1和相比会更大count3
例如:
void FilterA(Hashset<T> blackList, List<T> myList)
{
blackList.UnionWith(myList);
}
Run Code Online (Sandbox Code Playgroud)
我不知道是否会有一些好的编码风格分开blackList和myList分开.还是有必要的?
这很奇怪,我以为我们应该总是把高机会条款放到嵌套if-elses的前面,直到今天.
简要设置:
一个数组Zoo[]包含10,000个5类的对象,基于权重,例如4,3,2,1,0(意思是4000只猫,3000只狗,2000只鸡,1000只兔子,0只猫头鹰)它可以被洗牌或不洗牌(完全按顺序).
然后使用if-else检查每个阵列成员.
结果:时间(毫秒)
Weights 43210 01234 22222 43210 01234 22222
Shuffle Yes Yes Yes No No No
Polymorphism 101 100 107 26 26 27
If Else 77 28 59 17 16 17
If Else Reverse 28 77 59 16 17 16
Switch 21 21 21 18 19 18
Run Code Online (Sandbox Code Playgroud)
当我看到If-Else相反的情况好得多时,它引起了我的注意if-else.这里if-else考试Cat-> Dog-> Chicken-> Rabbit-> Owl,反向版本以相反的顺序检查它们.
还有,有人可以在非洗牌版本中解释每种方法都有很大改进吗?(我会假设由于内存中的缓存或更高的命中率?)
Weights 27 9 3 1 0 0 1 3 9 27 27 9 …Run Code Online (Sandbox Code Playgroud) if reelID = reelWeights.Count - 1
then Array.fold calc1 (0L,0) reelWeights.[reelID]
else Array.fold calc2 (0L,0) reelWeights.[reelID]
Run Code Online (Sandbox Code Playgroud)
我尝试使用管道,似乎放慢了一点(不知道为什么):
reelWeights.[reelID]
|> (if reelID = reelWeights.Count - 1 then Array.fold calc1 else Array.fold calc2) (0L,0)
Run Code Online (Sandbox Code Playgroud)
如果我做
let calc x = if x then calc1 else calc2
Array.fold (calc reelID = reelWeights.Count - 1) (0L,0) reelWeights.[reelID]
Run Code Online (Sandbox Code Playgroud)
那么在循环中冗余检查条件的成本看起来很不错.
我在F#中创建了一个函数
let tryParseArray tryParse (separator:char) (line: string) =
// inside the function I use the tuple form of tryParse
Run Code Online (Sandbox Code Playgroud)
如果我以这种方式调用它,它工作正常: tryParseArray Int32.TryParse ',' "2,3,2,3,2"
现在我希望这个函数也可以在C#中使用,所以我这样做了:
static member TryParseArray (line, tryParse, separator) =
line |> tryParseArray tryParse separator
Run Code Online (Sandbox Code Playgroud)
然后我意识到TryParseArray实际上把tryParse参数作为FSharpFunc,对C#一点都不友好,所以我尝试了这个:
static member TryParseArray (line, [<Out>] tryParse: (string * byref<'a> -> bool), separator) =
line |> tryParseArray tryParse separator
Run Code Online (Sandbox Code Playgroud)
但现在tryParseArray不接受tryParse作为有效参数(类型错误)
我该怎么办?
我想在C#中我可以调用TryParseArray("2,3,2,3,2", Int32.TryParse, ',')以及
python ×4
c# ×3
f# ×2
numpy ×2
pandas ×2
dataframe ×1
dictionary ×1
if-statement ×1
julia ×1
linq ×1
performance ×1