Python 如果键列表中的所有元素都存在,则检索字典中的键

Python 如果键列表中的所有元素都存在,则检索字典中的键,python,python-3.x,Python,Python 3.x,我有很多带有特定字符串的文件,这些文件必须根据这些字符串分组到单独的文件中 我有一个必须存在于文件中的字符串列表,当所有字符串都存在于同一个文件中时,它们将被提取并包含在第二个文件中。 我最初的想法是将文本转换为dictionary,然后,如果列表中的所有元素都存在于dictionary中,则提取它们并创建、添加到新文件中 list_str = ["Ana", "Beatrice", "Mike"] File1= {"Ana

我有很多带有特定字符串的文件,这些文件必须根据这些字符串分组到单独的文件中

我有一个必须存在于文件中的字符串列表,当所有字符串都存在于同一个文件中时,它们将被提取并包含在第二个文件中。 我最初的想法是将文本转换为dictionary,然后,如果列表中的所有元素都存在于dictionary中,则提取它们并创建、添加到新文件中

list_str = ["Ana", "Beatrice", "Mike"]

File1= {"Ana":["red", "lamp"], "Beatrice":["blue", "notebook"], "Mike":["green", "t-shirt"]}
File2= {"Ana":["big", "car"], "Beatrice":["ugly","bike"], "Mike":["plastic", "boat"]}
File3= {"Beatrice":["fried","egg"], "Mike":["toasted","bread"]}
File4= {"Ana":["new","phone"], "Beatrice":["black","computer"]}
File5= {"Ana":["black","pen"], "Beatrice":["white","glue"], "Mike":["blue","pencil"]}
有了这些信息,我的预期结果是:

new_files = {"Ana": [ ["red", "lamp"],["big", "car"],["black","pen"]], "Beatrice": [["blue", "notebook"],  ["ugly","bike"], ["white","glue"] ], "Mike":[["green", "t-shirt"],["plastic", "boat"], ["blue","pencil"]   ] }
之后,我将转换字典中的新文件,使用它们的键作为文件名,并将它们在每个列表中的内容作为一行

这样做的顺序与文件1、文件2、…、文件n的处理顺序相同

Ana.txt
>red lamp
>big car
>black pen
有什么建议吗?
同时,我将继续尝试。

您需要以某种方式使文件列表可编辑(我已将它们放在
文件列表中),但我将通过以下内容获得您的预期输出:

names = ["Ana", "Beatrice", "Mike"]

File1= {"Ana":["red", "lamp"], "Beatrice":["blue", "notebook"], "Mike":["green", "t-shirt"]}
File2= {"Ana":["big", "car"], "Beatrice":["ugly","bike"], "Mike":["plastic", "boat"]}
File3= {"Beatrice":["fried","egg"], "Mike":["toasted","bread"]}
File4= {"Ana":["new","phone"], "Beatrice":["black","computer"]}
File5= {"Ana":["black","pen"], "Beatrice":["white","glue"], "Mike":["blue","pencil"]}

files = [File1, File2, File3, File4, File5]

new_files = {name: [] for name in names}
for name in names:
    for file in files:
        if all(name in file.keys() for name in names):
            new_files[name].append(file[name])

print(new_files)

考虑使用

from collections import defaultdict

list_str = ["Ana", "Beatrice", "Mike"]

file1 = {"Ana":["red", "lamp"], "Beatrice":["blue", "notebook"], "Mike":["green", "t-shirt"]}
file2 = {"Ana":["big", "car"], "Beatrice":["ugly","bike"], "Mike":["plastic", "boat"]}
file3 = {"Beatrice":["fried","egg"], "Mike":["toasted","bread"]}
file4 = {"Ana":["new","phone"], "Beatrice":["black","computer"]}
file5 = {"Ana":["black","pen"], "Beatrice":["white","glue"], "Mike":["blue","pencil"]}

files = [file1, file2, file3, file4, file5]

new_files = defaultdict(list)
for f in files:
    if all(first in f for first in list_str):
        for first in list_str:
            new_files[first].append(f[first])

new_files = dict(new_files)
print(new_files)

for file_name, objects in new_files.items():
    with open(f'{file_name}.txt', 'w') as file_handle:
        for thing_color, thing in objects:
            file_handle.write(f'{thing_color} {thing}\n')
输出:

{'Ana': [['red', 'lamp'], ['big', 'car'], ['black', 'pen']], 'Beatrice': [['blue', 'notebook'], ['ugly', 'bike'], ['white', 'glue']], 'Mike': [['green', 't-shirt'], ['plastic', 'boat'], ['blue', 'pencil']]}
Ana.txt

red lamp
big car
black pen

我按照你的要求保留了文件结构

list_str = ["Ana", "Beatrice", "Mike"]

File1= {"Ana":["red", "lamp"], "Beatrice":["blue", "notebook"], "Mike":["green", "t-shirt"]}
File2= {"Ana":["big", "car"], "Beatrice":["ugly","bike"], "Mike":["plastic", "boat"]}
File3= {"Beatrice":["fried","egg"], "Mike":["toasted","bread"]}
File4= {"Ana":["new","phone"], "Beatrice":["black","computer"]}
File5= {"Ana":["black","pen"], "Beatrice":["white","glue"], "Mike":["blue","pencil"]}

new_files = {s:[] for s in list_str}
for i in range(1, 6):
    f = eval(f'File{i}')
    for k in list_str:
        if k in f:
            new_files[k].append(f[k])

# new_files == {'Ana': [['red', 'lamp'], ['big', 'car'], ['new', 'phone'], ['black', 'pen']], 'Beatrice': [['blue', 'notebook'], ['ugly', 'bike'], ['fried', 'egg'], ['black', 'computer'], ['white', 'glue']], 'Mike': [['green', 't-shirt'], ['plastic', 'boat'], ['toasted', 'bread'], ['blue', 'pencil']]}

## and know we should store them in files
for k, v in new_files.items():
    with open(f'{k}.txt', 'a') as f:
        f.write('\n'.join(map(lambda x: '> ' + ' '.join(x), v)))
输出的文件之一:


关闭,但是
Ana.txt
不应该有
新手机
,因为
File4
没有
Mike的条目