Merging every 2 elements of a list in Python [duplicate] - python

This question already has answers here:
list comprehension joining every two elements together in a list
(4 answers)
Joining pairs of elements of a list [duplicate]
(7 answers)
Closed 9 months ago.
So let's say I have a list as follows:
Li = ['1', '2', '3', '4', '5', '6', '7', '8']
I want to have a list modification to have this:
Li = ['12', '34', '56', '78']
Is it possible to merge every 2 elements of a list together?

>>> Li = ['1', '2', '3', '4', '5', '6', '7', '8']
>>> [''.join(Li[i:i+2]) for i in range(0, len(Li), 2)]
['12', '34', '56', '78']

And if you prefer functional python:
list(map(lambda x: x[0]+x[1], zip(*[iter(Li)] * 2)))

You could use map on striding subsets of the list that step by 2 and are offset by 1:
list(map(str.__add__,Li[::2],Li[1::2]+[""]))
['12', '34', '56', '78']
Note: The +[""] is there to cover cases where the list has an odd number of elements

Related

How to split a list in python like below? [duplicate]

This question already has answers here:
Joining pairs of elements of a list [duplicate]
(7 answers)
Closed 1 year ago.
['4', '5', '6', '8', '5', '9', '6', '2', '6', '5']
How can I get it to look like this:
[45,68,59,62,65]
How to combine two or more list elements like ['4','5'] to [45]
The strategy is to pair the initial data, then convert to int, zip and slices are a good way dot that
values = ['4', '5', '6', '8', '5', '9', '6', '2', '6', '5']
result = [int(a + b) for a, b in zip(values[::2], values[1::2])]
print(result) # [45, 68, 59, 62, 65]
values[::2] : one over two ['4', '6', '5', '6', '6']
values[1::2] : over over two, starting at 2nd ['5', '8', '9', '2', '5']

List Comprehension - Conditional [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 22 days ago.
For these 2 lists:
A=['3.40', '4.00', '151.00', '8.00', '81.00', '23.00', '17.00', '8.50', '5.00', '151.00', 'SCR', 'SCR', '13.00']
B=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '12', '10', '3']
The desired output is:
C=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '3']
So - list 'A' and list 'B' are the same length. List 'C' is the same as list 'B' - but does not have the values where 'SCR' exists in list 'A'.
My attempt at this is:
C = [x for x in B if x in A!='SCR']
Thankyou
just zip them together:
C = [b for a,b in zip(A,B) if a != 'SCR']
Based on what I think you're trying to accomplish, I think you would need this:
C = [B[x] for x in range(len(B)) if A[x] != 'SCR']
This is straightforward using the built-in enumerate function:
[x for (idx, x) in enumerate(B) if A[idx] == 'SCR']

Pythonic fast way for working with dict and array [duplicate]

This question already has answers here:
How to replace elements in a list using dictionary lookup
(6 answers)
Closed 5 years ago.
I am working with really huge data size. I have a calculation in my code that take much much much time. the code is as below:
chester_output_connections = ['0', '5', '0', '1', '0', '2', '0', '3', '0', '4', '1', '9', '1', '2', '1', '6', '1', '8', '2', '9', '2', '3', '2', '8', '3', '9', '3', '7', '3', '8']
dic = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, '2fit': 7}
for index,item in enumerate(chester_output_connections):
for k,v in dic.items():
if str(v)==str(item):
chester_output_connections[index]=str(k)
Please make suggestions for better run time.
It might help if you don't convert the comparison to a string
if str(v)==str(item):
maybe (since item is already a string)
if str(v)==item:
might speed it up a tad.. ??

Sum numbers in a list of lists in Python 2.7

I've got a list of lists in Python 2.7 with numbers in strings:
[['1', '2'], ['3'], ['4', '5', '6']]
How could I sum all the numbers in these lists?
You have first chain all the lists, then convert them to int using map and finally sum them.
import itertools
m = [['1', '2'], ['3'], ['4', '5', '6']]
print sum(map(int, list(itertools.chain(*m))))
Output:
21
As an alternate solution to chaining them you could use list comprehension and convert each element to an int and then sum the new list
l = [['1', '2'], ['3'], ['4', '5', '6']]
print sum([int(j) for i in l for j in i])
21

Order of python generator expression

I have an example where the iteration order of a python generator expression seems to be different from the analogous list comprehension and generator function.
For example, the code
n = 10
d = {i : str(i) for i in range(n)}
for i in d:
print d[i],
print
def func_gen(d):
for i in range(n):
yield d[i]
print(list(func_gen(d)))
g = [d[i] for i in range(n)]
print(g)
g = {d[i] for i in range(n)}
print(list(g))
has the output
0 1 2 3 4 5 6 7 8 9
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['1', '0', '3', '2', '5', '4', '7', '6', '9', '8']
I would expect the generator expression to produce an ordered output like it does with the list comprehension. Is there something about generator expressions that
says the order isn't guaranteed?
Why is the order for the generator expression different from the others, even though the constructs are almost identical? It isn't even the same order as iterating through the dictionary, so it doesn't seem like
it's deferring to that.
I'm seeing the same behavior in Python 2 and 3.
Dicts aren't ordered. You can't rely on their items to come back in any particular order, even between function calls.

Categories

Resources