Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中列表中列表的频率_Python_List - Fatal编程技术网

python中列表中列表的频率

python中列表中列表的频率,python,list,Python,List,关于在列表中查找值的频率,我发现了几个问题。虽然我还没有找到任何关于在列表中查找列表频率的信息。(或ndarray中的ndarray) 本质上,我希望在以下内容中找到唯一的行: 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

关于在列表中查找值的频率,我发现了几个问题。虽然我还没有找到任何关于在列表中查找列表频率的信息。(或ndarray中的ndarray)

本质上,我希望在以下内容中找到唯一的行:

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...
和它们相应的频率。(我想画一个二维直方图)


有什么想法/技巧/解决方案吗?

你应该看看
numpy.historogram2d
你应该看看
numpy.historogram2d
或者把项目转换成元组,然后像这样散列:-

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
这项工作:-

$ 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}

或者将项目转换为元组并按如下方式对其进行散列:-

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
这项工作:-

$ 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}

通常,只要您的数据是可散列的,就可以使用
defaultdict
来计算每次发生的次数。由于
list
不可散列,因此我将其转换为
元组。
因此,假设您的数据在
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)

通常,只要您的数据是可散列的,就可以使用
defaultdict
来计算每次发生的次数。由于
list
不可散列,因此我将其转换为
元组。
因此,假设您的数据在
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)

结果很好:'D结果很好:'D