2021年4月29日星期四

How can I compare 2 list and remove the a string from 1 list that contain a substring from other list? Python

is there anyway I can remove a specific string if other list contain a substring of it? For example: list x contain the substring, since y[0] have 'hi'(which contain in list x) inside the word, I would like to remove it.

x=["yo",'sup','hi']    y= ['hi-test','test2','test3']  

This is what I have try, but I think my concept are wrong.

x=["yo",'sup','hi']  y= ['hi-test','test2','test3']  list1=[]  for namex in x:      for namey in y:          if namex in namey:              break          else:              list1.append(namey)    print(list1)  

I would like to get a list with 'test2' and 'test3'.

https://stackoverflow.com/questions/67328004/how-can-i-compare-2-list-and-remove-the-a-string-from-1-list-that-contain-a-subs April 30, 2021 at 12:00PM

没有评论:

发表评论