简体   繁体   中英

Element addition

Good Morning,

I built two lists below:

Years = [1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985]
Amount = [100, 200, 300, 400, 100, 200, 300, 400, 100, 200, 300, 400]

price = 0
for item in Years:
    i = 0
    while Years[i] <= item:
    price += Amount[i] 
    i += i
print(item,price)

How do I make this print so that it will only print years and corresponding total amount?

It should print: 1982 300

did i miss something here?

Personally I'd do it using a dictionary structure, and by using zip to iterate both lists simultaneously:

years = [1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985]
amount = [100, 200, 300, 400, 100, 200, 300, 400, 100, 200, 300, 400]

results = {}
for y, a in zip(years,amount):
    if y in results:
        results[y] += a
    else:
        results[y] = a

for year, total in results.items():
    print(str(year) + ": " + str(total))

That way you can easily access each year and it's amount by going results[year] to get the corresponding amount.

Also I renamed Years and Amounts to years and amounts because it's convention to use lowercase first letters on variables in Python.

To avoid the test to see if a key is in the results dictionary (the if statement), you could also use a defaultdict structure:

import collections

years = [1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985]
amount = [100, 200, 300, 400, 100, 200, 300, 400, 100, 200, 300, 400]

results = collections.defaultdict(int)
for y, a in zip(years,amount):
    results[y] += (a)

for year, total in results.items():
    print(str(year) + ": " + str(total))

You can use enumerate to get the index of the list you are iterating over.

for ind, item in enumerate(Years):
    print(item, sum(Amount[:ind+1]))

The sum function takes a list a returns its sum. To get the prices up to the current year, you can use list splicing to access the relevant list items.

I hope I understand you correctly. Here I create a code that should be easier to understand

Years = [1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985]
Amount = [100, 200, 300, 400, 100, 200, 300, 400, 100, 200, 300, 400]

price = 0
for i in range(len(Years)):
    print(str(Years[i]) + ' ' + str(Amount[i]))

This will give you the following output:

1982 100
1983 200
1984 300
1985 400
1982 100
1983 200
1984 300
1985 400
1982 100
1983 200
1984 300
1985 400

You can use the zip function:

Years = [1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985, 1982, 1983, 1984, 1985]
Amount = [100, 200, 300, 400, 100, 200, 300, 400, 100, 200, 300, 400]
for b in zip(Years, Amount):
    print(b[0], b[1])

An even more condensed (elegant?) way to use the zip function is:

for a,b in zip(Years, Amount):
    print(a, b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM