简体   繁体   English

python /设置/字典/初始化

[英]python / sets / dictionary / initialization

Can someone explain help me understand how the this bit of code works? 有人可以帮我解释一下这段代码是如何工作的吗? Particularly how the myHeap assignment works. 特别是myHeap分配的工作方式。 I know the freq variable is assigned as a dictionary. 我知道freq变量被分配为字典。 But what about my myHeap? 但是我的myHeap呢? is it a Set? 是套装吗?

    exe_Data = {
      'e' : 0.124167,
      't' : 0.0969225,
      'a' : 0.0820011,
      'i' : 0.0768052,
     }

    freq = exe_Data)

    myHeap = [[pct, [symbol, ""]] for symbol, pct in freq.items()]

freq is a reference to the dictionary, as you said. 正如您所说, freq是对字典的引用。

myHeap is constructed using a list comprehension , and so it is a list. myHeap是使用列表 myHeap构建的,因此它是一个列表。 The general form of a list comprehension is: 列表理解的一般形式为:

[ expr for x in iterable ]

So myHeap will be a list, each element of which is a list with the first element being the value of the corresponding dictionary entry, and the second element being another list whose first element is the corresponding key of the dictionary, and whose second element is "" . 因此, myHeap将是一个列表,其中的每个元素都是一个列表,第一个元素是相应字典条目的值,第二个元素是另一个列表,其第一个元素是字典的对应键,而第二个元素是""

There are no sets in your given code sample. 给定的代码示例中没有集合。

You can see this working like so (I edited the number output for readability): 您可以看到这样工作(我编辑了数字输出以提高可读性):

>>> [ symbol for symbol, pct in freq.items() ]
['a', 'i', 'e', 't']
>>> from pprint import pprint  # Yay, pretty printing
>>> pprint([ [pct, symbol] for symbol, pct in freq.items() ])
[[0.0820011, 'a'],
 [0.0768052, 'i'],
 [0.1241670, 'e'],
 [0.0969225, 't']]
>>> pprint([ [pct, [symbol, ""]] for symbol, pct in freq.items() ])
[[0.0820011, ['a', '']],
 [0.0768052, ['i', '']],
 [0.1241670, ['e', '']],
 [0.0969225, ['t', '']]]

Note that, since dictionaries in Python don't preserve the order of their elements, there's no guarantee what order the freq elements will end up being in in myHeap . 请注意,由于Python中的字典不保留其元素的顺序,因此无法保证freq元素最终位于myHeap中的myHeap

I assume you meant 我想你是说

freq = exe_Data

In this case, myHeap will look like this: 在这种情况下,myHeap将如下所示:

[ [0.124167, ['e', ""]],
  [0.0969225, ['t', ""]],
  [0.0820011, ['a', ""]],
  [0.0768052, ['i', ""]]
]

Note that the order here is arbitrary, but I wanted to write it plainly so you can see what you have in the end results. 请注意,这里的顺序是任意的,但是我想写得很清楚,这样您就可以看到最终结果。

Basically it just changes the order of your key/value of your dictionary, and puts the key in a sub-array for some reason. 基本上,它只是更改键/字典值的顺序,并且出于某种原因将键放在子数组中。

exe_Data = {
  'e' : 0.124167,
  't' : 0.0969225,
  'a' : 0.0820011,
  'i' : 0.0768052,
 }

The above code creates a dictionary called 'exe_Data'. 上面的代码创建了一个名为“ exe_Data”的字典。 Another way to do this is to use the built-in constructor, dict() with keyword arguments as follows: exe_Data = dict(e=0.12467, t=0.0969225, a=0.0820011, i=0.0768052) 执行此操作的另一种方法是将内置构造函数dict()与关键字参数一起使用,如下所示: exe_Data = dict(e=0.12467, t=0.0969225, a=0.0820011, i=0.0768052)

freq = exe_Data)

I think the above bit should read freq=exe_Data . 我认为上面的位应该读为freq=exe_Data It makes another reference to the dictionary created in the previous bit. 它再次引用在前一位中创建的字典。

myHeap = [[pct, [symbol, ""]] for symbol, pct in freq.items()]

This last part creates a list using a list comprehension. 最后一部分使用列表理解来创建列表。 It creates a list of lists of two things, The first thing is a key from the dictionary created and referenced above, and the second thing is a list containing the corresponding value from the dictionary and a blank string. 它创建一个包含两件事的列表的列表,第一件事是上面创建和引用的字典中的键,第二件事是包含字典中的对应值和空字符串的列表。

EDIT: In answer to the comment, it would be the equivalent of writing: 编辑:在评论的答案,这相当于写作:

myHeap = []
for key, val in freq.items():
    myHeap.append([key, [val, ""]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM