在Python中,我有一个y
打印为的ndarrayarray([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
我正在计算这个数组中有多少0s和多少1s.
但是,当我输入y.count(0)或者y.count(1),它说
numpy.ndarray对象没有属性count
我该怎么办?
在Python中,我有一个列表:
L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67]
Run Code Online (Sandbox Code Playgroud)
我想确定发生次数最多的项目.我能够解决它,但我需要最快的方法来解决它.我知道有一个很好的Pythonic答案.
我在查看SO上的职位发布时发现了这个编程问题.我认为它非常有趣,作为一名初学Python程序员,我试图解决它.但是我觉得我的解决方案非常......凌乱......任何人都可以提出任何建议来优化它或使其更清洁吗?我知道这很简单,但我写得很开心.注意:Python 2.6
问题:
为函数写入伪代码(或实际代码),该函数接收字符串并返回该字符串中出现最多的字母.
我的尝试:
import string
def find_max_letter_count(word):
alphabet = string.ascii_lowercase
dictionary = {}
for letters in alphabet:
dictionary[letters] = 0
for letters in word:
dictionary[letters] += 1
dictionary = sorted(dictionary.items(),
reverse=True,
key=lambda x: x[1])
for position in range(0, 26):
print dictionary[position]
if position != len(dictionary) - 1:
if dictionary[position + 1][1] < dictionary[position][1]:
break
find_max_letter_count("helloworld")
Run Code Online (Sandbox Code Playgroud)
输出:
>>>
('l', 3)
Run Code Online (Sandbox Code Playgroud)
更新示例:
find_max_letter_count("balloon")
>>>
('l', 2)
('o', 2)
Run Code Online (Sandbox Code Playgroud) 我是一个Python新手试图解析一个文件来制作一个内存分配表.我的输入文件格式如下:
48 bytes allocated at 0x8bb970a0
24 bytes allocated at 0x8bb950c0
48 bytes allocated at 0x958bd0e0
48 bytes allocated at 0x8bb9b060
96 bytes allocated at 0x8bb9afe0
24 bytes allocated at 0x8bb9af60
Run Code Online (Sandbox Code Playgroud)
我的第一个目标是创建一个表来计算特定数量的字节分配的实例.换句话说,我对上面输入的所需输出将是这样的:
48 bytes -> 3 times
96 bytes -> 1 times
24 bytes -> 2 times
Run Code Online (Sandbox Code Playgroud)
(现在,我不关心内存地址)
由于我正在使用Python,我认为使用字典这样做是正确的方法(基于大约3个小时的阅读Python教程).这是一个好主意吗?
在尝试使用字典时,我决定将字节数设为'key',将计数器设为'value'.我的计划是在每次出现钥匙时递增计数器.截至目前,我的代码段如下:
# Create an empty dictionary
allocationList = {}
# Open file for reading
with open("allocFile.txt") as fp:
for line in fp:
# Split the line into a list (using space …Run Code Online (Sandbox Code Playgroud) 我有一个这样的列表:
lst = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我想找到最常出现的所有元素.所以我想:
most = [1, 3, 5]
Run Code Online (Sandbox Code Playgroud)
1,3和5会发生最多,这是4次.什么是快速,pythonic方式来做到这一点?我试过这里显示的方法:
但它只给我前3名,我需要所有元素.谢谢.
我有一个如此处所示的列表. a=[1936,2401,2916,4761,9216,9216,9604,9801]
我想获得更多重复的值.在这里它是'9216'我怎么能得到这个值?谢谢
嗨,按照早先的帖子.
鉴于以下列表:
['Jellicle','猫','是','黑','和','白色','Jellicle','猫','是','宁可','小;','Jellicle' ,'猫','是','快乐','和','明亮','和','愉快','到','听','什么时候','他们','caterwaul'. ,'Jellicle','猫','有','开朗','面孔','Jellicle','猫','有','明亮','黑','眼睛;','他们' ,'喜欢','到','练习','他们','空气','和','增添','和','等','为',''','Jellicle','月亮','到','上升.','']
我试图计算每个出现在资本中的单词出现多少次并显示前三个单词.
我对那些不以资本开头的话语感兴趣.
如果一个单词出现多次,有时以资本开头而有时不出现,只计算它与资本的次数.
这就是我的代码目前的样子:
words = ""
for word in open('novel.txt', 'rU'):
words += word
words = words.split(' ')
words= list(words)
words = ('\n'.join(words)).split('\n')
word_counter = {}
for word in words:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
top_3 = popular_words[:3]
matches = []
for i in range(3):
print word_counter[top_3[i]], top_3[i]
Run Code Online (Sandbox Code Playgroud) python ×7
list ×3
max ×2
algorithm ×1
count ×1
counting ×1
frequency ×1
numpy ×1
optimization ×1
python-3.x ×1