【问题标题】:How to split values inside python dictionary?如何在python字典中拆分值?
【发布时间】:2019-04-22 10:38:39
【问题描述】:

我有 dict 的值以列表的形式存储。 这是它的样子:

d = [
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=prince,initial=p'},
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=steven,initial=s'}
]

在这里,在键 Length 中,我有两个值。 first=steven,initial=s 我想拆分这些值并创建两个新字段并将其以更新的形式存储在字典中。

所需输出:

d = [
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=prince,initial=p','first':'prince','initial':'p'},
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=steven,initial=s','first':'steven','initial':'s'}
]

这是我尝试过的程序:

d = [
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=prince,initial=p'},
{'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=steven,initial=s'}
]
field_to_split = "Length"
split_using1 = ','
split_using2 = '='
b =[]
for i in d:
    s = i[field_to_split].split(split_using1)
    print(s)
    b.append(s)
    #print(s)
print(b)

我怎样才能做到这一点...

【问题讨论】:

    标签: python python-3.x list dictionary for-loop


    【解决方案1】:

    使用简单的迭代。

    例如:

    d = [
        {'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=prince,initial=p'},
        {'Driveline': 'Rear-wheel drive', 'Hybrid': 'False', 'Classification': 'Automatic,Transmission', 'Number_of_Forward_Gears': 6, 'Fuel_Type': 'Gasoline', 'Length': 'first=steven,initial=s'}
        ]
    
    for i in d:
        for j in i['Length'].split(","):    #Split string by comma
            i.update(dict([j.split("=")]))  #Split string by eq sign and use dict() method to create a dictionary 
    

    输出:

    [{'Classification': 'Automatic,Transmission',
      'Driveline': 'Rear-wheel drive',
      'Fuel_Type': 'Gasoline',
      'Hybrid': 'False',
      'Length': 'first=prince,initial=p',
      'Number_of_Forward_Gears': 6,
      'first': 'prince',
      'initial': 'p'},
     {'Classification': 'Automatic,Transmission',
      'Driveline': 'Rear-wheel drive',
      'Fuel_Type': 'Gasoline',
      'Hybrid': 'False',
      'Length': 'first=steven,initial=s',
      'Number_of_Forward_Gears': 6,
      'first': 'steven',
      'initial': 's'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2018-10-04
      • 2022-01-15
      • 2013-05-22
      相关资源
      最近更新 更多