我正在尝试展平一个列表,该列表是两个列表之间的zip结果。对于代码,我从99个不同的网页中获取19个数据值。当我压缩这两个代码时,我将每个网页的标题放在19个嵌套数据列表的前面。现在,我想展平整个列表,并保持相同的顺序。我尝试了许多不同的拼合代码,但没有消除每个单独的数据列表周围的方括号的问题。救命! :)
我已经尝试过itertools,chain,for循环以除去子集-我最近两天在Google搜索中找到的所有内容。
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
例如,如果使用itertools,它会切掉括号,但始终保留方括号。我打印了列表的长度,它是邻居头的数量加上每个数据列表的1。
答案 0 :(得分:2)
您可以先将population_data
解压缩到一个名称元组和一个包含数字元组的列表中。
然后我们遍历名称和数字,并将它们加入自己的子列表中
然后,我们最终将所有子列表平整为一个大列表
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
#Unzip population data into list of names and numbers
name, *li = zip(*population_data)
#name = ('Billbrook', 'Billstedt', 'Borgfelde', 'HafenCity', 'Hamburg-Altstadt', '...
#li = [(['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1l......
#Join the name and numbers in a list
res = [[name[idx]]+li[0][idx] for idx in range(len(name))]
#res = [['Billbrook', '2350', '2354', '2032', '1783', '1526', '1....
#Join all sublists together
concat_list = [j for i in res for j in i]
print(concat_list)
输出将为
['Billbrook', '2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992', 'Billstedt', '68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355', 'Borgfelde', '6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696', 'HafenCity', '-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592', 'Hamburg-Altstadt', '1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272', 'Hamm', '35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773', 'Hammerbrook', '565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323', 'Horn', '35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799', 'Kleiner Grasbrook und Steinwerder', '1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239', 'Neustadt', '11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920', 'Rothenburgsort', '8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268', 'St.Georg', '10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384', 'St.Pauli', '26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436', 'Veddel', '4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632', 'Waltershof und Finkenwerder', '11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808', 'Wilhelmsburg', '46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068']
答案 1 :(得分:1)
使用:
from itertools import chain
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
result = []
for elem in chain.from_iterable(population_data):
if isinstance(elem, list): #Check if element is list object
result.extend(elem) #Extend result
else:
result.append(elem)
print(result)
答案 2 :(得分:0)
我完全不知道您想如何展平它,下面的代码将对行和列进行展平。
# Flattening
flattened = list(map(lambda t : [t[0]] + t[1], population_data))
# Flattened data printing
for i in flattened:
print(i)
print('-' * 40)
# Transposed data printing
for k in zip(*flattened):
print(k)
答案 3 :(得分:0)
尝试一下:
"componentColumns": ["ID", "NAME"],
"componentQuery": "SELECT Vertical_Id, Vertical_Desc FROM D_DW_COMERCIAL.BAS_Vertical ORDER BY Vertical_Desc",
"componentResultSet": [
["1", "Name1"],
["2", "Name2"],
....
]
答案 4 :(得分:0)
reduce
轻松创建您需要使用初始化器-reduce
函数中的第三个参数。
reduce(
lambda result, _list: result.append(_list[0]) or result.extend(_list[1]) or result,
population_data,
[])
以上代码适用于python2和python3,但是您需要将reduce模块导入为from functools import reduce
。有关详细信息,请参见下面的链接。