Identifying common element in list in python - python

I have two lists. I need to compare elements whether any element in the list is matching or not.
Input:
a = ['1001,1715']
b = ['1009,1715']
Output : 1715
Please suggest how to do it?
I tried doing:
set(''.join(a))
and
set(''.join(b))
but it gave me {'5', '0', '7', ',', '1'}. how can I convert ['1001,1715'] to [1001,1715] ?

a = ['1001,1715']
b = ['1009,1715']
def fun(a):
return a[0].split(",")
def intersect(a, b):
return list(set(a) & set(b))
print(intersect(fun(a),fun(b)))

There are 2 parts to your problem.
Convert strings to sets of integers
Since your string is the only element of a list, you can use list indexing and str.split with map:
a_set = set(map(int, a[0].split(',')))
b_set = set(map(int, b[0].split(',')))
Calculate the intersection of 2 sets
res = a_set & b_set
# alternatively, a_set.intersection(b_set)
print(res)
{1715}

You can use set intersection:
set(a[0].split(',')).intersection(set(b[0].split(',')))
Which returns:
{'1715'}
Converting from '1001,1715' to ['1001', '1715'] can simply be done with .split(',')

A more general solution if you have lists with more elements (e.g a = ['1001,1715','1009,2000'] )
a = [x for xs in a for x in xs.split(',')]
b = [x for xs in b for x in xs.split(',')]
common = set(a).intersection(set(b))
Example:
a = ['1001,1715','1009,2000']
b = ['1009,1715']
Output:
{'1009', '1715'}

Related

How can it taken the specific args in lists of list?

for example we have;
L = [["Ak","154"],["Bm","200"],["Ck","250"], ["Ad","500"],["Ac","600"]]
I want to choose first element starting with 'A' I want to find their values which are in second element; see this output should like
["154","500","600"] or like [["154"],["500"],["600"]]
Filter and map with a list comprehension:
[b for a, b in L if a[0] == "A"]
Or, if you need to search for prefixes of more than one character:
[b for a, b in L if a.startswith("A")]
Another Solution Using map() and filter() functions
L = [["Ak","154"],["Bm","200"],["Ck","250"], ["Ad","500"],["Ac","600"]]
k = list(map(lambda y:y[1], list(filter(lambda x: x[0][0] == 'A' , L))))
Output:
['154', '500', '600']

How to compare two lists to keep matching substrings?

As best I can describe it, I have two lists of strings and I want to return all results from list A that contain any of the strings in list B. Here are details:
A = ['dataFile1999', 'dataFile2000', 'dataFile2001', 'dataFile2002']
B = ['2000', '2001']
How do I return
C = ['dataFile2000', 'dataFile2001']?
I've been looking into list comprehensions, doing something like below
C=[x for x in A if B in A]
but I can't seem to make it work. Am I on the right track?
You were close, use any:
C=[x for x in A if any(b in x for b in B)]
More detailed:
A = ['dataFile1999', 'dataFile2000', 'dataFile2001', 'dataFile2002']
B = ['2000', '2001']
C = [x for x in A if any(b in x for b in B)]
print(C)
Output
['dataFile2000', 'dataFile2001']
You can use any() to check if any element of your list B is in x:
A = ['dataFile1999', 'dataFile2000', 'dataFile2001', 'dataFile2002']
B = ['2000', '2001']
c = [x for x in A if any(k in x for k in B)]
print(c)
Output:
['dataFile2000', 'dataFile2001']
First, I would construct a set of the years for the O(1) lookup time.1
>>> A = ['dataFile1999', 'dataFile2000', 'dataFile2001', 'dataFile2002']
>>> B = ['2000', '2001']
>>>
>>> years = set(B)
Now, keep only the elements of A that end with an element of years.
>>> [file for file in A if file[-4:] in years]
>>> ['dataFile2000', 'dataFile2001']
1 If you have very small lists (two elements certainly qualify) keep the lists. Sets have O(1) lookup but the hashing still introduces overhead.

Matching characters in strings

I have 2 lists of strings:
list1 = ['GERMANY','FRANCE','SPAIN','PORTUAL','UK']
list2 = ['ERMANY','FRANCE','SPAN','PORTUGAL','K']
I wanted to obtain a list where only the respective strings with 1 character less are shown. i.e:
final_list = ['ERMANY','SPAN','K']
What's the best way to do it? Using regular expressions?
Thanks
You can try this:
list1 = ['GERMANY','FRANCE','SPAIN','PORTUGAL','UK']
list2 = ['ERMANY','FRANCE','SPAN','PORTUGAL','K']
new = [a for a, b in zip(list2, list1) if len(a) < len(b)]

python: compare two lists and return matches in order

I have two lists of unequal length and I would like to compare and pull matched values in the order from the first list, so a in this example.
a = ['a','s','d','f']
b = ['e','d','y','a','t','v']
expected output:
['a','d']
I was doing it like this but I forgot that set doesnt retain the order! how can I edit my code below to retain the order.
set(a).intersection(b)
Linked to this How can I compare two lists in python and return matches
Convert b to a set and then loop over a's items and check if they exist in that set:
>>> s = set(b)
>>> [x for x in a if x in s]
['a', 'd']
you need to use set:
>>> a = ['a','s','d','f']
>>> b = ['e','d','y','a','t','v']
>>> sorted(set(a) & set(b), key=a.index) # here sorting is done on the index of a
['a', 'd']
a = ['a','s','d','f']
b = ['e','d','y','a','t','v']
st_b = set(b)
print([ele for ele in a if ele in st_b])
['a', 'd']
a = ['a','s','d','f']
b = ['e','d','y','a','t','v']
matches=[]
for item_a in a:
for item_b in b:
if item_a == item_b:
matches.append(item_a)
print(matches)

How to remove an array containing certain strings from another array in Python

Example:
a = ['abc123','abc','543234','blah','tete','head','loo2']
So I want to filter out from the above array of strings the following array b = ['ab','2']
I want to remove strings containing 'ab' from that list along with other strings in the array so that I get the following:
a = ['blah', 'tete', 'head']
You can use a list comprehension:
[i for i in a if not any(x in i for x in b)]
This returns:
['blah', 'tete', 'head']
>>> a = ['abc123','abc','543234','blah','tete','head','loo2']
>>> b = ['ab','2']
>>> [e for e in a if not [s for s in b if s in e]]
['blah', 'tete', 'head']
newA = []
for c in a:
for d in b:
if d not in c:
newA.append(c)
break
a = newA

Categories