如何在Python 3中返回多个值,例如print?

时间:2018-11-25 11:10:50

标签: python python-3.5

例如,如果我使用打印,它会给我101238157,而没有。

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

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

4 个答案:

答案 0 :(得分:0)

您可以收集所有值并一次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

答案 1 :(得分:0)

有多种方法可以执行,但是以下是使用range和for循环的更简单方法。数据将是您的输出的列表。您也可以尝试

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

答案 2 :(得分:0)

将所有值添加到一个变量中并返回它。

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

data = get_my_value()

答案 3 :(得分:0)

return只能有一个值(可以是列表或其他对象)...为什么?仅仅因为return是承担函数的值。目前,您正在分配一个函数,例如

def champs()
     return MJ KD LBJ

champ = champs()

这样,数字应同时为MJ,KD和LBJ ...从概念上讲是不可能的。但是我们可以返回列表!


首先使用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)

现在您可以将其以函子形式返回:

    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

这是一种更加Python化的方式

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

我希望这是您需要做的!