[英]How can I compare two python lists by index return a Boolean if the index in each string matches?
[英]How do i compare 2 string Lists and return boolean index
如何比較字符串列表並創建要索引的布爾值列表
我認為它可能有一個循環,但我尋找一個簡單的函數
l1 =["a","b", "c"]
l2 =["b", "a"]
index = []
for i in l1:
index.append(i=l2)
預期輸出:[真、真、假]
你可以做這樣的事情。
l1 =["a","b", "c"]
l2 =["b", "a"]
index = []
for i in l1:
index.append(i in l2)
循環針對 l1 的每個元素,if 檢查 l1 的相同元素是否存在於 l2 中。 如果它存在於 l2 中,則附加True
,否則附加False
。
只是一個正常的列表理解會起作用
>>> [e in l2 for e in l1]
[True, True, False]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.