表格显示代码显示错误的列

时间:2017-12-14 23:55:32

标签: python

我写了一个程序,显示哪些国家被归类为人类发展的“非常高”,“高”,“中”或“低”。国家将分类如下:

  1. “非常高”,0.75 < HDI得分
  2. “high”.........,0.51&lt; HDI&lt; 0.75
  3. “medium”......,0.26&lt; HDI&lt; 0.5
  4. “low”...........,............ HDI&lt; 0.26
  5. 结果如下:

    Country  HDI score
    Japan 0.903
    Finland 0.895
    Qatar 0.856
    Lebanon 0.763
    Armenia 0.743
    Iraq 0.649
    Pakistan 0.550
    Djibouti 0.473
    Cameroon 0.518
    

    我用这段代码写道:

    Country=   ["Japan","Finland","Qatar","Lebanon","Armenia","Iraq","Pakistan",
                "Djibouti","Cameroon"]
    Country[0]=0.903
    Country[1]=0.895
    Country[2]=0.856
    Country[3]=0.763
    Country[4]=0.743
    Country[5]=0.649
    Country[6]=0.550
    Country[7]=0.473
    Country[8]=0.518
    for Country in Country:
        if Country>0.76:
            print [Country, "Very High"]
        elif 0.76>Country>0.51:
            print [Country,"High"]
        elif 0.51>Country>0.26:
            print [Country,"Medium"]
        elif Country < 0.26:
            print [Country,"Low"]
    

    但该代码的问题是,它显示HDI和等级而不是国家的名称和等级如下:

    [0.903, 'Very High']
    [0.895, 'Very High']
    [0.856, 'Very High']
    [0.763, 'Very High']
    [0.743, 'High']
    [0.649, 'High']
    [0.55, 'High']
    [0.473, 'Medium']
    [0.518, 'High']
    

    我不知道我在这里缺少什么,为什么打印HDI而不是国家的名字等同于它,尽管我首先介绍了这些国家的名称。

3 个答案:

答案 0 :(得分:1)

这里似乎有一些问题......

你使用for Country in Country ...声明......(对我来说很混乱)。无论如何,使用国家作为listname:

Countries = ["Japan","Finland","Qatar","Lebanon","Armenia","Iraq","Pakistan",
             "Djibouti","Cameroon"]

Countries[0]=0.903  # overriding in Countries the 'Japan' Country name with the value '0.903'
... snippet...

for Country in Countries:
    ...snippet...

你也差不多混合名单和词汇。让我们坚持列表+元组。

尝试:

Countries = [('Japan', 0.903), (country, value), etc...]

然后你得到:

for country in Countries:
    my_country_name  = country[0]
    my_country_value = country[1]

    if my_country_value >0.76:
        print [my_country_name, "Very High"]
    elif ... :
        print ...
    etc..
    ..snippet...

享受!

答案 1 :(得分:1)

您正在使用list来定义国家/地区及其HDI,但是当您输入HDI值时,您正在修改列表,而不是将值附加到每个国家/地区。

让我们快速了解一下您的一些早期步骤:

Country = ["Japan",
           "Finland", 
           "Qatar",
           "Lebanon",
           "Armenia",
           "Iraq",
           "Pakistan",
           "Djibouti",
           "Cameroon"]

如果我定义它然后在Python shell中打印出来:

> print Country
['Japan', 'Finland', 'Qatar', 'Lebanon', 'Armenia', 'Iraq', 'Pakistan', 'Djibouti', 'Cameroon']

一切看起来都不错。

但是如果我运行你的下一行代码:

> Country[0]=0.903 

让我们再打印出来:

> print Country
[0.903, 'Finland', 'Qatar', 'Lebanon', 'Armenia', 'Iraq', 'Pakistan', 'Djibouti', 'Cameroon']

我们刚刚将Japan替换为0.903,因为您在Country时替换了Country[0]列表中的原始值。

因此,您真正想要的是将每个国家与其各自的HDI值相关联的内容。实现此目的的一种方法是使用dictionary,将某事物与其他事物联系起来。

使用字典的基本语法是:

countries = { 'Japan': 0.903,
              'Finland': 0.895,
              # etc
            }

然后你可以按键迭代它,并使用这些值。这是你的代码适合使用字典,并使用其键和值迭代字典。

countries = {
    'Japan': 0.903,
    'Finland': 0.895,
    'Qatar': 0.856,
    'Lebanon': 0.763,
    'Armenia': 0.743,
    'Iraq': 0.649,
    'Pakistan': 0.550,
    'Djibouti': 0.473,
    'Cameroon': 0.518
}

for country, hdi in countries.iteritems():
    if hdi > 0.76:
        print [country, "Very High"]
    elif 0.76 > hdi > 0.51:
        print [country,"High"]
    elif 0.51 > hdi > 0.26:
        print [country,"Medium"]
    elif hdi < 0.26:
        print [country,"Low"]

输出如下:

['Pakistan', 'High']
['Qatar', 'Very High']
['Iraq', 'High']
['Armenia', 'High']
['Japan', 'Very High']
['Finland', 'Very High']
['Lebanon', 'Very High']
['Djibouti', 'Medium']
['Cameroon', 'High']

您会注意到订购与原始列表不同。普通字典不保持插入顺序。

请注意我做的一些风格:

  • 我使用小写名称作为变量
  • 我不会像使用Country
  • 那样使用相同的变量来引用不同的内容
  • 数学比较中的间距更多

答案 2 :(得分:0)

或...

country = [
    ("japan", 0.903),
    ("Finland", 0.895),
    ("Qatar", 0.856),
    ("Lebanon", 0.763),
    ("Armenia", 0.743),
    ("Iraq", 0.649),
    ("Pakistan", 0.550),
    ("Djibouti", 0.473),
    ("Cameroon", 0.518)
]

v1 = ''
for c, v in country:
    if v < 0.26:
        v1 = "very low"
    elif v > 0.26 and v < 0.51:
        v1 = "low"
    elif v > 0.51 and v < 0.76:
        v1 = "High"
    else:
        v1 = "Very High"
    print("{:10} {:6} {:10}".format(c, v, v1))
  

输出

japan       0.903 Very High 
Finland     0.895 Very High 
Qatar       0.856 Very High 
Lebanon     0.763 Very High 
Armenia     0.743 High      
Iraq        0.649 High      
Pakistan     0.55 High      
Djibouti    0.473 low       
Cameroon    0.518 High