我正在从一本书中自学Python,我在这2部分练习的第2部分遇到了麻烦。
练习的第一部分:列出魔术师的名字。将列表传递给一个名为show_magicians()的函数,该函数将打印列表中每个魔术师的名称。
我没问题地完成了这部分。
我的第一部分代码:
magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
def show_magicians():
for name in magicians_names:
print(name)
show_magicians()练习的第二部分:从练习8-9中的程序副本开始。编写一个名为make_great()的函数,通过在每个魔术师的名字中添加“伟大”这个短语来修改魔术师的列表。调用show_magicians()以查看列表实际上已被修改。
我的第2部分代码
magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
def show_magicians():
for name in magicians_names:
print(name)
def make_great():
show_magicians()我已经为make_great函数尝试了几乎所有我能想到的想法,但是到目前为止还没有什么效果。任何想法或例子都将不胜感激。
发布于 2016-03-01 01:17:20
列表中的每一项都有一个关联索引,从零开始。正如您可能知道的,您可以使用以下索引访问列表中的项:
>>> magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
>>> magicians_names[0]
'Marv'还可以使用以下索引修改列表项:
>>> magicians_names[0] = 'Jerry Boomfang'
>>> magicians_names[0]
'Jerry Boomfang'因此,您需要做的是循环遍历列表及其索引,并在执行过程中进行修改。这正是枚举函数的作用所在。
>>> for index, magician in enumerate(magicians_names):
... magicians_names[index] += ' is great!'
...
>>> magicians_names
['Jerry Boomfang is great!', 'Wowzo is great!', 'Trickster is great!', 'Didlo is great!']发布于 2016-08-10 06:03:20
希望这能有所帮助。[:]通过创建要在被调用函数中使用的原始列表的副本或切片来保留原始列表。本书的练习是第150页Python速成班上的8-9到8-11。
def meta_mags(show_mags, great_mags):
"""(Change regular show magicians to Great magicians by moving them
to another list using a function meta_mags)"""
while show_mags:
change_mags = show_mags.pop()
# show the change from one list show_mags to great_mags
print("Great magicians: " + change_mags.title())
great_mags.append(change_mags)
def show_great_mags(great_mags):
"""Print --The Great--- after each great_mags magicians name"""
for great_mag in great_mags:
print(great_mag.title() +" The Great will be performing tonight !")
show_mags = ['alice', 'david', 'carolina']
great_mags = []
meta_mags(show_mags[:], great_mags)
show_great_mags(great_mags)
print(show_mags)发布于 2016-08-10 08:12:08
TL;博士
使用range和len的最简单(虽然不是很漂亮)解决方案
def make_great():
for i in range(len(magicians_names)):
magicians_names[i] = 'The Great ' + magicians_names[i] + ' !'实际答案
对此有多种解决方案,我发现有两种解决方案非常简单:
range长度相同的magicians_names。- You can access element number `i` in a list `lst` with `lst[i]`
- `range(n)` generates a list : `[0, 1, 2, ..., n-1]`
- [`len`](http://www.tutorialspoint.com/python/list_len.htm) is a function that gives the length of a list (or the length of many things, but what we're interested in are lists)
这意味着:
def make_great():
# len(magician_names) is 4, and range(4) is [0, 1, 2, 3]
for i in range(len(magicians_names)):
# update the content of the list like this
magicians_names[i] = 'The Great ' + magicians_names[i] + ' !'enumerate函数,它将为列表中的每个元素提供像(index, element)这样的元组。就像这样:
def make_great():
# enumerate(magician_names) is [(0, 'Marv'), (1, 'Wowzo'), ...]
for i, name in enumerate(magicians_names):
# update the content of the list like this
magicians_names[i] = 'The Great ' + name + ' !'第二种方式被认为更优雅一些,尽管两者都有效。要测试它,首先运行make_great(),修改列表。然后运行show_magicians()
>>> make_great()
>>> show_magicians()
The Great Marv !
The Great Wowzo !
The Great Trickster !
The Great Didlo !附加信息
这实际上修改了列表magicians_names,将来您可能不希望这样做。您可以在列表的副本上运行该函数。要创建列表的副本,可以使用[:],这意味着来自。
copy_of_mn = magicians_names # does not copy : if you modify one, you modify the other
copy_of_mn = magicians_names[:] # does copy : the two are the sames, but independent如果您想获得这个列表,您可以这样做(创建一个副本并对其进行修改),或者使用append实现一个空列表。
def make_great():
# create an empty list
result = []
# we don't even need 'enumerate' anymore
for name in magicians_names:
# 'this.append(that)' means 'add that at the end of this'
result.append('The Great ' + name + ' !')
# return the list (if you omit this line, the function will return 'None')
return result使用此解决方案,函数返回一个包含所有修改名称的列表:['The Great Marv !', 'The Great Wowzo !', 'The Great Trickster !', 'The Great Didlo !']
一个小的修改show_magicians以后,你可以打印他们!
def show_magicians(list_of_names):
for name in list_of_names:
print(name)然后只需调用show_names(make_great(magicians_names)),因为make_great(magicians_names)将创建所有修改名称的列表,show_names(something)将打印something的所有元素。
您还可能希望能够与其他列表以及要添加的其他内容一起重用您的方法。我们的想法是为您的函数使用参数,正如在最后一段代码中介绍的:
def add_prefix_and_suffix(list_of_names, prefix, suffix):
for i, name in enumerate(list_of_names):
# alter the list_of_names, adding prefix and suffix to the name
list_of_names[i] = prefix + name + suffix然后您可以简单地使用add_prefix_and_suffix(magicians_names, 'The Great ', ' !')而不是make_great。更重要的是,您可以从这个函数定义make_great!
初学者方式:
def make_great():
add_prefix_and_suffix(magicians_names, 'The Great ', ' !')兰卜达方式:
make_great = lambda: add_prefix_and_suffix(magicians_names, 'The Great ', ' !')从你的练习中可以做很多事情,但我认为现在已经足够了。我提供了一些链接作为例子,但是如果你愿意的话,你可以在互联网上找到很多其他的链接。
https://stackoverflow.com/questions/35712891
复制相似问题