我怎样才能 return 像在 Python 3 中打印的多个值?

How can I return multiple values like print in Python 3?

例如,如果我使用打印,它会给我 101 238 157 和 None。

i = 0
while i < 3:
  champion = matchList['matches'][i]['champion']
  i = i + 1
  print(champion)

但是如果我使用 RETURN 它只会 returns 101。 那我该怎么办?

您可以收集所有值并一次 return 它们,或者 yield each value one after the other:

# initial match list
matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

def all_at_once():
    result = []
    for match in matchList['matches']:
        result.append(match['champion'])
    return result

def one_after_another():
    for match in matchList['matches']:
        yield match['champion']

这两个都提供了一个可迭代的 - 你可以在 for 循环中使用它们,将它们传递给 list 或者解构它们,例如:

for item in one_after_another():
    print(item)

print(*all_at_once())

first, second, third, *rest = one_after_another()
print(first, second, third)

由于您的转换直接从一种形式映射到另一种形式,因此您也可以在 comprehension form 中表达这两种形式:

all_at_once = [match['champion'] for match in matchList['matches']]
one_after_another = (match['champion'] for match in matchList['matches'])

虽然两者都提供可迭代对象,但两者并不等同。 return 意味着您预先构建整个列表,而 yield 延迟计算每个值。

def print_all_at_once():
    result = []
    for i in range(3):
        print(i)
        result.append(i)
    return result

def print_one_after_another():
    for i in range(3):
        print(i)
        yield i

# prints 0, 1, 2, 0, 1, 2
for item in print_all_at_once():
    print(item)

# print 0, 0, 1, 1, 2, 2
for item in print_one_after_another():
    print(item)

当您return一个列表时,您还可以重用它的内容。相反,当你 yield 每个值时,它在使用后就消失了:

returned = print_all_at_once()  # already prints as list is built
print('returned', *returned)  # prints all values
print('returned', *returned)  # still prints all values

yielded = print_one_after_another()  # no print as nothing consumed yet
print('yielded', *yielded)  # prints all values and value generation
print('yielded', *yielded)  # prints no values

有多种方法可以做到,但以下是使用范围和 for 循环的更简单方法。数据将是您的输出列表。你也可以试试 yeild

data=[matchList['matches'][i]['champion'] for i in range(3)]

将所有值添加到一个变量并return它。

def get_my_value():
    values = []
    for i in range(3):
        champion = matchList['matches'][i]['champion']
        values.append(champion)
    return values

data = get_my_value()

return 只能有一个值,(可以是一个对象,如列表或其他东西)...为什么?正因为return是承担函数的值。在你做一个函数赋值的那一刻,例如

def champs()
     return MJ KD LBJ

champ = champs()

这样一来,这个数字应该同时是MJ、KD和LBJ……概念上是不可能的。但是我们可以return一个列表!


首先使用for循环,更紧凑可读,做同样的事情:

for i in range(3):
    champion = matchList['matches'][i]['champion']
    print(champion)

现在使用冠军列表:

champions = []
for i in range(3):
    champion = matchList['matches'][i]['champion']
    champions.append(champion)
    print (champion)

更紧凑的方式:

champions = []
for i in range(3):
    champions.append(matchList['matches'][i]['champion'])
    print(champions)

现在您可以 return 它在函数中:

    def getChamp(matchList):
        champions = []
        for i in range(3):
            champions.append(matchList['matches'][i]['champion'])
        return champions

也许您想使 for 循环更具动态性:

def getChamp(matchList):
        champions = []
        for match in range(len(matchList['matches'])):
            champions.append(matchList['matches'][match]['champion'])
        return champions

这是一种更pythonic的方式

def getChamp(matchList):
        for match in matchList['matches']:
            yield match['champion']
        yield None

我希望这是你需要做的!