相关疑难解决方法(0)

如何计算列表中元素的频率?

我需要在列表中找到元素的频率

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
Run Code Online (Sandbox Code Playgroud)

输出 - >

b = [4,4,2,1,2]
Run Code Online (Sandbox Code Playgroud)

另外我想从a中删除重复项

a = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)

python counter frequency counting

214
推荐指数
12
解决办法
42万
查看次数

numpy:数组中唯一值的最有效频率计数

numpy/中scipy,是否有一种有效的方法来获取数组中唯一值的频率计数?

这些方面的东西:

x = array( [1,1,1,2,2,2,5,25,1,1] )
y = freq_count( x )
print y

>> [[1, 5], [2,3], [5,1], [25,1]]
Run Code Online (Sandbox Code Playgroud)

(对你来说,R用户在那里,我基本上都在寻找这个table()功能)

python arrays performance numpy

210
推荐指数
10
解决办法
24万
查看次数

Python:使用字典计算列表中的项目

我是Python的新手,我有一个简单的问题,比如我有一个项目列表:

['apple','red','apple','red','red','pear']
Run Code Online (Sandbox Code Playgroud)

什么是将列表项添加到字典中的最简单方法,并计算项目在列表中出现的次数.

所以对于上面的列表,我希望输出为:

{'apple': 2, 'red': 3, 'pear': 1}
Run Code Online (Sandbox Code Playgroud)

python

159
推荐指数
8
解决办法
20万
查看次数

如何按值对计数器进行排序? - 蟒蛇

除了对反向列表理解进行列表理解之外,还有一种pythonic方法可以按值对Counter进行排序吗?如果是这样,它比这更快:

>>> from collections import Counter
>>> x = Counter({'a':5, 'b':3, 'c':7})
>>> sorted(x)
['a', 'b', 'c']
>>> sorted(x.items())
[('a', 5), ('b', 3), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()])]
[('b', 3), ('a', 5), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()], reverse=True)]
[('c', 7), ('a', 5), ('b', 3)
Run Code Online (Sandbox Code Playgroud)

python sorting collections counter

107
推荐指数
4
解决办法
9万
查看次数

使用列表推导仅仅是副作用是Pythonic吗?

想想我正在调用它的副作用的函数,而不是返回值(比如打印到屏幕,更新GUI,打印到文件等).

def fun_with_side_effects(x):
    ...side effects...
    return y
Run Code Online (Sandbox Code Playgroud)

现在,是Pythonic使用列表推导来调用这个函数:

[fun_with_side_effects(x) for x in y if (...conditions...)]
Run Code Online (Sandbox Code Playgroud)

请注意,我不会将列表保存在任何位置

或者我应该像这样调用这个函数:

for x in y:
    if (...conditions...):
        fun_with_side_effects(x)
Run Code Online (Sandbox Code Playgroud)

哪个更好?为什么?

python list-comprehension

97
推荐指数
4
解决办法
8534
查看次数

计算Python列表中出现次数的最快方法

我有一个Python列表,我想知道'1'在这个列表中计算项目出现次数的最快方法是什么.在我的实际情况中,该项目可以发生数万次,这就是为什么我想要一个快速的方式.

['1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '7', '7', '7', '10', '10']
Run Code Online (Sandbox Code Playgroud)

.count模块有帮助吗?我正在使用Python 2.7

python list repeat

40
推荐指数
2
解决办法
8万
查看次数

打印包含和排除停用词的10个最常出现的文字

我从这里得到了我的改变的问题.我有以下代码:

from nltk.corpus import stopwords
>>> def content_text(text):
    stopwords = nltk.corpus.stopwords.words('english')
    content = [w for w in text if w.lower() in stopwords]
    return content
Run Code Online (Sandbox Code Playgroud)

如何打印 1)最常出现的文字1)包括和2)不包括停用词?

python nltk word-frequency find-occurrences

10
推荐指数
2
解决办法
2万
查看次数

在list-python中找到-1s,1s和0s的多数票

如何找到包含-1s,1s和0s的列表的多数票?

例如,给出一个列表:

x = [-1, -1, -1, -1, 0]
Run Code Online (Sandbox Code Playgroud)

大多数是-1,因此输出应该返回 -1

另一个例子,给出一个列表:

x = [1, 1, 1, 0, 0, -1]
Run Code Online (Sandbox Code Playgroud)

多数票将是 1

当我们有平局时,多数投票应该返回0,例如:

x = [1, 1, 1, -1, -1, -1]
Run Code Online (Sandbox Code Playgroud)

这也应该返回零:

x = [1, 1, 0, 0, -1, -1]
Run Code Online (Sandbox Code Playgroud)

获得多数投票的最简单案例似乎是将列表加起来并检查它是否为负数,正数或0.

>>> x = [-1, -1, -1, -1, 0]
>>> sum(x) # So majority -> 0
-4
>>> x = [-1, 1, 1, 1, 0]
>>> sum(x) # So majority -> 1
2
>>> x = [-1, -1, 1, 1, …
Run Code Online (Sandbox Code Playgroud)

python binary if-statement voting list

9
推荐指数
2
解决办法
2941
查看次数

内部列表中的第一项尽可能有效

我在python中有一个协调的存储列表, A[row,col,value]用于存储非零值.

如何获取所有行索引的列表?我希望这A[0:][0]可以作为print A[0:]打印整个列表,但print A[0:][0]只打印A[0].

我要问的原因是为了有效计算每一行中非零值的数量,迭代,range(0,n)其中n是总行数.这应该比我目前的方式便宜得多for i in range(0,n): for j in A: ....

就像是:

c = []
# for the total number of rows
for i in range(0,n):
     # get number of rows with only one entry in coordinate storage list
     if A[0:][0].count(i) == 1: c.append(i)                
return c
Run Code Online (Sandbox Code Playgroud)

过度:

c = []
# for the total number of rows 
for i in …
Run Code Online (Sandbox Code Playgroud)

python optimization performance list

6
推荐指数
1
解决办法
800
查看次数

Python:使用dict理解/生成器计算列表中的出现次数

我想编写一些测试来分析python中不同操作的效率,即字典理解和dict生成器的比较。

为了验证这一点,我想我会尝试一个简单的示例:使用字典计算列表中的单词数。

现在,我知道您可以使用collections.Counter(按照这里的答案:如何计算Python中列表项的出现?)进行此操作,但是我的目标是测试内存性能。

一种“长手”方法是在基本循环中进行操作。

from pprint import pprint

# Read in some text to create example data
with open('text.txt') as f:
    words = f.read().split()

dict1 = {}
for w in words:
    if not dict1.get(w):
        dict1[w] = 1
    else:
        dict1[w] += 1
pprint(dict1)
Run Code Online (Sandbox Code Playgroud)

结果:

{'a': 62,
 'aback': 1,
 'able': 1,
 'abolished': 2,
 'about': 6,
 'accept': 1,
 'accepted': 1,
 'accord': 1,
 'according': 1,
 'across': 1,
 ...
Run Code Online (Sandbox Code Playgroud)

然后,在字典理解中尝试执行相同操作时,我有些卡住了:

dict2  = { w: 1 if not dict2.get(w) else dict2.get(w) + …
Run Code Online (Sandbox Code Playgroud)

python performance dictionary generator

6
推荐指数
2
解决办法
6448
查看次数