我需要查看一个列表中的两个项目是否出现在另一个列表中,如果出现,请按它们在另一个列表中的位置比较它们。伪代码示例:
j=0
for x in mylist #loop through the list
i=0
for y in mylist #loop through the list again to compare items
if index of mylist[j] > index of mylist[i] in list1 and list2:
score[i][j] = 1 #writes the score to a 2d array(numpy) called score
i=i+1
else:
score[i][j]=0
i=i+1
j=j+1
叙述性描述示例:
Names = [John, James, Barry, Greg, Jenny]
Results1 = [James, Barry, Jenny, Greg, John]
Results2 = [Barry, Jenny, Greg, James, John]
loop through Names for i
loop through Names for j
if (index value for john) > (index value for james) in results1 and
(index value for john) > (index value for james) results2:
score[i][j] = 1
有人能告诉我正确的方向吗?我看了很多列表、数组和索引教程,但似乎没有什么能回答我的问题
将
list2
转换为对给定项的位置进行编码的字典:现在可以使用
x in dic2 and y in dic2
测试列表中的内容,并使用dic2[x]
获取列表中的索引。编辑:这违背了我的直觉,但这里是完整的代码。第一部分是使用我上面展示的内容,将一个简单的列表转换为索引的查找。接下来是初始化2D列表的标准if-unintive方法。接下来是循环,使用非常方便的
enumerate
函数为列表中的每个名称分配一个索引。如果我理解您的意图,这里有一个方法:
印刷品:
相关问题 更多 >
编程相关推荐