-9

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.

Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
anonymous
  • 83
  • 1

4 Answers4

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

In JS you can do something like this using filter:

var arr1 = [1, 2, 3, 4, 5];
var arr2 = [3, 10, 12, 4, 15];

function findCommon(arr1, arr2) {
    return arr1.filter(function (el) {
        return arr2.indexOf(el) > -1;
    });
}

findCommon(arr1, arr2); // [3, 4]

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
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
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