-4

Let's assume I have:

A = ['a','b','c','d','e']
B = ['a','b','e']

I want to compare this 2 lists and get smth like:

[True, True, False, False, True]

The problem which I have is that lists must have a same length. But how to compare if they aren't?

I need faster way, because I'm working with df with 98800 observations. I searched in internet, but I can not to find what I need.

Thank you

Pon4a
  • 27
  • 2
  • 9
  • 5
    `[x in B for x in A]` ? – jezrael Dec 04 '18 at 09:38
  • I didn't get. I know 'in' method, but I'm not sure that I understood your answer. – Pon4a Dec 04 '18 at 09:41
  • 1
    what if `B` contained a unique item `'f'`? Can you describe your problem a bit better? Is the comparison based on the values in `A` exclusively? – Ma0 Dec 04 '18 at 09:41
  • You didn't explicitely state what the rule should be to get the output if the lists don't have the same length. Try to explain it, give sample input and output. – Thierry Lathuille Dec 04 '18 at 09:42
  • And we could convert `B` to a `set` first to have better lookup time. – Matthias Dec 04 '18 at 09:42
  • You can check my answer – Hemil Patel Dec 04 '18 at 09:45
  • @Ev.Kounis sure, my bad. The concept is that 'A' is a list of all possible values(letters in an example). 'B' is a list of random letters. 'B' can not contains any uniques values, only values from 'A'. I want to get True if 'B'[i] in 'A'[i] and False if 'A'[i] not in 'B'. – Pon4a Dec 04 '18 at 09:50
  • @Pon4a, are you using dataframe or list? because they are different types of objects in python. – Mox Dec 04 '18 at 09:57
  • I have df(130x98800),but I thought if I save particular column to a list that would be faster. How do you think? – Pon4a Dec 04 '18 at 10:02
  • @Pon4a, you don't have to convert the column to list, you can simply do df['A'].isin(df['B']) – Mox Dec 04 '18 at 10:04
  • @Mox Thank you. I will try to work with df. I wrote for loop, but it tooks time for computation (but worked correct). So I will follow your advice! – Pon4a Dec 04 '18 at 10:07

2 Answers2

2

I think what you are looking for is the isin() function from Pandas

import pandas as pd
B = ['a','b','e']
df=pd.DataFrame({'A': ['a','b','c','d','e']});
df.isin(B)

Result

    A
0   True
1   True
2   False
3   False
4   True

the series version of isin()

Mox
  • 2,355
  • 2
  • 26
  • 42
-1

If you want to see which elements of list A are in list B, you can do this:

result = [x in B for x in A] 

this is called list comprehension.

This is equivalent to:

result = []
for x in A:
    if x in B:
       result.append(True)
    else:
       result.append(False)
Yakov Dan
  • 2,157
  • 15
  • 29