'I want to add some words after every item and tried two ways. But the first one doesn't produce the expected result, which part is wrong?
unshowed_magicians = ['tom','jim','green']
showed_magicians = []
def make_great(unshowed_magicians):
"""make magicians great again"""
great_magicians=[]
while unshowed_magicians:
magician = unshowed_magicians.pop()
great_magician =magician+' the Great'
great_magicians.append(great_magician)
unshowed_magicians = great_magicians
print(unshowed_magicians)
make_great(unshowed_magicians)
print(unshowed_magicians)
result: ['green the Great', 'jim the Great', 'tom the Great'] []
unshowed_magicians = ['tom','jim','green']
showed_magicians = []
def make_great(unshowed_magicians):
"""make magicians great again"""
great_magicians=[]
while unshowed_magicians:
magician = unshowed_magicians.pop()
great_magician =magician+' the Great'
great_magicians.append(great_magician)
for great_magician in great_magicians:
unshowed_magicians.append(great_magician)
print(unshowed_magicians)
make_great(unshowed_magicians)
print(unshowed_magicians)
result: ['green the Great', 'jim the Great', 'tom the Great'] ['green the Great', 'jim the Great', 'tom the Great']
Solution 1:[1]
I'm not sure what exactly you're trying to do with the showed_magicians list, but most likely what is happening is that the variable name inside the function is shadowing the list from the outer scope (above the function), so it's not accessing the list from outside the function. Consider using a different variable name in your function. If you want to append a word to every word in the list, you could simply use List Comprehension. In your case, it would be something like this:
unshowed_magicians = ['tom', 'jim', 'green']
showed_magicians = []
def make_great(magicians):
return [name + ' the Great' for name in magicians]
showed_magicians = make_great(unshowed_magicians)
print(unshowed_magicians)
If you want it reversed like how you did it in the code snippet (since you popped the elements like a stack), you could do it like this:
Edit: @kcsquared gave a much better suggestion, in which the original list is reversed inside of the list comprehension instead of reversing the whole result so that a list is returned instead of a list_reverseiterator object like so:
unshowed_magicians = ['tom', 'jim', 'green']
showed_magicians = []
def make_great(magicians):
return [name + ' the Great' for name in reversed(magicians)]
showed_magicians = make_great(unshowed_magicians)
print(unshowed_magicians)
Solution 2:[2]
The difference is in the scope. unshowed_magicians variable inside the function has a local scope, and is different from the global unshowed_magicians defined outside the function.
BTW Instead of trying to modify the global variables from within the function, you could instead return the result and assign it to the global.
def make_great(unshowed_magicians):
...
return great_magicians
showed_magicians = make_great(unshowed_magicians)
Solution 3:[3]
In the first example you assign unshowed_magicians to a list that exists only in the function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | hundredrab |
| Solution 3 | Kilian |
