栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 返回字典的值

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 返回字典的值

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.

例如,使用元组来存储有关一个人的信息:其姓名,年龄和位置。

nancy = ("nancy", 55, "chicago")

Here’s how you’d write a function that returns a tuple.

这是您编写返回元组的函数的方式。

def person():
   return "bob", 32, "boston"
 
print(person())
# result: ('bob', 32, 'boston')

Notice that we didn’t use parentheses in the return statement. That’s because you can return a tuple by separating each item with a comma, as shown in the above example.

注意,我们在return语句中没有使用括号。 这是因为您可以通过用逗号分隔每个项目来返回元组,如上面的示例所示。

“It is actually the comma which makes a tuple, not the parentheses,” the documentation points out. However, parentheses are required with empty tuples or to avoid confusion.

文档指出:“实际上,逗号是元组,而不是括号。” 但是,在空元组必须加上括号否则会造成混淆。

Here’s an example of a function that uses parentheses () to return a tuple.

这是一个使用括号()返回元组的函数示例。

def person(name, age):
   return (name, age)
print(person("henry", 5))
#result: ('henry', 5)

清单 (List)

A list is an ordered, mutable sequence. That means, a list can change.

列表是有序的可变序列。 这意味着列表可以更改。

You can use a list to store cities:

您可以使用列表来存储城市:

cities = ["Boston", "Chicago", "Jacksonville"]

Or test scores:

或考试成绩:

test_scores = [55, 99, 100, 68, 85, 78]

Take a look at the function below. It returns a list that contains ten numbers.

看一下下面的功能。 它返回一个包含十个数字的列表。

def ten_numbers():
   numbers = []
   for i in range(1, 11):
       numbers.append(i)
   return numbers
 
print(ten_numbers())
#result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Here’s another example. This time we pass in several arguments when we call the function.

这是另一个例子。 这次我们在调用函数时传递了几个参数。

def miles_ran(week_1, week_2, week_3, week_4):
   return [week_1, week_2, week_3, week_4]
 
monthly_mileage = miles_ran(25, 30, 28, 40)
print(monthly_mileage)
#result: [25, 30, 28, 40]

It’s easy to confuse tuples and lists. After all, both are containers that store objects. However, remember these key differences:

混淆元组和列表很容易。 毕竟,它们都是存储对象的容器。 但是,请记住以下主要差异:

  • Tuples can’t change.

    元组不能改变。
  • Lists can change.

    列表可以更改。

辞典 (Dictionaries)

A dictionary contains key-value pairs wrapped in curly brackets {}. Each “key” has a related “value.”  

字典包含用大括号{}括起来的键/值对。 每个“键”都有一个相关的“值”。

Consider the dictionary of employees below. Each employee name is a “key” and their position is the “value.”

考虑下面的员工词典。 每个员工的名字都是一个“关键”,他们的职位是“价值”。

employees = {
   "jack": "engineer",
   "mary": "manager",
   "henry": "writer",
}

Here’s how you’d write a function that returns a dictionary with a key, value pair.

这是您编写返回带有键,值对的字典的函数的方式。

def city_country(city, country):
   location = {}
   location[city] = country
   return location
 
favorite_location = city_country("Boston", "United States")
print(favorite_location)
# result: {'Boston': 'United States'}

In the above example, “Boston” is the key and “United States” is the value.  

在上面的示例中,“波士顿”是键 ,“美国”是值 。

We’ve covered a lot of ground. The key point is this: you can return multiple values from a Python function, and there are several ways to do so.

我们已经涵盖了很多基础。 关键是:您可以从Python函数返回多个值,并且有多种方法可以这样做。

I write about the programming skills you need to develop and the concepts you need to learn, and the best ways to learn them at amymhaddad.com.

我在amymhaddad.com上介绍了您需要开发的编程技能和需要学习的概念,以及学习它们的最佳方法。

翻译自: https://www.freecodecamp.org/news/python-returns-multiple-values-how-to-return-a-tuple-list-dictionary/

python 返回字典的值

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/834082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号