python 返回字典的值
You can return multiple values from a function in Python.
您可以从Python中的函数返回多个值。
To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week.
为此,返回一个包含多个值的数据结构,例如包含每周运行里程数的列表。
def miles_to_run(minimum_miles):
week_1 = minimum_miles + 2
week_2 = minimum_miles + 4
week_3 = minimum_miles + 6
return [week_1, week_2, week_3]
print(miles_to_run(2))
# result: [4, 6, 8]
Data structures in Python are used to store collections of data, which can be returned from functions. In this article, we’ll explore how to return multiple values from these data structures: tuples, lists, and dictionaries.
Python中的数据结构用于存储数据集合,这些数据可以从函数中返回。 在本文中,我们将探讨如何从这些数据结构中返回多个值:元组,列表和字典。
元组 (Tuples)
A tuple is an ordered, immutable sequence. That means, a tuple can’t change.
元组是有序的,不变的序列。 这意味着,元组不能改变。
Use a tuple, for example, to store information about a person: their name, age, and location.
例如,使用元组来存储有关一个人的信息:其姓名,年龄和位置。