小编Kee*_*yos的帖子

ImportError:无法导入名称计数器

我尝试过使用Counter(),但每次都这样做:

from collections import Counter
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

Traceback (most recent call last):
  File "<web session>", line 1, in <module>
ImportError: cannot import name Counter
Run Code Online (Sandbox Code Playgroud)

我实际上是否必须制作一个包含计数器的文件,然后从那里导入它或什么东西?我是初学者,所以只有最基本的答案才能做到.

python

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

如何从列表中的字符串末尾删除特定标点符号并将所有单词设置为小写

我有一个字符串列表,需要从列表中每个字符串的末尾剥离一些标点符号.该列表如下:

list = ['Twas', 'brillig,', 'and', 'the', 'slithy', 'toves', 'Did', 'gyre',
        'and', 'gimble', 'in', 'the', 'wabe:']  #the list is a lot longer
Run Code Online (Sandbox Code Playgroud)

我需要删除所有标点符号,'"-,.:;!?只包括每个字符串的结尾,并使所有单词都小写.

我需要'Twas'成为'twas',我需要'wabe:'成为'wabe',等等......列表中我没有包含的其他单词包含最后的其他标点符号.

我尝试使用.rstrip().lower()case但我不知道如何使用for或while循环遍历列表中的每个字符串并执行此操作.如果有其他方式不需要使用.rstrip.lower我对他们开放.

我是一个使用python的初学者,所以非常基本的答案会帮助我,如果你能准确地解释你做了什么,我将不胜感激.

python

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

如何在python中获取列表中最常用的10个字符串

我有一个包含93个不同字符串的列表.我需要找到10个最常见的字符串,并且返回必须按顺序从最频繁到最不频繁.

mylist = ['"and', '"beware', '`twas', 'all', 'all', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'and', 'arms', 'as', 'as', 'awhile', 'back', 'bandersnatch', 'beamish', 'beware', 'bird', 'bite', 'blade', 'borogoves', 'borogoves', 'boy', 'brillig']
 # this is just a sample of the actual list.
Run Code Online (Sandbox Code Playgroud)

我没有最新版本的python,不能使用计数器.

python

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

标签 统计

python ×3