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)]
Related
I have two lists, for example they look something like this:
list1 = ["Time1Person001", "Time1Person002", "Time1Person003", "Time1Person004", "Time1Person005"]
list2 = ["Time2Person001", "Time2Person003", "Time2Person004", "Time2Person007"]
I want to create a third list that contains only strings that share a substring of the last 3 charachters, so the output should be:
list3 = ["Time1Person001", "Time1Person003", "Time1Person004", "Time2Person001", "Time2Person003", "Time2Person004"]
Efficient way to do it?
Thanks!
Create a set of the common endings then filter each list by that set.
list1 = ["Time1Person001", "Time1Person002", "Time1Person003", "Time1Person004", "Time1Person005"]
list2 = ["Time2Person001", "Time2Person003", "Time2Person004", "Time2Person007"]
endings = set(v[-3:] for v in list1) & set(v[-3:] for v in list2)
list3 = [v for v in list1+list2 if v[-3:] in endings]
Is this what you're after:
list3 = [x for x in list1 for y in list2 if x[-3:] == y[-3:]]
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'}
I have a list with n elements but i would like to convert it to a list which contains n list, and every list contains a single element.
a = ['1','2','3','4']
b = [['1'],['2'],['3'],['4']]
How can I make from a to b?
You can try list comprehension
b = [[i] for i in a]
You can use map:
b = list(map(lambda x: [x], a))
or a list comprehension:
b = [[i] for i in a]
You can iterate through the a list and append new list with the item to b list.
a = ['1','2','3','4']
b = []
for i in range(len(a)):
b.append([a[i]])
print(b)
This is basically the same solution as the one from JRodDynamite, but more readable for beginners.
Very Simple Solution Could be from generators
a = ['1','2','3','4']
b = [[item] for item in a]
I know this won't work but you guys get the idea.
c = [m.split('=')[1] as a for m in matches if a != '1' ]
Is there a way to archive this? If you use a list comprehension like
c = [m.split('=')[1] as a for m in matches if m.split('=')[1] != '1' ]
two lists will be build from the split, right?
You can use use a generator expression inside the list comprehension:
c = [a for a in (m.split('=')[1] for m in matches) if a != '1']
It's sorta-possible, but when you find yourself resorting to awful hacks like the following, it's time to use a regular loop:
c = [a for m in matches for a in [m.split('=')[1]] if a != '1']
You can't do it, and there's no real point in using a nested map or nested list comprehension as the other solutions show. If you want to preprocess the list, just do:
whatIwant = (m.split('=')[1] for m in matches)
c = [a for a in whatIwant if a != 1]
Using a nested list comp or map saves nothing, since the entire list is still processed. All it does is reduce readability.
Something like this perhaps:
c = [ a for a, m in map(lambda x: (x.split('=')[1], x), matches) if a != '1' ]
you may want to use imap instead of map. Some cleaner version:
def right_eq(x): return (x.split('=')[1], x)
c = [ a for a, m in imap(right_eq, matches) if a != '1' ]
I have two lists where I am trying to see if there is any matches between substrings in elements in both lists.
["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
["2311","233412","2231"]
If any substrings in an element matches the second list such as "Po2311tato" will match with "2311". Then I would want to put "Po2311tato" in a new list in which all elements of the first that match would be placed in the new list. So the new list would be ["Po2311tato","Pin2231eap","Orange2231edg"]
You can use the syntax 'substring' in string to do this:
a = ["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
b = ["2311","233412","2231"]
def has_substring(word):
for substring in b:
if substring in word:
return True
return False
print filter(has_substring, a)
Hope this helps!
This can be a little more concise than the jobby's answer by using a list comprehension:
>>> list1 = ["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
>>> list2 = ["2311","233412","2231"]
>>> list3 = [string for string in list1 if any(substring in string for substring in list2)]
>>> list3
['Po2311tato', 'Pin2231eap', 'Orange2231edg']
Whether or not this is clearer / more elegant than jobby's version is a matter of taste!
import re
list1 = ["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
list2 = ["2311","233412","2231"]
matchlist = []
for str1 in list1:
for str2 in list2:
if (re.search(str2, str1)):
matchlist.append(str1)
break
print matchlist