我有:
return (
<TouchableOpacity
activeOpacity={1}
onPress={() => { alert(`You've clicked`); }}
>
<View>
{Title}
<Text
>
{available}
</Text>
</View>
</TouchableOpacity>
);
我有一个列表列表和单个整数,如下所示:
hi
0 1
1 2
2 4
3 8
4 3
5 3
6 2
7 8
8 3
9 5
10 4
对于主列表中的每个项目,我想找出其首次出现在列中的索引。
所以对于单个整数(即2),我想知道它是第一次出现在hi列(索引1,但我对再次出现即索引6不感兴趣)
对于列表中的列表,我想知道列表在该列中按顺序出现的最后索引。
因此对于[2,8,3]依次出现在索引6、7和8上,所以我希望返回8。请注意,它也出现在此之前,但被4插入,所以我对此不感兴趣。
到目前为止,我已经习惯了:
[[2,8,3], 2, [2,8]]
答案 0 :(得分:1)
您可以使用np.logical_and.reduce
+ shift
进行操作。但是有很多边缘情况需要处理:
import numpy as np
def find_idx(seq, df, col):
if type(seq) != list: # if not list
s = df[col].eq(seq)
if s.sum() >= 1: # if something matched
idx = s.idxmax().item()
else:
idx = np.NaN
elif seq: # if a list that isn't empty
seq = seq[::-1] # to get last index
m = np.logical_and.reduce([df[col].shift(i).eq(seq[i]) for i in range(len(seq))])
s = df.loc[m]
if not s.empty: # if something matched
idx = s.index[0]
else:
idx = np.NaN
else: # empty list
idx = np.NaN
return idx
l = [[2,8,3], 2, [2,8]]
[find_idx(seq, df, col='hi') for seq in l]
#[8, 1, 7]
l = [[2,8,3], 2, [2,8], [], ['foo'], 'foo', [1,2,4,8,3,3]]
[find_idx(seq, df, col='hi') for seq in l]
#[8, 1, 7, nan, nan, nan, 5]