Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

python - How to scan a big list to find if all elements match the ones of a second, smaller, list?

I'm hvaing a hard time with a problem which I thought was pretty simple. I have two lists:

  1. A small one, that looks like this:
list1 = ['A', 'B', 'C', 'D','E']
  1. The second list is much bigger, going on for about 800 elements. It looks like this:
list2 = ['E', 'B', 'F', 'A', 'C', 'N'...]

I want to scan list2 and see if all of its elements are the same as the ones in list1. If they are different, I want to see which are the elements that differ and cancel them from list2. In this example, I want to print "F" and "N" from list2 and cancel them.

I tried:

found = False
lenght2 = len(list2)
i = 0
for j in list1:
  for i in range(0, lenght2):
     if i != j:
        found = True
        #I don't know how to cancel i
        print(i)
        i = i + 1
     break

However, the whole thing does not work. Is there anyone who could help me?

question from:https://stackoverflow.com/questions/65846239/how-to-scan-a-big-list-to-find-if-all-elements-match-the-ones-of-a-second-small

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could go through all of list2 then check if it is in list one, like this:

for i in range(len(list2)):
  if list2[i] in list1:
    pass
  else:
    #Cancel list2[i] ? Or whatever.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...