簡體   English   中英

從列表列表中創建一個列表,對 enumerate 方法創建的每個列表使用相同的重復索引號

[英]Making a list from a list of lists, using the same repeated index number for each list created by the enumerate method

我的 function desired_headers() 從“結果” object 中刪除元組,其中元組中的第一個元素與標題列表中的任何字符串都不匹配。 ['Fish', 'Dolphin'] from result1 object & ['Local Auth', 'bucket']已從 result3 中刪除。

然后它打印索引號和元組列表,如下面的當前 output 所示。

我的目標是使用我當前的 output 將元組列表“重新打包”並存儲在 object 中。

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()

當前 Output:

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']

所需的 Output:

[[['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)

嘗試這個

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

或者如果你想要一個 function


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 ]

output:

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來制作一組用於列表理解

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM