小编Pri*_*yan的帖子

如何查找以特定字符开头的单词

我想整理出python中用's'开头的单词.
这是我的代码:

import re
text = "I was searching my source to make a big desk yesterday."
m = re.findall(r'[s]\w+', text)
print m
Run Code Online (Sandbox Code Playgroud)

但代码的结果是:

['searching', 'source', 'sk', 'sterday'].
Run Code Online (Sandbox Code Playgroud)

如何编写有关正则表达式的代码?或者,有什么方法可以整理单词吗?

python regex

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

python中字典中每个元素的总和

我有这样的数据文件:

{'one', 'four', 'two', 'eight'}
{'two', 'three', 'seven', 'eight'}
Run Code Online (Sandbox Code Playgroud)

我想获得元素的总数并计算每个元素.结果如下:

total of element: 8
one: 1, two: 2, eight: 2, seven: 1, three: 1, four: 1
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

with open("data.json") as f:
     for line in f:
         result = json.loads(line)
         if 'text' in result.keys():
             response = result['text'] 
             words = response.encode("utf-8").split()
        list={}
        for word in words:
Run Code Online (Sandbox Code Playgroud)

在此之后,我不知道如何获得元素总数并计算每个元素.你可以帮帮我吗?

python dictionary

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

标签 统计

python ×2

dictionary ×1

regex ×1