简体   繁体   English

计算两个列表中的频率,Python

[英]Counting frequencies in two lists, Python

I'm new to programming in python so please bear over with my newbie question... 我是python编程的新手,所以请关注我的新手问题......

I have one initial list (list1) , which I have cleaned for duplicates and ended up with a list with only one of each value (list2): 我有一个初始列表(list1),我已经清理了重复项,最后只有一个列表,每个值只有一个(list2):

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16], list1 = [13,19,13,2,16,6,5,19,20,21,20,13,19,13,16],

list2 = [13, 19, 2, 16, 6, 5, 20, 21] list2 = [13,19,2,16,6,5,20,21]

What I want is to count how many times each of the values in "list2" appears in "list1", but I can't figure out how to do that without getting it wrong. 我想要的是计算“list2”中每个值出现在“list1”中的次数,但我无法弄清楚如何做到这一点而不会出错。

The output I am looking for is something similar to this: 我正在寻找的输出类似于:

Number 13 is represented 1 times in list1. 数字13在list1中表示1次。 ........ Number 16 is represented 2 times in list1. ........数字16在list1中表示2次。

visited = []
for i in list2:
  if i not in visited:
    print "Number", i, "is presented", list1.count(i), "times in list1"
    visited.append(i)

The easiest way is to use a counter: 最简单的方法是使用计数器:

from collections import Counter
list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
c = Counter(list1)
print(c)

giving

Counter({2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1})

So you can access the key-value-pairs of the counter representing the items and its occurrences using the same syntax used for acessing dicts: 因此,您可以使用与访问dicts相同的语法来访问表示项目及其出现次数的计数器的键值对:

for k, v in c.items():
    print('- Element {} has {} occurrences'.format(k, v))

giving: 赠送:

- Element 16 has 2 occurrences
- Element 2 has 1 occurrences
- Element 19 has 3 occurrences
- Element 20 has 2 occurrences
- Element 5 has 1 occurrences
- Element 6 has 1 occurrences
- Element 13 has 4 occurrences
- Element 21 has 1 occurrences

Simplest , easiest to understand, no-magic-approach is to create an object(associative array) and just count the numbers in list1: 最简单最容易理解, 无魔法的方法是创建一个对象(关联数组)并只计算list1中的数字:

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]

frequency_list = {}

for l in list1:
    if l in frequency_list:
        frequency_list[l] += 1
    else:
        frequency_list[l] = 1

print(frequency_list)

prints out this: 打印出来:

{
    16: 2,
    2: 1,
    19: 3,
    20: 2,
    5: 1,
    6: 1,
    13: 4,
    21: 1
}

meaning 16 is there twice, 2 is there once... 意思是16有两次,2有一次......

You can also use operator 您也可以使用运算符

>>> list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16],
>>> list2 = [13, 19, 2, 16, 6, 5, 20, 21]

>>> import operator
>>> for s in list2:
...    print s, 'appearing in :',  operator.countOf(list1, s)

In technical terms, list is a "type" of "object". 在技​​术术语中, list是“对象”的“类型”。 Python has a number of built in types like strings ( str ), integers ( int ), and a few others that can be easily found on google. Python有许多内置类型,如字符串( str ),整数( int ),以及其他一些可以在谷歌上轻松找到的类型。 The reason this is important is because each object type has its own "methods". 这很重要的原因是因为每个对象类型都有自己的“方法”。 You can think of these methods as functions that complete common programming tasks and make your life easier. 您可以将这些方法视为完成常见编程任务并使您的生活更轻松的功能。

Counting the number of occurrences in a list is an example of a common programing task. 计算列表中出现的次数是常见编程任务的示例。 We can accomplish it with the count() method. 我们可以使用count()方法完成它。 For example, to count the number of times 13 appears in list1: 例如,要计算list1中出现的次数13:

count_13 = list1.count(13)

We can also use a for loop to iterate over the whole list: 我们还可以使用for循环遍历整个列表:

for x in list2:
    print(list1.count(x)) #This is for python version 3 and up

or for python versions older than 3: 或者对于3岁以上的python版本:

for x in list2:
    print list1.count(x)

You don't need to remove duplicates. 您不需要删除重复项。 When you add to a dictionary, automatically, the duplicates will be considered as single values. 当您自动添加到字典时,重复项将被视为单个值。

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
counts = {s:list1.count(s) for s in list1}
print counts

{2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1}

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

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