Posts: 84
Threads: 36
Joined: Jul 2017
I've defined the third element in each list item as a float, but I want to convert them to currency format:
#!/usr/bin/env python3
#SamsLists.py
movies = [["Monty Python and the Holy Grail", 1975, 9.99],
["Uncle Bazerko's Violent Adventure", 2011, 9.99],
["The Expendables", 2010, 9.99],
["Duke Nukem: Fate of Humanity", 2019, 11.99]]
i = 0
for i in movies:
movies[i][("${:,.2f}".format(movies[2]))]
i += 1
for movie in movies:
print(movie)
print()
print(movies) The error was:
Error: ========= RESTART: I:/Python/Python36-32/SamsPrograms/MovieList2D.py =========
Traceback (most recent call last):
File "I:/Python/Python36-32/SamsPrograms/MovieList2D.py", line 11, in <module>
movies[i][("${:,.2f}".format(movies[2]))]
TypeError: list indices must be integers or slices, not list
>>>
My desired output is:
Output: ['Monty Python and the Holy Grail', 1975, $9.99]
["Uncle Bazerko's Violent Adventure", 2011, $9.99]
['The Expendables', 2010, $9.99]
['Duke Nukem: Fate of Humanity', 2019, $11.99]
What do I need to do?
Posts: 1,150
Threads: 42
Joined: Sep 2016
Mar-09-2018, 11:53 AM
(This post was last modified: Mar-09-2018, 11:53 AM by j.crater.)
movies is a list of lists. So any "first level" indexing of movies will return a list.
movies = [
["Monty Python and the Holy Grail", 1975, 9.99],
["Uncle Bazerko's Violent Adventure", 2011, 9.99],
["The Expendables", 2010, 9.99],
["Duke Nukem: Fate of Humanity", 2019, 11.99]
] And in the for loop:
i = 0
for i in movies:
movies[i][("${:,.2f}".format(movies[2]))]
i += 1 the integer i ("i=0") variable hasn't got much effect, and really you probably shouldn't be using a for loop like that anyway.
In each iteration i will be one of the nested lists from "movies" list, not the integer i.
Other thing is, if you want to change an item in the list, you should assign a new value to it:
movies[i][2] = "formatted movies[i][2]"
Posts: 84
Threads: 36
Joined: Jul 2017
(Mar-09-2018, 11:53 AM)j.crater Wrote: the integer i ("i=0") variable hasn't got much effect, and really you probably shouldn't be using a for loop like that anyway.
In each iteration i will be one of the nested lists from "movies" list, not the integer i. Yeah, i is supposed to be one of the nested lists. Sorry, but I'm not quite sure what you're trying to point out here.
(Mar-09-2018, 11:53 AM)j.crater Wrote: Other thing is, if you want to change an item in the list, you should assign a new value to it:
movies[i][2] = "formatted movies[i][2]" Well, I gave that my best shot:
#!/usr/bin/env python3
#SamsLists.py
movies = [["Monty Python and the Holy Grail", 1975, 9.99],
["Uncle Bazerko's Violent Adventure", 2011, 9.99],
["The Expendables", 2010, 9.99],
["Duke Nukem: Fate of Humanity", 2019, 9.99]]
i = 0
for i in movies:
#movies[i][("${:,.2f}".format(movies[2]))]
#movies["${:,.2f}".format(movies[i][2])]
#movies[i]["${:,.2f}".format([2])]
movies[i][2] = "${:,.2f}".format[i][2]
i += 1
for movie in movies:
print(movie)
print()
print(movies) Error: ========= RESTART: I:/Python/Python36-32/SamsPrograms/MovieList2D.py =========
Traceback (most recent call last):
File "I:/Python/Python36-32/SamsPrograms/MovieList2D.py", line 14, in <module>
movies[i][2] = "${:,.2f}".format[i][2]
TypeError: 'builtin_function_or_method' object is not subscriptable
>>>
Posts: 1,150
Threads: 42
Joined: Sep 2016
Quote:Yeah, i is supposed to be one of the nested lists. Sorry, but I'm not quite sure what you're trying to point out here.
I don't blame you, it was a clumsy description on my part :) i "increments" on each loop iteration automatically, you don't need to assign a value to it, nor incremented it yourself (with i+=1).
You were on the right track, but the formatting function doesn't have the right syntax:
for movie in movies:
movie[2] = "${:,.2f}".format(movie[2]) movie is more descriptive than i. In this case it would be good to change name of "movies" also, since it's not too distinct from "movie". You may use any (valid) name though, "i" will work too.
|