简体   繁体   English

我有一本包含值列表的字典。 我想从值列表中取最高值

[英]I have a dictionary with lists of values. I want to take the highest value from the list of values

suppose I have a dictionary假设我有一本字典

y = {'town1': ['moderate', 'low'], 'town2': ['high'], 'town3': ['moderate', 'severe']} y = {'town1':['中等','低'],'town2':['高'],'town3':['中等','严重']}

I want the dictionary to only have one value rather than a list and take the highest risk value.我希望字典只有一个值而不是一个列表,并采用最高风险值。 So for town 1 highest risk is moderate, for town 2 it's high and for town 3 its severe The order of risk is ( 'severe','high','moderate', low)因此,对于城镇 1 的最高风险是中等的,对于城镇 2 的风险很高,对于城镇 3 的风险是严重的。风险的顺序是('严重','高','中等',低)

desired output: y = {'town1': ['moderate'], 'town2': ['high'], 'town3': ['severe']}所需的 output: y = {'town1': ['moderate'], 'town2': ['high'], 'town3': ['severe']}

My code is below:我的代码如下:

x = []
for k,v in y.items():
    x.append((k,v))

print(x)
for key, value in y.items():
    for i in range(len(x)):
        if 'severe' in x[i][1]:
            y[key] = 'severe'
        elif 'high' in x[i][1]:
            y[key] = ['high']
        elif 'moderate' in x[i][1]:
            y[key] = ['moderate']
        elif 'low' in x[i][1]:
            y[key] = ['low']

print(y)

You don't have to iterate explicitly over the severity lists, the in operator is doing this for you:您不必显式地遍历严重性列表, in运算符正在为您执行此操作:

x = {'town1': ['moderate', 'low'], 'town2': ['high'], 'town3': ['moderate', 'severe']}
print(x)

y = {}
for key, values in x.items():
    if 'severe' in values:
        y[key] = ['severe']
    elif 'high' in values:
        y[key] = ['high']
    elif 'moderate' in values:
        y[key] = ['moderate']
    elif 'low' in values:
        y[key] = ['low']

print(y)

暂无
暂无

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

相关问题 我有两个列表,我想从中创建一个字典。 但是我想为一个键存储多个值 - I have two list and I want to create a dictionary from it . But I want to store multiple values for one key 我有一个包含不同值列表的 json 文件。 我想根据值给出不同颜色的不同值范围。 我怎样才能做到这一点? - I have a json file with list of different values. I want to give different ranges of values different color on based on the value. How can I do that? 我有两个列表,一个用于x值,一个用于y值,我想从这些列表中创建一个列表 - I have two lists one for x values and one for y values, I want to make a list from these 我有一个包含 boolean 个值的列表列表。 如何输入 boolean function 并在列表的每个子列表中检查它? - I have a list of lists containing boolean values. How can I input a boolean function and check it in every sublist of my list? pandas数据框中的列具有作为值的列表。 如何创建此列的版本,但列表中只有第一个值? - Column in pandas dataframe has lists as values. How do I create a version of this column but with only the first value in the list? 如果我在字典中有值列表,可以在这些值列表上执行条件zip吗? - if I have lists for the values in a dictionary, can I perform a conditional zip on these value lists? 我已经使用列表作为每个键的值制作了一个字典,我想打印没有方括号的值 - I have made a dictionary using a list as the values for each key, I want to print the values without square brackets 我有一个字典值列表,我想要列表的第一个索引。 如何遍历它? - I have a list in values of a dictionary and I want the first index of the list. How to iterate through it? 我在 pandas dataframe 列中有字典作为值。 我想将键列和值作为列值 - I have dictionary as value in pandas dataframe columns. I want to make the keys columns and values as column value Python - 我有两个不同的列表。 我想找到与 list1 中的 value:2 相对应的负值的百分比(来自 list2)? - Python - I have two different list. I want to find the percentage of negative values(from list2) that corresponds to value:2 in list1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM