I have two lists of protein sequences, I have to check every entry's existence in the two lists, say like

list A = [1,2,3,4]
list B= [3,4,5]

result = [
[1, true, false],
[2, true, false],
[3, true, true],
[4, true, true],
[5, false, true]

I did it this way. Not sure if it's the right way

def FindDifferences():    
    df1 = pd.read_csv('Gmax_v6_annotation_info.txt', names=['name'], usecols=[0], delimiter='\t')
    df2 = pd.read_csv('Gmax_v9_annotation_info.txt', names=['name'], usecols=[2], delimiter='\t')
    v6_set = set(df1['name'])
    v9_set = set(df2['name'])
    result = []
    for val in v6_set:
        if val in v9_set:
            result.append([val, True, True])
            result.append([val, True, False])
    for val in v9_set:
        if val not in v6_set:
            result.append([val, False, True])
    result_df = pd.DataFrame(result, columns=['name', 'inv6', 'inv9'])
    result_df.to_csv('result_csv.csv', index=False, header=False)
  • 1
    \$\begingroup\$ I can't test this code or any improvements without the txt files. \$\endgroup\$ – hpaulj Mar 16 '17 at 21:40
  • \$\begingroup\$ @hpaulj Sorry about that I don't have the permission to release those files :( \$\endgroup\$ – Bobby Mar 17 '17 at 0:13
  • \$\begingroup\$ I don't expect to see the real thing. But to get good answers you need to post something that works (and is small) The smart thing is to identify the core of the problem and post it, along with some 'toy' data. Instead of including the file read/write just give some toy values of v6_set etc. \$\endgroup\$ – hpaulj Mar 17 '17 at 0:26
  • \$\begingroup\$ @hpaulj the real data is almost exactly the same after filtering out the useful columns. literally, two lists containing strings instead of the toy ints \$\endgroup\$ – Bobby Mar 17 '17 at 0:29
  • \$\begingroup\$ Just in case anyone is curious. The real pythonic way to do this is in the answer here stackoverflow.com/questions/42847053/… \$\endgroup\$ – Bobby Mar 17 '17 at 0:38

General problem

It's not much, but for your general problem, mathematically speaking, I'd split the logic into three steps (It might be a bit more code, but personally speaking, it keeps things cleaner):

  1. I'd intersect list_A with list_B:

    def intersect(list_A, list_B):
        return [[x, True, True] for x in set(list_A).intersection(list_B)]

    Which is going to give you:

    [[3, True, True], [4, True, True]]

  2. Then, I'd do list_A - list_B:

    def a_diff_b(list_A, list_B):
        return [[x, True, False] for x in set(list_A).difference(list_B)]

    Which is going to give you:

    [[1, True, False], [2, True, False]]

  3. Then, I'd do list_B - list_A:

    def b_diff_a(list_A, list_B):
        return [[x, False, True] for x in set(list_B).difference(list_A)]

    Which is going to give you:

    [[5, False, True]]


In mathematics, the intersection A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements.


In set theory, the complement of a set A refers to elements not in A. The relative complement of A with respect to a set B, also termed the difference of sets A and B, written B ∖ A, is the set of elements in B but not in A. When all sets under consideration are considered to be subsets of a given set U, the absolute complement of A is the set of elements in U but not in A.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.