Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-基于列表中的反向位置计算分数_Python - Fatal编程技术网

Python-基于列表中的反向位置计算分数

Python-基于列表中的反向位置计算分数,python,Python,我能做到这一点是非常冗长,黑客代码。我正在寻找如何以一种干净的、类似蟒蛇的方式来做这件事的指针。我正在使用Python 3.5.2 我有两个有序的单词列表,因此 ting_word_bag = ['word', 'tree', 'dependency', 'number', 'pattern'] ted_word_bag = ['dependency', 'verb', 'grammar', 'word', 'parser'] 我想迭代ted_word_bag中的单词,并根据单词在ting

我能做到这一点是非常冗长,黑客代码。我正在寻找如何以一种干净的、类似蟒蛇的方式来做这件事的指针。我正在使用Python 3.5.2

我有两个有序的单词列表,因此

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

ted_word_bag = ['dependency', 'verb', 'grammar', 'word', 'parser'] 
我想迭代ted_word_bag中的单词,并根据单词在
ting_word_bag
中的位置为每个单词分配一个值。这很简单


更不重要的是,我想反转这些值,这样ting_word_bag列表中的第一个单词值5分,列表中的最后一个单词值1分(基于5个元素的列表)

在本例中,ted_单词_包的总分为8分。5个用于
“单词”
,3个用于
“相关性”

任何关于如何简单快速地完成这项工作的建议都将不胜感激


干杯

通过理解,您可以构建一个dict来存储每个单词的“分数”:

>>> 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}
使用
反向
或其他方法,您可以获得您想要的

接下来,使用字典的
.get(索引,默认值)
,对第二个列表中的每个值求和:

>>> sum(some_dict.get(i,0) for i in ted_word_bag)
8 #with a correct value of some_dict
鉴于以下情况:

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)
正确分配分数:

>>> scores
{'pattern': 1, 'tree': 4, 'number': 2, 'word': 5, 'dependency': 3}
即使如此,最终的总数也是相同的:

>>> total
8

做一个列表理解,按相反顺序列举
ting\u word\u bag

points = [(i,w) for i,w in enumerate(ting_word_bag[::-1])]
对分数的另一种理解:

score =  sum([x[0] for x in points if x[1] in ted_word_bag]) 
此代码执行此操作(假设未找到项目的分数为
0
):

输出:

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

请看,列表中的最后一个单词值1。。。我认为这应该是0,因为你每字减少1。在
ting\u word\u bag
中有6个单词,但您的
ting\u word\u bag
包含6个单词。这是否意味着“信息”值0分?还是“单词”值6,“依赖性”值4?
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))
[(3, 'dependency'), (0, 'verb'), (0, 'grammar'), (5, 'word'), (0, 'parser')]
8