How to Compare List of Strings and create a List of booleans to index over
i think its possible with a loop, but i search for a simple function
l1 =["a","b", "c"]
l2 =["b", "a"]
index = []
for i in l1:
index.append(i=l2)
expected output: [True, True, False]
You can do something like this.
l1 =["a","b", "c"]
l2 =["b", "a"]
index = []
for i in l1:
index.append(i in l2)
The loop is for every element of l1 and the if checks if the same element of l1 present in l2 or not. If it exists in l2 then True
is appended else False
is appended.
Just a normal list comprehension would work
>>> [e in l2 for e in l1]
[True, True, False]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.