简体   繁体   English

如何在字典列表中拆分字符串值,以便拆分字符串的每个部分都在其自己的字典中?

[英]How to split string values across a list of dictionaries, so that each part of the split string is in it's own dictionary?

How to split string values across a list of dictionaries, so that each part of the split string is in it's own dictionary? 如何在字典列表中拆分字符串值,以便拆分字符串的每个部分都在其自己的字典中?

For example: 例如:

lst = [{item:'321,121,343','name':'abby','amount':'400'},{item:'111,222,333','name':'chris','amount':'500'}]

I'd like each part of the item key-value pair to be in its own dictionary 我希望项目键值对的每个部分都在其自己的字典中

Desired_List = [{item:'321','name':'abby','amount':'400'},{item:'121','name':'abby','amount':'400'},{item:'343','name':'abby','amount':'400'},{item:'111','name':'chris','amount':'500'},{item:'222','name':'chris','amount':'500'},{item:'333','name':'chris','amount':'500'}]

I've tried this with no luck: 我没有运气尝试过这个:

[li.update({li['item']:spl}) for li in lst for spl in li['item'].split(',')]
return Li
def unpack_dict(d, field, unpack):
    packed = d.pop(field)
    for item in unpack(packed):
        result = d.copy()
        result[field] = item
        yield result

lst = [
    {'item':'321,121,343','name':'abby','amount':'400'},
    {'item':'111,222,333','name':'chris','amount':'500'}
]

new_lst = [
    ud
    for d in lst
    for ud in unpack_dict(d, "item", lambda item: item.split(","))
]

gives new_lst = 给出new_lst =

[
    {'amount': '400', 'item': '321', 'name': 'abby'},
    {'amount': '400', 'item': '121', 'name': 'abby'},
    {'amount': '400', 'item': '343', 'name': 'abby'},
    {'amount': '500', 'item': '111', 'name': 'chris'},
    {'amount': '500', 'item': '222', 'name': 'chris'},
    {'amount': '500', 'item': '333', 'name': 'chris'}
]

Here is a working script: 这是一个工作脚本:

lst = [{'item':'321,121,343','name':'abby','amount':'400'}, {'item':'321,121,343','name':'chris','amount':'500'}]

new_list = [{'item':j, 'name': i['name'], 'amount': i['amount']} for i in lst for j in i['item'].split(',')]

>>> print new_list
[{item:'321','name':'abby','amount':'400'},{item:'121','name':'abby','amount':'400'},{item:'343','name':'abby','amount':'400'},{item:'111','name':'chris','amount':'500'},{item:'222','name':'chris','amount':'500'},{item:'333','name':'chris','amount':'500'}]

Demo: http://repl.it/RaE 演示: http//repl.it/RaE

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

相关问题 如何在每个子列表的索引[0]处拆分字符串,并将每个拆分索引放在它自己的原始列表中? - How to split string at index[0] of each sublist, and have each split index in it's own original list? 使用.split()到字典列表的字符串 - String to list of dictionaries using .split() 如何在字典列表中拆分字典字符串值以添加新的字典项? - How do you split a dictionary string value in a list of dictionaries to add a new dictionary item? 将字典拆分成词典列表 - Split a dictionary into a list of dictionaries 将字典内的列表值拆分为单独的字典 - Split list values inside dictionary to separate dictionaries 如何将具有任意数量值的列表字典拆分为字典列表? - How to split a dictionary of lists with arbitrary number of values into a list of dictionaries? 如何拆分属于 python 中的元组列表的字符串 - How to split string that is a part of a tuple list in python 拆分列表字符串并创建字典 - Split List string and create dictionary 如何将字典列表转换为列表值中每个字典的列表? - How to convert a list of dictionaries into a list of each dictionary in the list's values? 如何拆分字符串,以便在不使用正则表达式的情况下将符号制成自己的列表项? - How do I split a string so that symbols are made into their own list item without the use of regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM