Append index of items in one list to another in Python 2.7 -
master =[23,5, 6, 34,11] list_check = [23, 6, 11]
ideal output be:
location = [0, 2, 4 ]
values in list_check in master. neglect duplication.
getting desired output particular lists have given easy list comprehension, can see other answers here, but, said wanted neglect duplication. know of no way list comprehension. example, using 1 of list comprehensions suggested here duplicate element in master
>>> master =[23,5, 6, 34, 11, 23] >>> list_check = [23, 6, 11] >>> [master.index(item) item in master if item in list_check] [0, 2, 4, 0]
using suggestion:
>>> location = [x x, y in enumerate(master) if y in list_check] [0, 2, 4, 5]
if understand question correctly, want result same no matter how many times element appears in master
. example,
master =[23,5, 6, 34, 11, 23, 23, 11, 6] list_check = [23, 6, 11]
output
location = [0, 2, 4]
i way:
master =[23, 5, 6, 34, 11, 23, 23, 11, 6] list_check = [23, 6, 11] location = [] element in list_check: if element in master: k in range(len(master)): if master[k] == element: location.append(k) break
with master
, list_check
used above, output indeed
location = [0, 2, 4]
Comments
Post a Comment