我想生成大量文本中最不常见单词的有序列表,其中最常见的单词首先出现,并且值指示它在文本中出现的次数.
我从一些在线期刊文章中删除了文本,然后简单地分配和分割;
article_one = """ large body of text """.split()
=> ("large","body", "of", "text")
Run Code Online (Sandbox Code Playgroud)
看起来像正则表达式适合接下来的步骤,但是对编程不熟悉我不太精通 - 如果最好的答案包括正则表达式,有人能指出我除了pydoc之外的一个很好的正则表达式教程吗?
在寻找解决方案时,通常会遇到几种方法。我经常使用与我熟悉的语法最接近的解决方案。但有时最受好评的解决方案涉及导入一个对我来说是新的模块,就像在这个线程中一样。
我已经在将循环 50K 次的大型脚本中导入各种模块。导入附加模块是否会影响处理时间,或者是否会影响脚本的效率?我需要担心被调用模块的大小吗?寻求有关使用我已经在使用的模块中包含的方法来寻找解决方案的额外时间/努力是否值得的指导。
如何从字典中的键设置 xticks?在我的原始代码中,字典是空的,并根据数据文件填充,所以我不能为 xticks 设置任何静态内容。根据用户输入的内容(1-10 之间的数字),图表从该金额的最高值到最低值绘制,但我希望用户能够看到该值与哪个 IP 相关。键是 IP 地址,所以刻度也必须是垂直的,因为它们占用了相当多的空间。谢谢
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
frequency2 = Counter({'205.166.231.2': 10, '205.166.231.250': 7, '205.166.231.4': 4, '98.23.108.3': 2, '205.166.231.36': 1})
vals = sorted(frequency2.values(), reverse=True)
response2 = int(input("How many top domains from source? Enter a number between 1-10: "))
if response2 > 0 and response2 < len(vals)+1:
figure(1)
y = vals[:response2]
print ("\nTop %i most domains are:" %response2)
for key, frequency2_value in frequency2.most_common(response2):
print("\nDomain IP:",key,"with frequency:",frequency2_value)
x = …Run Code Online (Sandbox Code Playgroud) 我到目前为止:
def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
Run Code Online (Sandbox Code Playgroud)
返回:
{'a': 2, 'c': 1, 'b': 3}
Run Code Online (Sandbox Code Playgroud)
现在我需要:
我应该将此字典转换为元组还是列表?
我有一个python字典:
x = {'a':10.1,'b':2,'c':5}
我如何进行排名并返回排名值?喜欢回来:
res = {'a':1,c':2,'b':3}
谢谢
编辑:
我不是要尝试排序,因为可以通过sortedpython中的函数来完成.我更考虑将等级值从最高到最小......所以在排序后用字符值替换字典值.1表示最高,3表示最低.
我有以下内容OrderedDict:
from collections import OrderedDict
a = OrderedDict()
a['2016:April'] = 1
a['2016:January'] = 2
a['2017:February'] = 3
a['2015:November'] = 4
Run Code Online (Sandbox Code Playgroud)
我想按时间顺序按键排序字典,结果如下:
OrderedDict([('2015:November', 4), ('2016:January', 2), ('2016:April', 1), ('2017:February', 3)])
Run Code Online (Sandbox Code Playgroud) 假设我们希望像这样从字典中提取最小值
scores = {
0:1.3399288498085087,
1:1.2672683347433629,
3:1.6999159970296505,
4:1.8410942584597279,
5:1.336658057628646
}
#find minimum value in dictionary
minimum_value = min(scores.values())
#get keys with minimal value using list comprehension
minimum_keys = [key for key in scores if scores[key]==minimum_value]
minimum_keys
Run Code Online (Sandbox Code Playgroud)
这将返回具有最低值的键。但是,如果我想提取最少 2 个键并将它们放入列表中怎么办?如果我想要最少 20 个怎么办?对于任意数量的所需最小值,我将如何做到这一点?
我已经看过这篇文章和这篇文章以及其他许多文章,但还没有找到我的问题的答案,也无法弄明白.
我有一份清单.例如,它看起来像:
Dict = {'a':[1,2,3,4], 'b':[9,8,7,6], 'c':[8,5,3,2]}
Run Code Online (Sandbox Code Playgroud)
我想根据列表中的特定项返回已排序(降序/反向)的键列表.例如,我想根据每个列表中的第4项对a,b,c进行排序.
这应该返回sorted_keys = ['b','a','c']按值排序的列表[6,4,2].
合理?请帮忙......谢谢!
我想获得按其值排序的键列表,如果有任何关系,按字母顺序排序.我可以按值排序.如果是关系,我面临着问题.
对于字典:
aDict = {'a':8, 'one' : 1, 'two' : 1, 'three':2, 'c':6,'four':2,'five':1}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
sorted(aDict, key=aDict.get, reverse=True)
Run Code Online (Sandbox Code Playgroud)
这给了我:
['a', 'c', 'three', 'four', 'two', 'five', 'one']
Run Code Online (Sandbox Code Playgroud)
但我想要:
['a', 'c', 'four', 'three', 'five', 'one', 'two']
Run Code Online (Sandbox Code Playgroud)