This question already has answers here:
Most pythonic way to interleave two strings
(14 answers)
Closed 4 years ago.
How to join two or more strings in python with one character after another?
For example
a = 'hello'
b = 'world'
output = 'hweolellod'
Same goes for three or more strings. using + is not helping.
You could try this:
''.join([x + y for x, y in zip(a, b)])
which gives:
'hweolrllod'
One way is to use str.join with itertools:
from itertools import chain, zip_longest
a = 'hello'
b = 'world'
zipper = zip_longest(a, b, fillvalue='')
res = ''.join(list(chain.from_iterable(zipper)))
print(res)
hweolrllod
Explanation
zip_longest is used to account for inconsistent length strings.
zipper here is a lazy iterator which iterates each character of a and b simultaneously by index.
List creation, while not necessary, is more efficient with str.join.
Related
This question already has answers here:
Python code that will find words made out of specific letters. Any subset of the letters could be used [duplicate]
(4 answers)
Closed 3 years ago.
S = ['cat','tor','tutorial','item','aba','tori']
T = "oti"
for item in S:
for i in item:
if i in T:
print(item)
I want to get 'tori' and 'tutorial' because those are the 2 words that contain o, t, i. The code above will print everything from the list. Using find() is not an option because there is no match.
Will using match be a good option here? I could use some hint here. Thanks in advance.
Use sets for membership testing.
>>>
>>> T = 'oti'
>>> S = ['cat','tor','tutorial','item','aba','tori']
>>> t = set(T)
>>> t
{'o', 't', 'i'}
>>> for thing in S:
... if t.issubset(thing):
print(thing)
tutorial
tori
>>>
This won't work if there are duplicate characters.
>>> q = set('otti')
>>> for thing in S:
... if q.issubset(thing):
print(thing)
tutorial
tori
>>>
Using all
>>>
>>> for thing in S:
... if all(c in thing for c in T):
print(thing)
tutorial
tori
>>>
This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 5 years ago.
I have two lists:
A = ['John,Male,20,','Jenny,Female,25','James,Male,30']
B = ['London','Paris','Washington']
Is there a way I can insert the values in the second list into the first list
Hypothetical output:
['John,Male,20,London','Jenny,Female,25,Paris','James,Male,30,Washington']
Use zip to join the lists into lists of pairs. That would give you:
>>> zip(A, B)
[('John,Male,20,', 'London'), ('Jenny,Female,25', 'Paris'), 'James,Male,30', 'Washington')]
Then you can concatenate the strings in each pair using a list comprehension:
>>> [a + b for a, b in zip(A, B)]
['John,Male,20,London', 'Jenny,Female,25,Paris', 'James,Male,30,Washington']
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 9 years ago.
I have two lists. How can I define a function "remove_repeat_element" to remove repeat elements from them ? ?
def remove_repeat_element(a, b):
... ...
a = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238']
b = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238', '3c26f383-da50-446c-8613-64e1068bd57e']
result = remove_repeat_element(a, b)
print result
>>> ['3c26f383-da50-446c-8613-64e1068bd57e']
Could someone give me some advice ??
Thanks a lot!
You can use sets :
>>> a = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238']
>>> b = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238', '3c26f383-da50-446c-8613-64e1068bd57e']
>>> list(set(b) - set(a))
['3c26f383-da50-446c-8613-64e1068bd57e']
My suggestion:
c = [x for x in b if x not in a]
It is pretty straight forward to write and efficient enough as a list comprehension.
Hope this helps!
Update:
For more efficient membership checking, use set instead of list.
a = set(a)
c = [x for x in b if x not in a]
This would be even faster and not to mention the improvement when the list is large.
return [el for el in b if el not in a]
This question already has answers here:
Add a character to each item in a list [duplicate]
(3 answers)
Closed 9 years ago.
I have a string which I want to concatenate with every object within a list. Here is an example:
a = ['1','2']
b = 'a'
and I want:
c = ['a1','a2']
It seems that strings can't be concatenated to list objects directly so I assume that I should convert my list to the string and then add it. Is it correct or any suggestions?
Try Python list comprehensions.
>>> a = ['1','2']
>>> b = 'a'
>>> [b+i for i in a]
['a1', 'a2']
This question already has answers here:
Removing duplicates from a list of lists
(16 answers)
Closed 4 years ago.
I want to remove all duplicates list from a list of list.
So I have a list of lists like this.
a = [[1,2],[1,2],[3,4,5],[3,4,5],[3,4,5]]
I want to have:
b = [[1,2],[3,4,5]]
I don't know how to do it.
You could use a set:
b_set = set(map(tuple,a)) #need to convert the inner lists to tuples so they are hashable
b = map(list,b_set) #Now convert tuples back into lists (maybe unnecessary?)
Or, if you prefer list comprehensions/generators:
b_set = set(tuple(x) for x in a)
b = [ list(x) for x in b_set ]
Finally, if order is important, you can always sort b:
b.sort(key = lambda x: a.index(x) )
See mgilson's answer if the order of the lists is not important. If you want to retain the order, do something like:
b = list()
for sublist in a:
if sublist not in b:
b.append(sublist)
This will keep the order in the original list. However, it is slower and more verbose than using sets.