简体   繁体   English

我有一个值列表,我想将 append 键值对迭代到 python 字典

[英]i've a list of values and I would like to iteratively append key-value pair to a python dictionary

i've a list of values and I would like to iteratively append key-value pair to a python dictionary我有一个值列表,我想将 append 键值对迭代到 python 字典

lst = ['12233223','23232423','23453443'] lst = ['12233223','23232423','23453443']

i'm looking output like this:我正在寻找 output 像这样:

new_list = [{'id':'12233223', 'new_id':'d2233223'},{'id':'13232423','new_id':'d3232423'}] new_list = [{'id':'12233223', 'new_id':'d2233223'},{'id':'13232423','new_id':'d3232423'}]

but I'm getting this但我明白了

[{'id':13453443,'new_id':13453443},{'id':13453443,'new_id':13453443}] [{'id':13453443,'new_id':13453443},{'id':13453443,'new_id':13453443}]

Here is my code这是我的代码

lst = [{'id':'12233223'},{'id':'13232423',} {'id':'23453443'}]
d={}
new_list=[]
for each in lst:
    d['id']= each
    if len(each)==8 and each[0]==1:
    new_id = each.replace('1','d',1)
    d['new_id'] = new_id
    new_list.append(d)

same value is added to the list for two times相同的值被添加到列表中两次

You can't have a dict with all keys bearing the same value.你不能有一个所有键都具有相同值的字典。 But you can have a list of dicts instead但是你可以有一个字典列表

new_list = [{'id': each} for each in lst]

You can use a list comprehension to go through your list of dictionaries with a nested for to get to the values.您可以通过嵌套for的字典列表使用对 go 的列表理解来获取值。 Then apply the filter condition and build new dictionaries from the eligible dictionaries/values:然后应用过滤条件并从符合条件的字典/值构建新字典:

lst = [{'id':'12233223'},{'id':'13232423'}, {'id':'23453443'}]


new_list = [ {**each,'new_id':"d"+n[1:]} for each in lst 
             for n in each.values() if len(n)==8 and n[0]=='1' ]

print(new_list)
              
[{'id': '12233223', 'new_id': 'd2233223'}, 
 {'id': '13232423', 'new_id': 'd3232423'}]

If the dictionaries in lst have other keys, you can make the nested for more specific to obtain just the value of the 'id' keys:如果lst中的字典有其他键,则可以使嵌套for具体以仅获取“id”键的值:

new_list = [ {**each,'new_id':"d"+n[1:]} for each in lst 
             for n in [each['id']] if len(n)==8 and n[0]=='1' ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何匹配键,然后将键值对值的值与另一个字典中的值相乘? - Python: How do I match keys then multiply the values of a key-value pair value with values from another dictionary? 如何将现有列表转换为 Python 中字典的键值对? - How to turn an existing list into a key-value pair for a dictionary in Python? 如果键值对不存在,如何在不执行 IF 和 ELSE 的情况下自动初始化 Python 字典中的键值? - How can I auto-initialise key-value in python dictionary if key-value pair does not exist, without doing IF & ELSE? 如何在条件语句中使用嵌套在列表中的字典键值对的值? - How do I use the value of a key-value pair of a dictionary nested inside a list in a conditional statement? 如何将 python 字典的每个键值对存储在 json 文件中的单独行上? - How can I store each key-value pair of my python dictionary on a separate line in json file? 如何检查字典中是否存在键值对? 如果一个键的值是字典 - How do I check if a key-value pair is present in a dictionary? If the value of one key is a dictionary 如何在Python中打印截断的键/值对? - How do I print a truncated key-value pair in Python? 根据来自单独列表的匹配项,使用来自字典列表中的值的键值对创建一个新字典 - Create a new dictionary with the key-value pair from values in a list of dictionaries based on matches from a separate list 字典列表 Python:如果现有的键、值对匹配,则将新的键、值对附加到字典中 - List of Dictionaries Python: Append a new key, value pair to dictionary if existing key, value pair matches 如何从基于另一个键值对的列表中的字典中访问值? - How to access values from a dictionary inside a list based on another key-value pair?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM