【问题标题】:How do i change the value of a specific item in a list with many items?如何更改包含许多项目的列表中特定项目的值?
【发布时间】:2021-10-19 08:43:45
【问题描述】:

我是 python 新手,需要一些帮助。

我如何更改特定项目的价值,(在这种情况下:“一个产品的单价”在包含许多项目/产品的列表/字典中?

products = [
    {
        "discontinued": 0,
        "lead_time_days": 1,
        "product_category": "Toy",
        "product_description": "Basketball Hoop Adjusts to Six Heights",
        "product_id": 152,
        "product_name": "Little Tikes EasyScore Basketball Set",
        "reorder_level": 36,
        "unit_price": 36.99
    },
    {
        "discontinued": 0,
        "lead_time_days": 4,
        "product_category": "Hardcover",
        "product_description": "Weight - 1.476 Depth - 0.00 Width - 0.00 Height - 0.00",
        "product_id": 153,
        "product_name": "The Very Quiet Cricket",
        "reorder_level": 10,
        "unit_price": 23.99
    },
    {
        "discontinued": 0,
        "lead_time_days": 2,
        "product_category": "Automotive",
        "product_description": "Compact 64 piece road assistance kit",
        "product_id": 154,
        "product_name": "AAA 64 Piece Premium Traveler Road Kit",
        "reorder_level": 12,
        **"unit_price": 39.99**
    },

    
     {
        "discontinued": 0,
        "lead_time_days": 3,
        "product_category": "Wireless Phone",
        "product_description": "Display: 5.1-inches Camera",
        "product_id": 155,
        "product_name": "Samsung Galaxy S5, Black 16GB (Sprint)",
        "reorder_level": 18,
        "unit_price": 699.99
    }


    
]


如何更改一个特定的值,即第一个产品的单价?

谢谢。

【问题讨论】:

  • 你怎么知道你要修改哪个元素?
  • products[0]['unit_price']=x...?
  • 假设您正在尝试修改第 i 件商品的单价:products[i][unit_price"] = 31.45

标签: python python-3.x list python-2.7 dictionary


【解决方案1】:

为什么,您可以简单地使用列表索引,然后使用以下方法设置字典的unit_price

products[0]['unit_price']= what_ever_price

【讨论】:

    【解决方案2】:

    您可以使用其索引更改列表中的项目:

    list[index] = value

    记住python中的索引是从0开始的,所以第一个元素的索引是0,第二个是1,第三个是2,以此类推……

    您可以使用其键更改字典中的值:

    dictionary[key] = value

    你的例子:

    products[0]['unit_price'] = new_price

    这里products[0] 返回第一个产品字典,然后您可以更改“unit_price”值。

    【讨论】:

      【解决方案3】:

      要访问listdictionary 中的元素,请使用[] 运算符。 对于list,您可以使用索引访问,对于dict,您可以使用唯一键访问,例如string 因此您可以像这样访问第一个产品的 unit_price 值:

      first_elem_unit_price = products[0]['unit_price']
      

      并像这样修改它:

      products[0]['unit_price'] = new_price
      

      【讨论】:

        【解决方案4】:

        正如一些答案中指出的那样,如果您知道要修改的元素的位置,您只需要这样做:

        products[0]["unit_price"] = new_price  # or any other index 1, 2, 3
        

        如果要根据其他条件修改产品,则使用for循环:

        for product in products:
             if product["id"] == some_id:
                 product["unit_price"] = some_price
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-11-23
          • 1970-01-01
          • 2015-02-24
          • 2011-12-27
          • 1970-01-01
          • 2022-01-11
          • 2014-07-08
          相关资源
          最近更新 更多