简单的python快速帮助(学习列表)

时间:2013-04-16 22:56:13

标签: python

我正在学习如何使用循环并正在执行此程序。但是,我唯一遇到的困难是正确打印列表每次输入一次。这是我的代码:

country_list=[]
emission_list=[]
capita_list=[]
total=0
end_loop=False
together=[]

while True:
    country=raw_input("Enter country or (done to end): ")
    if country=="done":
        break
    else:
        emission=input("Enter emissions: ")
        capita=input("Enter capita: ")

        country_list.append(country)
        emission_list.append(emission)
        capita_list.append(capita)
        total+=1

for x in range(len(country_list)):
    for y in range(len(emission_list)):
        for z in range(len(capita_list)):
            end_loop=True
            print country_list[x]+": Emission="+str(emission_list[y]),"and Capita="+str(capita_list[z])           

我正在输入:

Enter country or (done to end): Testcase
Enter emissions: 1
Enter capita: 2
Enter country or (done to end): Testcase2
Enter emissions: 3
Enter capita: 4
Enter country or (done to end): done   

这是我得到的输出:

Testcase: Emission=1 and Capita=2
Testcase: Emission=1 and Capita=4
Testcase: Emission=3 and Capita=2
Testcase: Emission=3 and Capita=4
Testcase2: Emission=1 and Capita=2
Testcase2: Emission=1 and Capita=4
Testcase2: Emission=3 and Capita=2
Testcase2: Emission=3 and Capita=4          

它正在打印所有可能的排放和人均组合,但我只想打印我输入的每个国家的排放量/人均排放量。任何人都知道如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

这可以作为学习练习,但执行此任务的Pythonic方法是使用zip

for cnty, em, cap in zip(country_list, emission_list, capita_list):
    print "{}: Emission={} and Capita={}".format(cnty, em, cap)

实际上你应该存储结果(三元组的列表)而不是你以后压缩的三个列表。

cecs = []

while True:
    country  = raw_input("Enter country (or nothing to end): ").strip()
    if not country:
        break
    emission = input("Enter emissions: ").strip()
    capita   = input("Enter capita: ").strip()

    cecs.append((country, emission, capita))

# total = len(cecs)

for cec in cecs:
    print "{}: Emission={} and Capita={}".format(*cec)

答案 1 :(得分:2)

而不是这样做:

for x in range(len(country_list)):
    for y in range(len(emission_list)):
        for z in range(len(capita_list)):
            print country_list[x]+": Emission="+str(emission_list[y]),"and Capita="+str(capita_list[z])

这样做:

for x in range(len(country_list)):
    print country_list[x]+": Emission="+str(emission_list[x]),"and Capita="+str(capita_list[x])

注意:如果您创建的课程具有一个国家/地区名称,一个排放和一个人均,则您的代码将更有意义。然后像这样的错误是不可能的,因为你只有一个对象列表可以迭代,无论如何。

答案 2 :(得分:1)

我宁愿使用字典。它看起来像这样:

countries = {}
while True:
    inp = input("Enter country or (done to end): ")
    if inp == 'done':
        break;
    countries[inp] = {'emission': input("Enter emissions: ")}
    countries[inp]['capita']   = input("Enter capita: ")

for i in countries:
    print(i, 'Emission:', countries[i]['emission'],
          'and Capita', countries[i]['capita'])

输入:

Enter country or (done to end): test
Enter emissions: 1
Enter capita: 2
Enter country or (done to end): test2
Enter emissions: 3
Enter capita: 4
Enter country or (done to end): done

输出继电器:

test Emission: 1 and Capita 2
test2 Emission: 3 and Capita 4

最后字典看起来像这样:

{'test': {'emission': '1', 'capita': '2'}, 'test2': {'emission': '3', 'capita': '4'}}