Possible duplicate:Python: how to find the intersection of a list?
I have two data lists in .txt
data1 = "name1", "name2", "name3", "name4" etc. data2 = "name3", "name6", "name10" etc.
I want to know which names appear on both lists. How do I do this?
Use sets :
set(data1) & set(data2)
The & operator means "give me the intersection of these two sets"; alternatively you can use the .intersection method:
&
.intersection
set(data1).intersection(data2)
nf = [x for x in data1 if x in data2] nf
will return a common item in both lists
>>> [ name for name in data1 if name in data2 ] ['name3']
For a in data1: for b in data2: if a==b: print(a)
This is one way to do this, not the best way though
Source: https://habr.com/ru/post/921137/More articles:Double negation and execution model in Prolog - prologHow can I cast or initialize an ImageInputStream with an InputStream? - javaReceive Android GCM notifications when the app is in a stop state - androidhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/921135/revert-rows-to-default-column-value-mysql&usg=ALkJrhi2Nj3HV3lIj8pBiyNhL1zhcsDphAConvert RGBA to RGB tailored - javascript400 Excel Macro Error - vbaAndroid: IO File Errors - javaENOENT (There is no such file or directory) when trying to open a file downloaded from a URL using AsyncTask - androidNSUrlConnection sendAsynchronousRequest and self-signed certificates - iosRails: Rubymine: GitHub - ruby-on-railsAll Articles