Frage

Can someone explain help me understand how the this bit of code works? Particularly how the myHeap assignment works. I know the freq variable is assigned as a dictionary. But what about my 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()]
War es hilfreich?

Lösung

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

The above code creates a dictionary called '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)

freq = exe_Data)

I think the above bit should read 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, ""]])

Andere Tipps

freq is a reference to the dictionary, as you said.

myHeap is constructed using a list comprehension, and so it is a list. 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 "".

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.

I assume you meant

freq = exe_Data

In this case, myHeap will look like this:

[ [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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top