简体   繁体   English

Python字典计数器位置变化

[英]Python dictionary counter positions change

So i'm trying to count the most repeated values in an text file.所以我试图计算文本文件中重复次数最多的值。 By using the Counter method it returns exaclty what im looking for通过使用Counter方法,它会返回我正在寻找的内容

file.txt文件.txt

12334
99965
99965
44144
99965
00000
44144

script.py脚本文件

pArray=[]
with open("file.txt") as my_file:
        for line in my_file:
             pArray.append((line.split('\n'))[0])
        dictn = Counter(pArray)
        print(dictn)
        for key, value in dictn.items():
                print("KEY",key)    
                print("VALUE",value)
        print(dictn)

OUTPUT输出

Counter({'99965': 3, '44144': 2, '12334': 1, '00000': 1})
KEY 12334
VALUE 1
KEY 99965
VALUE 3
KEY 44144
VALUE 2
KEY 00000
VALUE 1
['12334', '99965', '44144', '00000']

But as you can see the output of the final array is not in the same order as the dictionary但是正如您所看到的,最终数组的输出与字典的顺序不同
( value should be in descending order) value应按降序排列)

I am expecting an output like我期待像这样的输出

 ['99965', '44144', '12334', '00000']

I also tried list(dictn.keys()) but i got the same output :/我也试过list(dictn.keys())但我得到了相同的输出:/
Why is the order changing and how can I fix it?为什么订单会发生变化,我该如何解决?

From the docs , we see that Counter objects: are "unordered collections" - much like dictionaries , (in-fact they are a sub-class).文档中,我们看到Counter对象:是“无序集合”——很像dictionaries (实际上它们是一个子类)。 So this means that iterating over .items() won't give the elements in order of size.所以这意味着迭代.items()不会按大小顺序给出元素。

However, we can simply use .most_common which returns a list of tuples - each containing an element and its count.但是,我们可以简单地使用.most_common返回一个元组列表- 每个包含一个元素及其计数。 The most important thing being that it is in order.最重要的是它是有序的。

So all we need to do is use a list-comprehension to extract the first element of each tuple in the list returned.所以我们需要做的就是使用list-comprehension来提取返回列表中每个元组的第一个元素。 That can be done with:这可以通过以下方式完成:

[t[0] for t in dictn.most_common()]

which gave:这给了:

['99965', '44144', '12334', '00000']

but could also give the following as the counts for '12334' and '00000' are the same.但也可以给出以下内容,因为'12334''00000'的计数相同。 This is unavoidable due to the nature of how dictionaries (and Counters) work.由于字典(和计数器)工作方式的性质,这是不可避免的。 But if this is important, just let me know and I can update the answer.但如果这很重要,请告诉我,我可以更新答案。

['99965', '44144', '00000', '12334']

Note that not all of your code needs to be inside the with statement, once you have created pArray , you can exit the with statement.请注意,并非所有代码都需要在with语句中,一旦创建了pArray ,就可以退出with语句。 Also, basic Python uses lists , not arrays !此外,基本的 Python 使用lists而不是arrays

If you want to print Key , Value pairs you can do如果你想打印KeyValue对,你可以做

for key in reversed(sorted(dictn)):
    print("KEY: {0}, VALUE: {1}".format(key, dictn[key]))

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

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