如何从Python词典列表中更改特定值

时间:2020-03-31 05:31:24

标签: python

suppliers = [
    {
        "city": "St. Kellyton",
        "contact_firstname": "Michelle",
        "contact_lastname": "Mitchell",
        "contact_title": "Mrs",
        "country": "Australia",
        "email": "michelle3113.mitchell@gmail.com",
        "notes": "",
        "phone": "(03) 9269 4800",
        "postcode": "2693",
        "state": "Mitchell",
        "street_address": "2 Alexander Grove",
        "supplier_id": 101,
        "supplier_name": "Ferguson Inc"
    },
    {
        "city": "Lake Peterfurt",
        "contact_firstname": "Lucas",
        "contact_lastname": "Reyes",
        "contact_title": "Mr",
        "country": "Australia",
        "email": "lucas7404.reyes@gmail.com",
        "notes": "",
        "phone": "(02) 2310 6339",
        "postcode": "2634",
        "state": "Reyes",
        "street_address": "09 /\n 0 Tara Laneway",
        "supplier_id": 102,
        "supplier_name": "Mata, Townsend and Black"
    },
    {
        "city": "Parkerborough",
        "contact_firstname": "Marcia",
        "contact_lastname": "Stanley",
        "contact_title": "Mrs",
        "country": "Australia",
        "email": "marcia2459.stanley@gmail.com",
        "notes": "",
        "phone": "(07) 2644 5735",
        "postcode": "2619",
        "state": "Stanley",
        "street_address": "Suite 071\n 3 Cody Upper",
        "supplier_id": 103,
        "supplier_name": "White, Vargas and Ballard"
    }
]

我必须将“ supplier_id” = 102的供应商的“ supplier_name”从“ Mata,Townsend and Black”更改为“ Mata,Townsend and Black&Co.”。我是python的新手,不了解如何访问特定值以及如何从上面的代码中更改它。

3 个答案:

答案 0 :(得分:4)

一种方法是:

for dictionay in suppliers:
    suppId = dictionay.get('supplier_id')
    if suppId == 102:
        dictionay['supplier_name'] = 'Mata, Townsend and Black & Co'

答案 1 :(得分:0)

首先,通过supplier_id查找供应商:

supplier = next(x for x in suppliers if x["supplier_id"] == 102)

然后,将该供应商的supply_name更改为所需的供应商:

supplier['supplier_name'] = 'Mata, Townsend and Black & Co.'

答案 2 :(得分:0)

如果suppliers列表很大,您也可以使用pandas

安装方式如下:pip install pandas

In [79]: df = pd.DataFrame(suppliers)
In [81]: df                                                                                                                                                                                                 
Out[81]: 
             city contact_firstname contact_lastname contact_title    country  ... postcode     state            street_address supplier_id              supplier_name
0    St. Kellyton          Michelle         Mitchell           Mrs  Australia  ...     2693  Mitchell         2 Alexander Grove         101               Ferguson Inc
1  Lake Peterfurt             Lucas            Reyes            Mr  Australia  ...     2634     Reyes     09 /\n 0 Tara Laneway         102   Mata, Townsend and Black
2   Parkerborough            Marcia          Stanley           Mrs  Australia  ...     2619   Stanley  Suite 071\n 3 Cody Upper         103  White, Vargas and Ballard

[3 rows x 13 columns]

In [77]: import numpy as np   
In [82]: df['supplier_name'] = np.where(df['supplier_id'] == 102, 'Mata, Townsend and Black & Co.','Mata, Townsend and Black')
In [86]: suppliers = df.to_dict('records')                                                                                                                                                                  

In [87]: suppliers                                                                                                                                                                                          
Out[87]: 
[{'city': 'St. Kellyton',
  'contact_firstname': 'Michelle',
  'contact_lastname': 'Mitchell',
  'contact_title': 'Mrs',
  'country': 'Australia',
  'email': 'michelle3113.mitchell@gmail.com',
  'notes': '',
  'phone': '(03) 9269 4800',
  'postcode': '2693',
  'state': 'Mitchell',
  'street_address': '2 Alexander Grove',
  'supplier_id': 101,
  'supplier_name': 'Mata, Townsend and Black'},
 {'city': 'Lake Peterfurt',
  'contact_firstname': 'Lucas',
  'contact_lastname': 'Reyes',
  'contact_title': 'Mr',
  'country': 'Australia',
  'email': 'lucas7404.reyes@gmail.com',
  'notes': '',
  'phone': '(02) 2310 6339',
  'postcode': '2634',
  'state': 'Reyes',
  'street_address': '09 /\n 0 Tara Laneway',
  'supplier_id': 102,
  'supplier_name': 'Mata, Townsend and Black & Co.'},
 {'city': 'Parkerborough',
  'contact_firstname': 'Marcia',
  'contact_lastname': 'Stanley',
  'contact_title': 'Mrs',
  'country': 'Australia',
  'email': 'marcia2459.stanley@gmail.com',
  'notes': '',
  'phone': '(07) 2644 5735',
  'postcode': '2619',
  'state': 'Stanley',
  'street_address': 'Suite 071\n 3 Cody Upper',
  'supplier_id': 103,
  'supplier_name': 'Mata, Townsend and Black'}]