I have 2 long lists of chemicals (approx. 100 chemicals each) and would like to find which chemical are common in both lists. I basically need a program that will allow me to insert 2 long lists and then print out the chemicals that are common. I currently only know java-script and python and am not sure how to do this without going through every chemical in at least 1 of the 2 list.
Asked
Active
Viewed 76 times
-9
-
2what you tried already ? – Muhammad Usman Sep 18 '15 at 13:00
-
I have inserted one list into the script and then inputted the chemicals from the other list to see if the chemical that I inputted is in the list that is in the script. – anonymous Sep 18 '15 at 13:03
-
2You should be posting what you've tried not asking for code – Daniel Rosano Sep 18 '15 at 13:17
4 Answers
1
You can try underscore.js for variety of operations in JavaScript.
_.difference :
Returns the values from array that are not present in the other arrays.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]
You can try it in python like
list(set([1, 2, 3, 4, 5]) - set([5, 2, 10]))
=> [1, 3, 4]

Vipul Bhatt
- 372
- 4
- 10
0
You could use sets to do this. I would though recommend converting each entry to lowercase first though as follows:
list_1 = ["Barrelene", "Mimimycin"]
list_2 = ["Prismane", "barrelene", "Josiphos"]
set_1 = set([x.lower() for x in list_1])
set_2 = set([x.lower() for x in list_2])
print set_1.intersection(set_2)
This would display:
set(['barrelene'])
If the two lists are in text files, you could use the following script:
with open('list_1.txt', 'r') as f_list_1:
set_1 = set([name.strip().lower() for name in f_list_1])
with open('list_2.txt', 'r') as f_list_2:
set_2 = set([name.strip().lower() for name in f_list_2])
for name in set_1.intersection(set_2):
print name
I would suggest you use Microsoft Word to save your two documents as a text file. It is though also possible to use a Python module to read the docx
file directly.

Martin Evans
- 45,791
- 17
- 81
- 97
-
Does that mean that I have to put quotation marks around every single chemical? – anonymous Sep 18 '15 at 13:12
-
Where are your lists? Are they in two text files, i.e. one name per line? – Martin Evans Sep 18 '15 at 13:13
-
-
I have added a version that can work via text files. Word files would need another library. – Martin Evans Sep 18 '15 at 13:31
0
l1 = [1,2,3,4]
l2 = [3,2,22]
commonInBoth = set(l1) & set(l2)
print(commonInBoth)
{2, 3}

LetzerWille
- 5,355
- 4
- 23
- 26