相关疑难解决方法(0)

如何计算Python中ndarray中某些项的出现次数?

在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 numpy count multidimensional-array

308
推荐指数
17
解决办法
43万
查看次数

Python-找到列表中出现次数最多的项目

在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答案.

python list max counting

51
推荐指数
5
解决办法
8万
查看次数

查找字符串中最常见的字符

我在查看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 algorithm optimization time-complexity

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

Python字典,变量作为键

我是一个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)

python

7
推荐指数
1
解决办法
3204
查看次数

Pythonic方法找到频率最高的所有元素?

我有一个这样的列表:

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名,我需要所有元素.谢谢.

python list frequency max

4
推荐指数
1
解决办法
1382
查看次数

如何从python中的列表中获取最常见的元素

我有一个如此处所示的列表. a=[1936,2401,2916,4761,9216,9216,9604,9801] 我想获得更多重复的值.在这里它是'9216'我怎么能得到这个值?谢谢

python list python-3.x

2
推荐指数
1
解决办法
6587
查看次数

查找列表的常见元素

嗨,按照早先的帖子.

鉴于以下列表:

['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

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