I am trying to associate a value in what I think is a 2d list (ie. (1,3),(2,1),(0,1); which correspond to variables essentially: 'item number' and 'quantity' respectively). I also have another list but it is 1d with values (1.25, 3.75, 1.70, 2.30, 1.10). So I am trying to associate the item number values in the 2d list with the values in the 1d list (so 0 for 1.25, 1 for 3.75, etc). In short, I was wondering if there was a way that could be accomplished. I would imagine I would have to use nested for loops to iterate through the values. The end goal for the program is to print a total from a provided .txt file (holds the 1d list values) and a .csv file (holds the 2d list values). Thank you!
Asked
Active
Viewed 47 times
-1
-
I don't see the relationship. How does `0` correspond to `1.25`? – Barmar Oct 01 '20 at 23:57
-
It seems to me that this is simply using the integer value as an index into the 1D list. Where are you stuck? – Prune Oct 01 '20 at 23:58
-
@Barmar Essentially '0' corresponds to the position of '1.25' in the 1d list. So 0 corresponds to 1.25, 1 is 3.75, 2 is 1.70. If that makes sense – AGut07 Oct 01 '20 at 23:58
-
But you said you're associating them with item numbers in the 2D list. So is it `item 1 => price 1.25`, `item 2 => price 3.75`, `item 0 => price 2.30`? – Barmar Oct 01 '20 at 23:59
-
`for (itemno, quantity), price in zip(list2d, list1d):` – Barmar Oct 02 '20 at 00:02
-
@Prune Maybe I am overthinking but I am just confused on how I would go about assigning the item number to the natural positions of the prices in the 1d list – AGut07 Oct 02 '20 at 00:36
-
Again, it appears that your item number is already the list position. – Prune Oct 02 '20 at 00:37
-
@Barmar the for statement is close to what I need but instead I want to match the item number '0' to the first price in the 1d list instead of just assigning them as they are entered (ex: (1,3) -> 1.25 as shown by my IDLE output) – AGut07 Oct 02 '20 at 00:38
-
What IDLE output? – Barmar Oct 02 '20 at 00:39
-
`for itemno, quantity in list2d: price = list1d[itemno]` – Barmar Oct 02 '20 at 00:40
-
@Prune My output using the for loop @Barmar suggested gives me something like `(1,3) 1.25` instead I want `(1,3) 3.75` so that I can use it for calculations. Hopefully that makes sense – AGut07 Oct 02 '20 at 00:42
-
@Barmar Second form of the for loop seemed to give me more for what I am looking for, so thank you. Now I will just need to figure out how I can also consider the quantity value to calculate a total price. – AGut07 Oct 02 '20 at 00:45
-
Quickly figured out how to use the quantity value parts of the 2d list. Thank you guys for the help! – AGut07 Oct 02 '20 at 00:47
1 Answers
1
Use the item number as an index into the price list. Multiply the price by the quantity, and use sum()
to calculate the total.
order_items = [(1,3),(2,1),(0,1)]
prices = [1.25, 3.75, 1.70, 2.30, 1.10]
total_price = sum(prices[itemnum] * quantity for itemnum, quantity in order_items)

Barmar
- 741,623
- 53
- 500
- 612