【问题标题】:I am trying to do an update for a list of dictionaries in python我正在尝试更新 python 中的字典列表
【发布时间】:2022-01-10 16:29:46
【问题描述】:

我必须更新字典列表,更准确地说:如果值大于 70,则关联的值则应为大写,否则如果小于或等于 70,则应为小写。 我尝试使用 for 循环,但在 list() 中出现 k,v 错误:

TypeError: 'list' object is not callable

这是我目前所拥有的:

list = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"}] 

for k,v in list(): 
    if v > 50: 
        list[k].upper() 
    else:
        list[k].lower() 

print = list()

【问题讨论】:

  • 您好,您能否提供一些示例数据、预期结果以及您已经尝试过的东西?谢谢!
  • 您好,欢迎来到 SO。您表明您正在努力解决您的问题,这对社区很重要。在我看来,最好的方法是包含你目前拥有的基于 text 的源代码版本,即使它运行不正确。
  • list = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand ':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"} ] for k,v in list(): if v > 50: list[k].upper() else: list[k].lower() print = list() * 这是我目前所拥有的 我是新人对此,我并没有完全正确。我非常感谢您的帮助。我希望我以后能很快理解这一点
  • list = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand ':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"} ] for k,v in list(): if v > 50: list[k].upper() else: list[k].lower() print = list()

标签: python list dictionary


【解决方案1】:

从你得到的错误开始,这是因为你需要先迭代列表元素,然后再使用键值对迭代字典项。

现在,如果您的列表是:

my_list = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"}] 

只需运行以下代码:

for my_dict in my_list:
    age_value = int(my_dict["age"])
    brand_value = my_dict["brand"]
    if age_value > 70:
        my_dict["brand"] = brand_value.upper()
    else:
        my_dict["brand"] = brand_value.lower()

你会得到如下结果:

[{'brand': 'nokia', 'age': '56'},
 {'brand': 'MOTOROLA', 'age': '80'},
 {'brand': 'sony', 'age': '42'},
 {'brand': 'allview', 'age': '10'},
 {'brand': 'huawei', 'age': '15'}]

PS - 不建议在您的列表中使用“list”,因为这是一种不好的做法,因为“list”是一种数据类型,您应该将其更改为“my_list”之类的内容。

【讨论】:

  • 非常感谢 :)
  • @Andy 是否解决了您面临的问题?
  • 是的,非常感谢您对此的帮助:) 非常感谢
  • @Andy 乐于助人!如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
【解决方案2】:

你想要什么:

s = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"}] 
[ {'brand': item['brand'].upper() if int(item['age'])>70 else item['brand'], 'AGE' if int(item['age']) > 70 else 'age': item['age']} for item in s]

输出是:

[{'brand': 'nokia', 'age': '56'}, {'brand': 'MOTOROLA', 'AGE': '80'}, {'brand': 'sony', 'age': '42'}, {'brand': 'allview', 'age': '10'}, {'brand': 'huawei', 'age': '15'}]

【讨论】:

  • 这看起来不错,但也许我对我的问题不是很清楚,因为当年龄的值 >70 时我需要“品牌”的输出为大写,当年龄为
  • 当年龄大于 70 岁时将键“品牌”更改为大写?或将“品牌”的值更改为大写?还是两个都改?
  • 只将'brand'的值改为大写
  • 非常感谢您对此提供的帮助
【解决方案3】:

根据您的代码,如果公司年龄超过 70 岁,我制作了以下解决方案,提供大写的“品牌”值。

list = [{'brand':'nokia', "age":"56"}, {'brand':'motorola', "age":"80"}, {'brand':'sony', "age":"42"}, {'brand':'allview', "age":"10"}, {'brand':'huawei', "age":"15"}] 
 
#set the range n for index 0,n to the number of dictionary entries in your list.

for index in range(0,5):
    print(list[index]["age"])
    # if list([index]["age"])>70:
    #     print("old asF")
    int_value = int(list[index]["age"])
  #here we reassign the value of 'brand' to be 'brand'.upper()
    if int_value >70:
    list[index]['brand'] = list[index]['brand'].upper()

        

print(list)

给出以下输出

 [{'brand': 'nokia', 'age': '56'}, {'brand': 'MOTOROLA', 'age': '80'}, {'brand': 'sony', 'age': '42'}, {'brand': 'allview', 'age': '10'}, {'brand': 'huawei', 'age': '15'}]

【讨论】:

  • 谢谢,这看起来不错,但也许我不是很清楚我的问题,因为当年龄的值大于 70 时,我需要“品牌”的输出为大写,而当这些值为 70时'品牌'之类的键值较高,而年龄
  • 嘿,安迪,你用 .upper() 命令做对了。更正以提供解决方案!希望有效
猜你喜欢
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多