0

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?

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • 1
    Can you please fix the indentation on this? Also `i += i` will just be `0` always if `i` is 0. Try `i += 1`. – Eli Sadoff Oct 30 '16 at 20:12

4 Answers4

2

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))
Darkstarone
  • 4,590
  • 8
  • 37
  • 74
0

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.

Community
  • 1
  • 1
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
0

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
Arwan Khoiruddin
  • 436
  • 3
  • 13
  • I want to sum up each element in the list Amount, corresponding to the Years List. For example: 1982 300 1983 600 1984 900 1985 1200 – Rude Awakening Oct 31 '16 at 01:49
0

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)
Markacho
  • 292
  • 2
  • 5