简体   繁体   English

无法弄清楚如何在Python函数中修改列表项?

[英]Can't figure out how to modify list items in function in Python?

When I call show_magicians, their names are printed, but I want, when I call make_great, have their name + the Great added. 当我打电话给show_magicians时,会打印他们的名字,但是当我打电话给make_great时,我想给他们加上名字+伟大。

def make_great(magician_names):
    """Adds the phrase 'the Great' to each magician's name."""
    for name in magician_names:
        # I want to add 'the Great' to each item in the list
        # magician_names so that when I call show_magicians, 
        # the items in the list will have 'the Great' added 
        # to them.

def show_magicians(magician_names):
    """Print the names of magicians in a list."""
    for name in magician_names:
        print(name.title())

magician_names = ['peter', 'maria', 'joshua']
show_magicians(magician_names)
make_great(magician_names)
show_magicians(magician_names)

Note the for name in magician_names won't allow you to change the list value of the name as strings in Python can't be changed in place, you must replace them with a new value. 请注意magician_names中的for名称将不允许您更改名称的列表值,因为无法在Python中更改字符串,必须将其替换为新值。 You'll have to edit the list directly by using magician_names[0]... etc. Here I have returned a new list with the changed names, which is the prefered way to deal with lists passed to methods. 您必须使用magician_names [0] ...等直接编辑列表。在这里,我返回了一个具有更改名称的新列表,这是处理传递给方法的列表的首选方法。

def make_great(magician_names):
    """Adds the phrase 'the Great' to each magician's name."""
    return [ name + ' the Great' for name in magician_names]


def show_magicians(magician_names):
    """Print the names of magicians in a list."""
    for name in magician_names:
        print(name.title())

magician_names = ['peter', 'maria', 'joshua']
show_magicians(magician_names)
magician_names = make_great(magician_names)
show_magicians(magician_names)

Here is a method that changes the list directly: 这是一种直接更改列表的方法:

def make_great(magician_names):
    """Adds the phrase 'the Great' to each magician's name."""
    for index in range(len(magician_names)):
        magician_names[index] += ' the Great'
#!/usr/bin/python

def make_great(magician_names):
        """Adds the phrase 'the Great' to each magician's name."""
        for name in magician_names:
                print "{} the Great".format(name)

def show_magicians(magician_names):
        """Print the names of magicians in a list."""
        for name in magician_names:
                print(name.title())

magician_names = ['peter', 'maria', 'joshua']
show_magicians(magician_names)
make_great(magician_names)
show_magicians(magician_names)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚如何在python中为列表中的元素分配一个数字 - Can't figure out how to assign a number to a element in a list in python 无法弄清楚如何使用 Python SDK 修改 Azure Loadbalancer BackEndPool - Can't figure out how to modify Azure Loadbalancer BackEndPool with Python SDK 无法弄清楚如何修复“列表索引超出范围” - Can't figure out how to fix "list index out of range" Python,for循环问题和List无法弄清楚 - Python, issue with for loop and List can't figure it out selenium和python的新手,无法弄清楚如何从列表中调用项目 - New to selenium and python, can't figure out how to call an item from a list 无法弄清楚如何在此Python代码中重新分配列表中的元素 - Can't figure out how to reassign an element in a list of lists in this Python code Python 不断为列表抛出“TypeError”错误,我不知道如何修复它 - Python keeps throwing a "TypeError" error for a list, and I can't figure out how to fix it 我不知道如何在数据库上执行SUM函数并将值存储到python变量中 - I can't figure out how to perform the SUM function on a DB and store the value into a python variable 无法弄清楚如何在另一个函数中使用一个函数中的变量 - Python - Can't figure out how to use a variable from one function in another - Python 无法弄清楚如何增加列表中的某些值 - Can't figure out how to increment certain values in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM