Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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,我的名单如下: my_list = ["10", "12", "32", "23"] 以及一本字典: my_dict = { 'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}, 'two': {'index': 0, 'sec_key': 'CD', 'id': '12'}, 'three': {'index': 0, 'sec_key':

我的名单如下:

my_list = ["10", "12", "32", "23"]
以及一本字典:

my_dict = {
    'one': {'index': 0, 'sec_key': 'AB', 'id': '10'},
    'two': {'index': 0, 'sec_key': 'CD', 'id': '12'}, 
    'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}
    }
我想保留一本字典,上面写着final_dict,只有当id出现在my_列表中时,它才会包含my_dict的内容。我试图通过以下方式做到这一点:

sf_dict = dict()

for list_id in my_list:
    for key, value in my_dict.items():
        if list_id == value['id']:
            print("Values : {} {} {}".format(value['index'], value['sec_key'], value['id']))
            sf_dict[key] = key
            sf_dict[key]['index'] = value['index']  
            sf_dict[key]['sec_key'] = value['sec_key']
            sf_dict[key]['id'] = value['id']
        
print(sf_dict)
我可以打印这些值,但由于以下错误,这些值的分配失败:

TypeError: 'str' object does not support item assignment

我哪里出错了?

您可以使用字典理解来循环您的
my_dict
,并检查
id
中存储的值是否在您的列表中

my_list = ["10", "12", "32", "23"]
my_dict = {
    'one': {'index': 0, 'sec_key': 'AB', 'id': '10'},
    'two': {'index': 0, 'sec_key': 'CD', 'id': '12'}, 
    'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}
    }

result = {key:value for key, value in my_dict.items() if value['id'] in my_list}

#{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}, 'two': {'index': 0, 'sec_key': 'CD', 'id': '12'}, 'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}}

my_list = ["10"]

#{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}}
这里有一个解决方案:

final_dict={}
for i in my_dict:
    if my_dict[i]['id'] in my_list:
        final_dict[i]=my_dict[i]
例如:

my_list = ["10", "32", "23"]
输出:

print(final_dict)
{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}, 'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}}
代码应该是这样的。

\p>问题就在这里

sf_dict[key] = key
例如,上面的行表示sf_dict={'one':'one'}

所以下一行的意思是“一”[“任何东西”]= 因此,不允许使用索引分配到str中


上面已经回答了更好的解决方案,只是指出了现有代码中的问题,已经给出的答案应该可以解决这个问题,尽管了解您的方法不正确的原因可能很重要

您所做的操作的问题是,当您执行
sf_dict[key]=key
时,您正在使用值
key
设置key
key
的字典值。但是
key
是一个字符串,因此,当您稍后尝试
sf_dict[key]['index']=value['index']
时,它会告诉您不能在字符串顶部赋值

试着去掉这些线

sf_dict[key]['index'] = value['index']  
sf_dict[key]['sec_key'] = value['sec_key']
sf_dict[key]['id'] = value['id']

只需将它们替换为
sf_dict[key]=value
。这样,当您找到要搜索的id时,您可以将所有字典一次分配到相应的键。

您应该编写sf_dict[key]=value而不是sf_dict[key]=key,其余不需要。谢谢您指出错误Ajeet。是的,我的错。我意识到有很多技巧要做,听写理解。。。
sf_dict[key]['index'] = value['index']  
sf_dict[key]['sec_key'] = value['sec_key']
sf_dict[key]['id'] = value['id']