简体   繁体   中英

Python List — checking if item in list exists in list

have a question.

list1 = ['the quick brown fox', 'brown fox', 'fox']
list2 = ['the', 'james went to the library', 'james']

For list1, would like to remove 'brown fox' and 'fox' since it exists in list1[0]. For list2, would like to remove 'james' and 'the' from the list since it exists in list2[1]

How do I do a check for this? The order for the terms in the list may be given in any order. Have a large number of lists as such so would appreciate the answers.

Have tried to do a for loop, going through the list but got stuck on the logic part.

for x in range(len(list1)):
    if list1[x] in list1: ## got stuck here on the logic.

If I understood your question properly, you can sort the list according to the length of the strings in it ascending to have the shortest string at the beginning and check if it's exist in the longer strings after it. If so, append it into a list to take the difference between it and the original list.

You can do something like this:

list1 = ['the quick brown fox', 'brown fox', 'fox']
list2 = ['the', 'james went to the library', 'james']
lists = [list1, list2]
sol = []
for lst in lists:
  diff = []
  lst = sorted(lst, key=len)
  for i in range(len(lst)):
    for j in range(i, len(lst)):
      if i != j and lst[i] in lst[j]:
        diff.append(lst[i])
  sol.append(list(set(tuple(lst)) - set(tuple(diff))))

for lst in sol:
  print(lst)

Note: I put the lists in one list to make the solution more general.

It's not the best solution, but I think it's easy to understand.

Hope this will help.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM