Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能让一本词典生成两个键相同的词典_Python_Python 3.x_List_Dictionary - Fatal编程技术网

Python 我怎样才能让一本词典生成两个键相同的词典

Python 我怎样才能让一本词典生成两个键相同的词典,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,我有一本字典,什么 what = {'a': ['b'], 'c': ['d\n', 'e\n', 'f'], 'd': ['f', 'g']} 并且需要将带有“/n”的项目分开,不带“/n”(顺序很重要,需要对值进行排序。): 这就是我所尝试的: def lst(profiles_file: TextIO): solve_lst = [] new_lst = fix_files(profiles_file) for k, v in ne

我有一本字典,什么

what = {'a': ['b'], 'c': ['d\n', 'e\n', 'f'], 'd': ['f', 'g']}
并且需要将带有“/n”的项目分开,不带“/n”(顺序很重要,需要对值进行排序。):

这就是我所尝试的:

    def lst(profiles_file: TextIO):
        solve_lst = []
        new_lst = fix_files(profiles_file)
        for k, v in new_lst.items():
            for i in v:
                if i.find('\n') != -1:
                get_index = [v.index(i)]
                solve_lst.append(get_index)
     return solve_lst
我怎样才能在不做任何复杂的事情的情况下得到它呢?

这里有一个使用

    def lst(profiles_file: TextIO):
        solve_lst = []
        new_lst = fix_files(profiles_file)
        for k, v in new_lst.items():
            for i in v:
                if i.find('\n') != -1:
                get_index = [v.index(i)]
                solve_lst.append(get_index)
     return solve_lst
from collections import defaultdict

def lst(profiles_file: TextIO):
    initial_dict = fix_files(profiles_file)

    with_n = defaultdict(list)
    without_n = defaultdict(list)

    for k, v in initial_dict.items():
        for item in v:
            if '\n' in item:
                with_n[k].append(item)
            else:
                without_n[k].append(item)

    return [without_n, with_n]