我有兩個列表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