Python 从列表列表中创建列表,对枚举方法创建的每个列表使用相同的重复索引号
我的函数desired_headers()从“results”对象中删除元组,元组中的第一个元素与headers列表中的任何字符串都不匹配<代码>['Fish','Dolphin']来自result1对象&Python 从列表列表中创建列表,对枚举方法创建的每个列表使用相同的重复索引号,python,list,for-loop,indexing,enumerate,Python,List,For Loop,Indexing,Enumerate,我的函数desired_headers()从“results”对象中删除元组,元组中的第一个元素与headers列表中的任何字符串都不匹配['Fish','Dolphin']来自result1对象&['Local Auth','bucket']已从result3中删除 然后它打印索引号和元组列表,如下面的CurrentOutput下所示 我的目标是使用当前输出“重新打包”元组列表并存储在对象中 headers2 = ['Group', 'Owner', 'Person in charge', '
['Local Auth','bucket']
已从result3中删除
然后它打印索引号和元组列表,如下面的CurrentOutput下所示
我的目标是使用当前输出“重新打包”元组列表并存储在对象中
headers2 = ['Group', 'Owner', 'Person in charge', 'Type of Service',
'Registered Care Categories*', 'Specialist Care Categories',
'Languages Spoken by Staff (other than English)','Single Rooms',
'Shared Rooms','Facilities & Service']
result1 = [['Group', 'MacIntyre'], ['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'], ['Fish', 'Dolphin'], ['Shared Rooms', '4']]
result2 = [['Group', 'Jameseson'], ['Type of Service', 'bad'], ['Shared Rooms', '8']]
result3 = [['Group', 'MacIntyre'], ['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'], ['Shared Rooms', '4'], ['Local Auth', 'bucket']]
results = [result1, result2, result3]
#Removes tuples which the first element in the tuple does not matcch any string in headers2 list
def desired_headers():
for index, z in enumerate(list(range(len(results)))):
for i in list(range(len(results[z]))):
if any(x in headers2 for x in results[z][i]):
print(index, results[z][i])
desired_headers()
电流输出:
0 ['Group', 'MacIntyre']
0 ['Person in charge', ' Vivienne Donald (Manager)']
0 ['Type of Service', 'good']
0 ['Shared Rooms', '4']
1 ['Group', 'Jameseson']
1 ['Type of Service', 'bad']
1 ['Shared Rooms', '8']
2 ['Group', 'MacIntyre']
2 ['Person in charge', ' Vivienne Donald (Manager)']
2 ['Type of Service', 'good']
2 ['Shared Rooms', '4']
期望输出:
[[['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']],
[['Group', 'Jameseson'], ['Type of Service', 'bad'], ['Shared Rooms', '8']],
[['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']]]
你需要这样的东西吗?这将在单个列表中对所有内容进行分组
# Removes tuples which the first element in the tuple does not matcch any string in headers2 list
def desired_headers():
grouped_elements = []
for index, z in enumerate(list(range(len(results)))):
inner_loop = []
for i in list(range(len(results[z]))):
if any(x in headers2 for x in results[z][i]):
inner_loop.append(results[z][i])
grouped_elements.append(inner_loop)
print(grouped_elements)
你需要这样的东西吗?这将在单个列表中对所有内容进行分组
# Removes tuples which the first element in the tuple does not matcch any string in headers2 list
def desired_headers():
grouped_elements = []
for index, z in enumerate(list(range(len(results)))):
inner_loop = []
for i in list(range(len(results[z]))):
if any(x in headers2 for x in results[z][i]):
inner_loop.append(results[z][i])
grouped_elements.append(inner_loop)
print(grouped_elements)
试试这个
lv1 = [] # initialize an empty list which we will append to based on our criteria
for lst in results: # loop though each element of the list, which itself is a list
lv2 = []
for el in lst: # Same as above
if el[0] in headers2: # check if first element of the list exists in headers2
lv2.append(el)
lv1.append(lv2)
lv1
或者如果你想要一个函数
def desired_headers(list_of_lists, inclution_list):
lv1 = []
for lst in list_of_lists:
lv2 = []
for el in lst:
if el[0] in inclution_list:
lv2.append(el)
lv1.append(lv2)
return lv1
desired_headers(list_of_lists=[result1, result2, result3], inclution_list=headers2)
或者如果你熟悉列表的理解
result = []
for lst in [result1, result2, result3]:
result.append([el for el in lst if el[0] in headers2])
result
试试这个
lv1 = [] # initialize an empty list which we will append to based on our criteria
for lst in results: # loop though each element of the list, which itself is a list
lv2 = []
for el in lst: # Same as above
if el[0] in headers2: # check if first element of the list exists in headers2
lv2.append(el)
lv1.append(lv2)
lv1
或者如果你想要一个函数
def desired_headers(list_of_lists, inclution_list):
lv1 = []
for lst in list_of_lists:
lv2 = []
for el in lst:
if el[0] in inclution_list:
lv2.append(el)
lv1.append(lv2)
return lv1
desired_headers(list_of_lists=[result1, result2, result3], inclution_list=headers2)
或者如果你熟悉列表的理解
result = []
for lst in [result1, result2, result3]:
result.append([el for el in lst if el[0] in headers2])
result
您可以通过列表来完成:
filtered = [ [sl for sl in r if sl[0] in headers2] for r in results ]
输出:
print(filtered)
[
[
['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']
],
[
['Group', 'Jameseson'],
['Type of Service', 'bad'],
['Shared Rooms', '8']
],
[
['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']
]
]
如果您有大量数据,您可能需要列出一组
标题2
,用于列表理解您可以使用列表理解:
filtered = [ [sl for sl in r if sl[0] in headers2] for r in results ]
输出:
print(filtered)
[
[
['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']
],
[
['Group', 'Jameseson'],
['Type of Service', 'bad'],
['Shared Rooms', '8']
],
[
['Group', 'MacIntyre'],
['Person in charge', ' Vivienne Donald (Manager)'],
['Type of Service', 'good'],
['Shared Rooms', '4']
]
]
如果你有很多数据,你可能想把
headers2
放在列表理解中在中用作索引,z在枚举中(列表(范围(len(results)))
索引和z值作为旁白,不要使用列表(范围(…)
只是范围(…)
元组是一个没有两个项的列表。您拥有的是一个列表列表。在索引的中,枚举中的z(范围(len(results))
索引和z值与旁白相同,不要使用列表(范围(…)
仅范围(…)
元组是一个不包含两项的列表。你拥有的是一个列表列表。