我有两个列表list1
和list2
,我想要得到的的list1
元素的各项指标,同时也是在第二届一个填充列表与其他列表的指标
for i in list1:
print(i) ## this works fine
Test_features_index.append(list1.index(i for i in list2))# here not that well
运行此doens't在这里工作是我得到的:
<ipython-input-35-8d7ff70a8be0> in <module>()
----> 1 Test_features_index.append(list1.index(i for i in list2))
ValueError: <generator object <genexpr> at 0x0000021710BBA7D8> is not in list
任何想法如何做到这一点?我想避免for循环,但不知道是否有可能
你可以通过列表理解来做到这一点。 'Test_features_index.append([list1.index(i)for i in list2 if I in list1])(假设你真的想把结果追加到另一个名为Test_features_index的列表中)。请注意,如果某个项目出现多次,索引只会返回第一个索引。如果无法找到该项目,'index'将抛出异常,要求'如果我在list1'中检查。 – roganjosh