简体   繁体   English

Python - 根据列表中的反向位置计算分数

[英]Python - calculating scores based on reverse position in list

I can do this is very long-winded, hacky code.我可以做到这是非常冗长的,hacky 的代码。 Am looking for pointers as to how to do this in a clean, pythonic way.我正在寻找有关如何以干净的 Pythonic 方式执行此操作的指示。 I am using Python 3.5.2我正在使用Python 3.5.2

I have 2 ordered lists of words thus....我有 2 个有序的单词列表,因此....

ting_word_bag = ['word', 'tree', 'dependency', 'number', 'pattern']

ted_word_bag = ['dependency', 'verb', 'grammar', 'word', 'parser'] 

I want to iterate over the words in ted_word_bag and assign a value to each, based on the word's position in the ting_word_bag .我想的话迭代中ted_word_bag和值分配给每个,基于单词在位置ting_word_bag This is straightforward.这很简单。

What is less so is that I want to reverse the values so the first word in the ting_word_bag list is worth 5 points, and the last word in the list is worth 1 point (based on a list of 5 elements)更少的是,我想反转这些值,因此 ting_word_bag 列表中的第一个单词值 5 分,列表中的最后一个单词值 1 分(基于 5 个元素的列表)

For this example, the total score for the ted_word_bag would be 8 points.在这个例子中,ted_word_bag 的总分是 8 分。 5 for 'word' and 3 for 'dependency' . 5 代表'word'和 3 代表'dependency'

Any pointers on how to do this simply and quickly would be much appreciated.任何有关如何简单快速地执行此操作的指示将不胜感激。

Cheers.干杯。

Using comprehension, you can build a dict to store the "score" for each word:使用理解,您可以构建一个字典来存储每个单词的“分数”:

>>> some_dict = {j:i for i,j in enumerate(ting_word_bag)}
>>> some_dict
{'information': 5, 'word': 0, 'pattern': 4, 'tree': 1, 'number': 3, 'dependency': 2}

Using reversed or another method, you can obtain what you want.使用reversed或其他方法,您可以获得您想要的。

Next, with the .get(index, default-value) of the dictionary, you sum each value of your second list:接下来,使用字典的.get(index, default-value)对第二个列表的每个值求和:

>>> sum(some_dict.get(i,0) for i in ted_word_bag)
8 #with a correct value of some_dict
ting_word_bag = ['word', 'tree', 'dependency', 'number', 'pattern']

ted_word_bag = ['dependency', 'verb', 'grammar', 'word', 'parser'] 

Given that the following:鉴于以下情况:

from itertools import count

scores = dict(zip(reversed(ting_word_bag), count(1)))

total = sum(scores.get(i,0) for i in ted_word_bag)

Properly assigns the scores:正确分配分数:

>>> scores
{'pattern': 1, 'tree': 4, 'number': 2, 'word': 5, 'dependency': 3}

Even so the resulting total is the same:即便如此,结果总数还是一样的:

>>> total
8

Make a list comprehension to enumerate ting_word_bag in reversed order:进行列表ting_word_bagting_word_bag枚举ting_word_bag

points = [(i,w) for i,w in enumerate(ting_word_bag[::-1])]

Another comprehension for the score:对分数的另一种理解:

score =  sum([x[0] for x in points if x[1] in ted_word_bag]) 

this code does this (assuming for not found items the score is 0 ):此代码执行此操作(假设未找到项目的分数为0 ):

ting_word_bag = ['word', 'tree', 'dependency', 'number', 'pattern']
ted_word_bag = ['dependency', 'verb', 'grammar', 'word', 'parser']

score = {x: i + 1 for i, x in enumerate(reversed(ting_word_bag))}
values = [(score.get(x, 0), x) for x in ted_word_bag]

print(values)
print(sum(i[0] for i in values))

output:输出:

[(3, 'dependency'), (0, 'verb'), (0, 'grammar'), (5, 'word'), (0, 'parser')]
8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM