下面是我尝试过的代码,check_keyword()方法基本上是将字符串与单词词典进行比较,如果单词匹配,则增加计数并在词典中找到最大值:
请关注我评论过“找到最大浮点值”的代码
gl_Position.w
count_dic的输出:
def check_keyword():
new_dict = {}
count_dict = {}
new_list = []
new_list2 = []
count = 0
with open(unknown.txt, "r") as fp:
unknown_file = fp.read()
print(unknown_file)
# read key phases from text file as a dictionary
df = pd.read_csv(key_phases.txt, sep='|')
key_phases_dict = df.to_dict(orient='records')
for i in key_phases_dict:
new_list = list(i.values())
new_dict[new_list[0]] = new_list[1]
for key in new_dict.keys():
count_dict[key] = 0
new_list2 = new_dict[key].split(",")
new_dict[key] = new_list2
for j in new_dict[key]:
if j in unknown_file:
print(j)
count_dict[key] = count_dict[key] + 1
count_dict[key] = float(count_dict[key] / len(new_list2))
print(count_dict)
# find the maximum float value
for k, v in count_dict.items():
if v > count:
highest_list = []
result = k, v
highest_list.append(result)
count = v
else:
v == count
result = k, v
highest_list.append(result)
return highest_list
遇到的问题是,当我打印highest_list时,它给了我(它没有显示出最大值):
{2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}
所需的输出实现:
[(3, 0.08666666666666667), (4, 0.08666666666666667), (5, 0.0), (6, 0.04666666666666667), (7, 0.02), (8, 0.013333333333333334)]
答案 0 :(得分:2)
您可以只计算最大值,然后使用列表推导:
d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}
maxval = max(d.values())
res = [(k, v) for k, v in d.items() if v == maxval]
[(3, 0.08666666666666667), (4, 0.08666666666666667)]
答案 1 :(得分:1)
有两种解决方法。
具有sorted
和列表理解的人:
d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}
sorted_items = sorted(d.items(), key=lambda x: x[1], reverse=True)
results = [item for item in sorted_items if item[1] == sorted_items[0][1]]
# output: [(3, 0.08666666666666667), (4, 0.08666666666666667)] #
另一个是sorted
和filter
:
d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}
sorted_items = sorted(d.items(), key=lambda x: x[1], reverse=True)
results = filter(lambda x: x[1] == sorted_items[0][1], sorted_items)
# output: [(3, 0.08666666666666667), (4, 0.08666666666666667)] #
使用sorted
,您可以使用key
按照字典的值对项目进行排序。 sorted_items
将为您提供:
[(3, 0.08666666666666667), (4, 0.08666666666666667), (6, 0.04666666666666667), (2, 0.02666666666666667), (7, 0.02), (8, 0.013333333333333334), (5, 0.0)]
包含reverse
使得结果的第一个索引将是最高值。
如果有多个具有相同最大值的索引,则获取results
的第二行是过滤列表。这样,它会修剪列表,最后得到最后两个值。
答案 2 :(得分:0)
代替
v == count
result = k, v
highest_list.append(result)
尝试:
v = count
result = k, v
highest_list.append(result)
换句话说,将==
更改为=
。