I have the following list
called flat
:
flat= ['11','13', '17', '19', '13', '17', '19','22','35','14','15']
I splitted it into a list
of tuples
using zip
function as
list(zip(flat[0::2], flat[1::2]))
with the following output:
[('11', '13'), ('17', '19'), ('13', '17'), ('19', '22'), ('35', '14')]
Now, how to convert the list flat
into the following result using list comprehension?
[['11', '13'], ['17', '19'], ['13', '17'], ['19', '22'], ['35', '14']]
Method 1: Without list Comprehension
You need to map
zip
object to list
:
list(map(list,zip(flat[0::2], flat[1::2])))
[['11', '13'], ['17', '19'], ['13', '17'], ['19', '22'], ['35', '14']]
Method 2 Using list comprehension:
[list(l) for l in zip(flat[0::2], flat[1::2])]
[['11', '13'], ['17', '19'], ['13', '17'], ['19', '22'], ['35', '14']]
Or:
[[l1,l2] for l1,l2 in zip(flat[0::2], flat[1::2])]
[['11', '13'], ['17', '19'], ['13', '17'], ['19', '22'], ['35', '14']]
A pure list comprehension approach:
[[flat[a], flat[a+1]] for a in range(0, len(flat)-2, 2)]
Here range(0, len(flat)-2, 2)
allows iteration through the indices of the flat array in steps of two, and [flat[a], flat[a+1]]
gets a pair of elements as a list. This approach will fail if the orginal list has a length which is not a multiple of 2.
Using a list comprehension you could use:
[[flat[i], flat[i+1]] for i in range(0, len(flat)-1, 2)]
which yields
[['11', '13'], ['17', '19'], ['13', '17'], ['19', '22'], ['35', '14']]
Consider using itertools for avoiding duplicating the list twice:
import itertools
flat= ['11','13', '17', '19', '13', '17', '19','22','35','14','15']
result = list(
map(list,
zip(
itertools.islice(flat, 0, len(flat), 2),
itertools.islice(flat, 1, len(flat), 2)
)
)
)
print(result)
or use iter
:
it = iter(flat)
[[x, y] for x, y in zip(it, it)]
Here you have the live example
The tests with timeit
report that the iter
solution with the list comprehension are the better aproaches:
import itertools
flat= ['11','13', '17', '19', '13', '17', '19','22','35','14','15']
def lcomprehension_aproach(flat):
return [[flat[a], flat[a+1]] for a in range(0, len(flat)-2, 2)]
def itertools_aproach(flat):
result = list(
map(list,
zip(
itertools.islice(flat, 0, len(flat), 2),
itertools.islice(flat, 1, len(flat), 2)
)
)
)
return result
def iter_aproach(flat):
it = iter(flat)
return [[x, y] for x, y in zip(it, it)]
print(lcomprehension_aproach(flat))
print(itertools_aproach(flat))
print(iter_aproach(flat))
from timeit import timeit
print(timeit("lcomprehension_aproach(flat)", globals=globals()))
print(timeit("itertools_aproach(flat)", globals=globals()))
print(timeit("iter_aproach(flat)", globals=globals()))
5.122298364993185 // list comprehension time (Jack Aidley solution)
8.221981940994738 // itertools time
3.757848952009226 // itert + comprehension time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.