I'm trying to create a list that is combining two lists and some strings:
string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
result = ["test.1.a","test.2.b","test.3.b"]
I tried messing around with .join and zip functions to no avail.
I believe that a pythonic way could be the following, using zip and list comprehension:
output = ["{}.{}.{}".format(string, a, b) for a, b in zip(list1, list2)]
This works:
['.'.join((string,)+i) for i in zip(list1, list2)]
Output:
['test.1.a', 'test.2.b', 'test.3.c']
string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
lst = ['{}.{}.{}'.format(string, list1[x], list2[x]) for x in range(len(list1))]
print(lst)
Output
['test.1.a', 'test.2.b', 'test.3.c']
Simply use concatenation trick:
string1 = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
result = [(string1+"."+list1[i]+"."+list2[i]) for i in range(len(list1))]
print(result)
Output:
['test.1.a', 'test.2.b', 'test.3.c']
List Comprehension:
string = "test"
list(map(lambda x,y: string+"."+x+"."+y, list1,list2))
Output:
['test.1.a', 'test.2.b', 'test.3.c']
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 a list:
List = [('4022-a751',), ('0bfc-4d53',)]
And want to resolve it into the output below:
Output = ['4022-a751','0bfc-4d53']
You should read about List Comprehensions in Python
list_ = [('4022-a751',), ('0bfc-4d53',)]
res = [x for item in list_ for x in item]
Output
['4022-a751', '0bfc-4d53']
A tuple can be manipulated like an array with index.
input_arr = [('4022-a751',), ('0bfc-4d53',)]
output_arr = [a[0] for a in input_arr]
print(output_arr)
You can use this.
old_list= [('4022-a751',), ('0bfc-4d53',)]
new_list = [''.join(i) for i in old_list]
print(new_list)
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)]
Im looking for the best way to take a list of stirngs, generate a new list with each item from the previous list concatenated with a specific string.
Example sudo code
list1 = ['Item1','Item2','Item3','Item4']
string = '-example'
NewList = ['Item1-example','Item2-example','Item3-example','Item4-example']
Attempt
NewList = (string.join(list1))
#This of course makes one big string
If you want to create a list, a list comprehension is usually the thing to do.
new_list = ["{}{}".format(item, string) for item in list1]
Use string concatenation in a list comprehension:
>>> list1 = ['Item1', 'Item2', 'Item3', 'Item4']
>>> string = '-example'
>>> [x + string for x in list1]
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
An alternative to list comprehension is using map():
>>> map(lambda x: x+string,list1)
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
Note, list(map(lambda x: x+string,list1)) in Python3.
concate list item and string
>>>list= ['Item1', 'Item2', 'Item3', 'Item4']
>>>newList=[ i+'-example' for i in list]
>>>newList
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
I am trying to write a list comprehension that uses List1 to create a list of words of length 4.
List1 = ['jacob','batman','mozarella']
wordList = [words for i in range(1)]
print(wordList)
This prints out the wordList however with words of length higher than 4
I am looking for this program to print out instead:
['jaco','batm','moza']
which are the same words in List1 but with length 4
I tried this and it didn't work
wordList = [[len(4)] words for i in range(1)]
any thoughts ?
You could use this list comp
>>> List1 = ['jacob','batman','mozarella']
>>> [i[:4] for i in List1]
['jaco', 'batm', 'moza']
Ref:
i[:4] is a slice of the string of first 4 characters
Other ways to do it (All have their own disadvantages)
[re.sub(r'(?<=^.{4}).*', '', i) for i in List1]
[re.match(r'.{4}', i).group() for i in List1]
[''.join(i[j] for j in range(4)) for i in List1]
[i.replace(i[4:],'') for i in List1] ----- Fails in case of moinmoin or bongbong
Credit - Avinash Raj
len() function return the length of string in your case. So list compression with len function will give the list of all item lenght.
e.g.
>>> List1 = ['jacob','batman','mozarella']
>>> [len(i) for i in List1]
[5, 6, 9]
>>>
Use slice() list method to get substring from the string. more info
e.g.
>>> a = "abcdef"
>>> a[:4]
'abcd'
>>> [i[:4] for i in List1]
['jaco', 'batm', 'moza']
Python beginner
Define List1.
Define empty List2
Use for loop to iterate every item from the List1
Use list append() method to add item into list with slice() method.
Use print to see result.
sample code:
>>> List1 = ['jacob','batman','mozarella']
>>> List2 = []
>>> for i in List1:
... List2.append(i[:4])
...
>>> print List2
['jaco', 'batm', 'moza']
>>>
One more way, now using map function:
List1 = ['jacob','batman','mozarella']
List2 = map(lambda x: x[:4], List1)