Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python解释这一行表达式_Python_Python 3.x - Fatal编程技术网

用python解释这一行表达式

用python解释这一行表达式,python,python-3.x,Python,Python 3.x,我正在学习python,我看到了一行python表达式,但我并不真正理解它。有人能帮我解释一下代码吗?或者在传统的for循环中扩展代码?更具体地说,{fruit:是什么意思 return {fruit: basket.count(fruit) for fruit in set(basket)} 谢谢!您拥有的是一个dict理解。它在一个语句中从一个iterable构建一个字典。它们是在年引入的 因此,对于iterable中的每个元素,我们创建一个dict条目,其中水果作为键,它在列表中出现的次

我正在学习python,我看到了一行python表达式,但我并不真正理解它。有人能帮我解释一下代码吗?或者在传统的for循环中扩展代码?更具体地说,
{fruit:
是什么意思

return {fruit: basket.count(fruit) for fruit in set(basket)}

谢谢!

您拥有的是一个dict理解。它在一个语句中从一个
iterable
构建一个字典。它们是在年引入的

因此,对于
iterable
中的每个元素,我们创建一个
dict
条目,其中水果作为键,它在列表中出现的次数作为值

下面是一个例子。我假设了变量
篮的内容

basket = ['banana', 'apple', 'orange', 'cherry', 'orange', 'banana', 'orange', 'orange']

print( {fruit: basket.count(fruit) for fruit in set(basket)})

# or expanded as regular loop
d = {}
for fruit in set(basket):
    d[fruit] = basket.count(fruit)

print(d)
count
方法不是很有效,尤其是当与集合本身上的另一个循环结合时。更有效的方法是使用
defaultdict

from collections import defaultdict

dd = defaultdict(int)

for fruit in basket:
    dd[fruit] += 1

print(dd)

现在,您只需在集合上迭代一次。而不是一次创建集合,而是在集合本身上迭代一次,在集合中的每个项目的列表上迭代一次。

baset
列表中形成一个字典是一种口述理解,该字典将水果名称作为键,并将该水果的计数作为值。for循环是:

basket = ['banana', 'banana', 'apple', 'banana', 'orange', 'apple']
d = {}
for fruit in set(basket):
    d[fruit] = basket.count(fruit)

# d = {'banana': 3, 'apple': 2, 'orange': 1}

x = {fruit: basket.count(fruit) for fruit in set(basket)}

# x = {'banana': 3, 'apple': 2, 'orange': 1}

代码创建了一个字典,告诉你篮子里每个水果的数量

也许我可以把它分解一下,让它更容易理解。下面的代码相当于您的一行代码:

basket = ["apple", "apple", "pear", "banana"]

# a set is an unsorted list of unique items
uniqueFruits = set(basket)

# initialise empty dict
counts = {}

# loop through every unique fruit
for fruit in uniqueFruits:

    # how often does it come up in the basket?
    occurrences = basket.count(fruit)

    # put that number into the dict with the fruit name as value
    counts[fruit] = occurrences
现在,对象
counts
包含与语句返回的字典相同的字典。在我们的示例中:

{'pear': 1, 'banana': 1, 'apple': 2}

这个表达式称为dict理解(本质上是创建字典的单行表达式)

在Python中,dict如下所示:

dictionary = {"key": "value", "anotherkey": ["some", "other", "value"]}
dictionary["key"]
>> "value"
其中,
的右侧是键(通常是字符串),而
的左侧是分配给键的值

您可以使用字典从如下键获取值:

dictionary = {"key": "value", "anotherkey": ["some", "other", "value"]}
dictionary["key"]
>> "value"
因此,dict理解使用一些表达式构建词典,如示例中的表达式

如果您有这个
篮子

basket = ["apple", "apple", "banana"]
set(basket)  # set() returns a set of unique items from basket.
>> {"apple", "banana"} 
basket.count("apple")  # list.count(x) returns the number of times x occurs in list.
>> 2
这:

与此相同:

return {fruit: basket.count(fruit) for fruit in set(basket)}
#      {^key : ^value}  is added   for each item in set(basket)           
他们都回来了:

{"apple": 2, "banana": 1}
它相当于:

temp_dict = {}
for fruit in set(basket):
    temp_dict[fruit] = basket.count(fruit)
return temp_dict

这是一个dict理解。
fruit:
是为特定元素定义键的部分。另外,
set()
篮子中删除重复项。因此,您可以确保最后的dict将具有唯一键。@ChihebNexus 1)我写的
set()
返回一组唯一的项,2)它不会从
篮子中删除重复项。它返回一组
篮子,其中不能有重复项。如果它从
篮子中删除重复项,那么
篮子将被修改,这是一个重要的区别,因为在python中列表是可变的。是的!我没有看到评论。我感到羞耻:-(我从篮子中删除重复项的意思是拥有一套没有任何重复项的新项目。