简体   繁体   English

python中列表的列表频率

[英]Frequency of list in list in python

I have found several questions regarding finding the frequency of a value in a list. 我发现了一些有关在列表中查找值频率的问题。 Though I haven't found anything regarding finding the frequency of a list in a list. 尽管我还没有找到有关在列表中查找列表频率的任何信息。 (or ndarray in ndarray) (或ndarray中的ndarray)

In essence I want to find the unique rows in : 本质上,我想在中找到唯一的行:

ndarray: [[ 3.95428571 5.67428571] ndarray:[[3.95428571 5.67428571]

 [ 3.795       4.67166667]
 [ 5.05        6.79      ]
 [ 4.54333333  6.16666667]
 [ 4.7175      6.31      ]
 [ 4.81        6.41      ]
 [ 3.82166667  5.34666667]
 [ 4.16        6.315     ]
 [ 3.915       4.855     ]
 [ 4.44        6.57      ]
 [ 5.1         6.78      ]
 [ 4.03        6.655     ]
 [ 3.71        6.22      ]
 [ 4.57142857  5.51      ]
 [ 3.67        5.45      ]
 [ 4.048       5.484     ]
 [ 4.24714286  5.31142857]
 [ 4.125       6.175     ]
 [ 4.72        4.18      ]
 [ 4.02125     5.82625   ]
 [ 3.729       5.688     ]
 [ 4.17666667  5.80666667]
 [ 4.08        6.102     ]
 [ 5.05        7.1       ]
 [ 4.22        4.968     ]
 [ 3.6625      5.9625    ]
 [ 4.444       5.832     ]
 [ 4.395       7.09      ]
 [ 4.39        5.        ]
 [ 4.745       5.995     ]
 [ 4.81        7.25      ]
 [ 3.74285714  6.22571429]
 [ 5.52        4.38      ]
 [ 3.92        4.1       ]
 [ 3.525       5.91833333]
 [ 3.85666667  6.09333333]
 [ 3.42        5.87...

and their corresponding frequency. 及其相应的频率。 ( I want to plot a 2d histogram) (我想绘制一个二维直方图)

Any ideas/tips/solutions ? 有什么想法/技巧/解决方案吗?

您应该看看numpy.histogram2d

Or convert the items to tuples and hash them like so :- 或将项目转换为元组并像这样对它们进行哈希处理:-

l = [[ 3.95428571, 5.67428571],
 [ 3.795       ,4.67166667],
 [ 5.05        ,6.79      ],  
 [ 4.54333333  ,6.16666667],  
 [ 5.1         ,6.78      ],  
 [ 4.03        ,6.655     ],  
 [ 3.71        ,6.22      ]]


hashtable = dict()
for i in l:
    hashtable.setdefault(tuple(i), 0)
    hashtable[tuple(i)] = hashtable[tuple(i)]+1

print hashtable

This works :- 这有效:-

$ python test.py
{(4.44, 6.57): 1, (3.915, 4.855): 1, (4.54333333, 6.16666667): 1, (4.7175, 6.31): 1, (4.03, 6.655): 1, (5.1, 6.78): 1, (3.71, 6.22): 1, (3.82166667, 5.34666667): 1, (4.81, 6.41): 1, (3.795, 4.67166667): 1, (5.05, 6.79): 1, (4.16, 6.315): 1, (3.95428571, 5.67428571): 1}

Generally, as long as your data is hashable, you can use defaultdict to count every occurence. 通常,只要您的数据是可散列的,就可以使用defaultdict来计算每次发生的次数。 Since list is not hashable, I have transformed it to a tuple . 由于list不可散列,因此我将其转换为tuple So assuming your data are in data variable (list of lists), this should work and print simple histogram: 因此,假设您的数据位于data变量(列表列表)中,则此方法应该有效并打印简单的直方图:

from collections import defaultdict
counts = defaultdict(int)
for x in data:
    counts[tuple(x)] += 1
for val, cnt in sorted(counts.iteritems(), key=lambda x: x[1]):
    print '%3d: %s' % (cnt, val)

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

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