对 Python 中的列表的每 n 个列表进行分组
Grouping every nth lists of list in Python
我正在尝试将每 4 个列表分组为 python 中的一个列表。每当我抓取数据时,它 returns 看起来像这样:
[['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
我希望输出现在变成:
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
有什么想法吗?谢谢!
您可以执行以下操作:
l = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
res = res = [sum(l[i:i+4], []) for i in range(0, len(l), 4)]
输出是:
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
Sum of Nested List with Empty List Explanation
提供了有关我合并列表方式的说明
https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
提供了列表理解的解释
我会使用 reduce,
import functools
l = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
[functools.reduce(list.__add__, l[:4])] + [functools.reduce(list.__add__, l[4:])] # [['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
没有列表理解:
lst = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'],
['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
res = []
while lst:
res.append(sum(lst[:4], []))
lst = lst[4:]
print(res)
输出
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
我正在尝试将每 4 个列表分组为 python 中的一个列表。每当我抓取数据时,它 returns 看起来像这样:
[['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
我希望输出现在变成:
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
有什么想法吗?谢谢!
您可以执行以下操作:
l = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
res = res = [sum(l[i:i+4], []) for i in range(0, len(l), 4)]
输出是:
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
Sum of Nested List with Empty List Explanation
提供了有关我合并列表方式的说明https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
提供了列表理解的解释
我会使用 reduce,
import functools
l = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'], ['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
[functools.reduce(list.__add__, l[:4])] + [functools.reduce(list.__add__, l[4:])] # [['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]
没有列表理解:
lst = [['Ethnicity /Sex'], ['DOB /Age'], ['Booking Date'], ['Release Date'],
['AMERICAN (US)', 'FEMALE'], ['04/07/1968', '52 years old'], ['1/2/2020 4:34 AM'], ['1/2/2020 8:47 PM']]
res = []
while lst:
res.append(sum(lst[:4], []))
lst = lst[4:]
print(res)
输出
[['Ethnicity /Sex', 'DOB /Age', 'Booking Date', 'Release Date'], ['AMERICAN (US)', 'FEMALE', '04/07/1968', '52 years old', '1/2/2020 4:34 AM', '1/2/2020 8:47 PM']]