简体   繁体   English

从字典列表中获取键值

[英]Getting key values from a list of dictionaries

I have a list that contains dictionaries with Letters and Frequencies. 我有一个包含字母和频率字典的列表。 Basically, I have 53 dictionaries each for every alphabet (lowercase and uppercase) and space. 基本上,我每个字母表(小写和大写)和空格都有53个字典。

adict = {'Letter':'a', 'Frequency':0}
bdict = {'Letter':'b', 'Frequency':0}
cdict = {'Letter':'c', 'Frequency':0}

If you input a word, it will scan the word and update the frequency for its corresponding letter. 如果您输入一个单词,它将扫描该单词并更新其相应字母的频率。

for ex in range(0, len(temp)):
    if temp[count] == 'a': adict['Frequency']+=1
    elif temp[count] == 'b': bdict['Frequency']+=1
    elif temp[count] == 'c': cdict['Frequency']+=1

For example, I enter the word "Hello", The letters H,e,l,l,o is detected and its frequencies updated. 例如,我输入单词“Hello”,检测字母H,e,l,l,o并更新其频率。 Non zero frequencies will be transferred to a new list. 非零频率将被转移到新列表。

if adict['Frequency'] != 0 : newArr.append(adict) 
if bdict['Frequency'] != 0 : newArr.append(bdict)
if cdict['Frequency'] != 0 : newArr.append(cdict) 

After this, I had the newArr sorted by Frequency and transferred to a new list called finalArr. 在此之后,我将newArr按频率排序并转移到名为finalArr的新列表。 Below is a sample list contents for the word "Hello" 以下是单词“Hello”的示例列表内容

{'Letter': 'H', 'Frequency': 1}
{'Letter': 'e', 'Frequency': 1}
{'Letter': 'o', 'Frequency': 1}
{'Letter': 'l', 'Frequency': 2}

Now what I want is to transfer only the key values to 2 seperate lists; 现在我想要的是只将键值传递给2个单独的列表; letterArr and numArr. letterArr和numArr。 How do I do this? 我该怎么做呢? my desired output is: 我想要的输出是:

letterArr = [H,e,o,l]
numArr = [1,1,1,2]

Why don't you just use a collections.Counter ? 你为什么不只是使用collections.Countercollections.Counter For example: 例如:

from collections import Counter
from operator import itemgetter

word = input('Enter a word: ')
c = Counter(word)

letter_arr, num_arr = zip(*sorted(c.items(), key=itemgetter(1,0)))
print(letter_arr)
print(num_arr)

Note the use of sorted() to sort by increasing frequency. 注意使用sorted()来增加频率进行排序。 itemgetter() is used to reverse the sort order so that the sort is performed first on the frequency, and then on the letter. itemgetter()用于反转排序顺序,以便首先在频率上执行排序,然后在字母上执行排序。 The sorted frequencies are then separated using zip() on the unpacked list. 然后使用解压缩列表上的zip()分隔排序的频率。

Demo 演示

Enter a word: Hello
('H', 'e', 'o', 'l')
(1, 1, 1, 2)

The results are tuples, but you can convert to lists if you want with list(letter_arr) and list(num_arr) . 结果是元组,但如果需要,可以使用list(letter_arr)list(num_arr)转换为列表。

I have a hard time understanding your data structure choice for this problem. 我很难理解这个问题的数据结构选择。 Why don't you just go with a dictionary like this: 你为什么不跟这样的字典一起去:

frequencies = { 'H': 1, 'e': 1, 'l': 2, 'o': 1 }

Which is even easier to implement with a Counter: 使用计数器更容易实现:

from collections import Counter
frequencies = Counter("Hello")
print(frequencies)
>>> Counter({ 'H': 1, 'e': 1, 'l': 2, 'o': 1 })

Then to add another word, you'd simply have to use the update method: 然后要添加另一个单词,您只需使用update方法:

frequencies.update("How")
print(frequencies)
>>> Counter({'l': 2, 'H': 2, 'o': 2, 'w': 1, 'e': 1})

Finally, to get your 2 arrays, you can do: 最后,要获得2个阵列,您可以:

letterArr, numArr = zip(*frequencies.items())

This will give you tuples though, if you really need lists, just do: list(letterArr) 这会给你元组,如果你真的需要列表,只需: list(letterArr)

You wanted a simple answer without further todo like zip, collections, itemgetter etc. This does the minimum to get it done, 3 lines in a loop. 你想要一个简单的答案而不需要像zip,集合,项目等那样进一步完成。这样就完成了它的最小化,循环中有3行。

finalArr= [{'Letter': 'H', 'Frequency': 1},
           {'Letter': 'e', 'Frequency': 1},
           {'Letter': 'o', 'Frequency': 1},
           {'Letter': 'l', 'Frequency': 2}]

letterArr = []
numArr    = []
for i in range(len(finalArr)):
    letterArr.append(finalArr[i]['Letter'])
    numArr.append(finalArr[i]['Frequency'])
print letterArr
print numArr

Output is 输出是

['H', 'e', 'o', 'l']
[1, 1, 1, 2]

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

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