我想编写一些测试来分析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编程的新手,所以请关注我的新手问题......
我有一个初始列表(list1),我已经清理了重复项,最后只有一个列表,每个值只有一个(list2):
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"中每个值出现在"list1"中的次数,但我无法弄清楚如何做到这一点而不会出错.
我正在寻找的输出类似于:
数字13在list1中表示1次.........数字16在list1中表示2次.
我需要找到一种方法来计算从0到9的每个数字出现在使用的随机矩阵中的次数 np.random.randint()
import numpy as np
p = int(input("Length of matrix: "))
m = np.random.randint(0,9,(p,p))
print(m)
Run Code Online (Sandbox Code Playgroud)
例如,如果矩阵的长度= 4
4号出现多少次?它应该返回5.
基本上我只需要弄清楚如何从Python的列表中生成模式(最常出现的数字),无论该列表是否有多种模式?
像这样的东西:
def print_mode (thelist):
counts = {}
for item in thelist:
counts [item] = counts.get (item, 0) + 1
maxcount = 0
maxitem = None
for k, v in counts.items ():
if v > maxcount:
maxitem = k
maxcount = v
if maxcount == 1:
print "All values only appear once"
if counts.values().count (maxcount) > 1:
print "List has multiple modes"
else:
print "Mode of list:", maxitem
Run Code Online (Sandbox Code Playgroud)
但是,不是在"所有值只显示一次"或"列表有多种模式"中返回字符串,我希望它返回它引用的实际整数?
我有一个这样的清单:[5,6,7,2,4,8,5,2,3]
我想检查此列表中每个元素存在多少次.
在Python中使用它的最佳方法是什么?
我想计算X列表中的实例,类似于
但要考虑到每个实例的权重。
例如,
L = [(a,4), (a,1), (b,1), (b,1)]
Run Code Online (Sandbox Code Playgroud)
函数weighted_count()应该返回类似
[(a,5), (b,2)]
Run Code Online (Sandbox Code Playgroud)
编辑添加:my a,b将是整数。
说我有一个国家名单
l = ['India', 'China', 'China', 'Japan', 'USA', 'India', 'USA']
Run Code Online (Sandbox Code Playgroud)
然后我有一个独特的国家清单
ul = ['India', 'China', 'Japan', 'USA']
Run Code Online (Sandbox Code Playgroud)
我想按升序对列表中的每个独特国家/地区进行计数。因此输出应如下所示:
Japan 1
China 2
India 2
USA 2
Run Code Online (Sandbox Code Playgroud) 我正在用Python 3编写一个程序,它的一部分功能是找出列表中出现最多的单词并返回该单词的出现次数.我有适用的代码,但部分要求是它需要一个200,000多个单词的列表并在几秒钟内完成此活动,并且我的代码需要很长时间才能运行.我想知道你对这种方法的速度改进有什么建议.
def max_word_frequency(words):
"""A method that takes a list and finds the word with the most
occurrences and returns the number of occurences of that word
as an integer.
"""
max_count = 0
for word in set(words):
count = words.count(word)
if count > max_count:
max_count = count
return max_count
我已经考虑过使用字典,因为与列表相比它们可以清洗和超级快速,但我还不知道如何实现它.
谢谢大家的时间!
- 芬恩
我试图计算一个单词的每个字母的出现次数
word = input("Enter a word")
Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i in range(0,26):
print(word.count(Alphabet[i]))
Run Code Online (Sandbox Code Playgroud)
这当前输出每个字母出现的次数,包括不出现的次数.
如何垂直列出字母及其旁边的频率,例如:
字="你好"
H 1
E 1
L 2
O 1
我正在寻找一种有效的方法来获取包含出现两次或多次的多个元素的列表,并将其转换为字典,其中值等于出现次数。
示例列表:
l = ['dog', 'bird', 'bird', 'cat', 'dog', 'fish', 'cat', 'cat', 'dog', 'cat', 'bird', 'dog']
l_dict = {'dog':4, 'bird': 3, 'cat': 4, 'fish': 1}
Run Code Online (Sandbox Code Playgroud)
任何建议表示赞赏。谢谢。
python ×10
count ×3
list ×3
dictionary ×2
performance ×2
counting ×1
frequency ×1
generator ×1
mode ×1
numpy ×1
python-3.x ×1