在Python中按索引比较列表值

时间:2022-11-09 22:51:16

I need to see if 2 items from a list appears in another list, and if they do, compare the items by their position in the other list. Example in pseudo code:

我需要查看一个列表中的2个条目是否出现在另一个列表中,如果它们做了,则将它们与其他列表中的位置进行比较。在伪代码示例:

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

Sample Narrative Description:

样本叙事描述:

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

Can someone please point me in the right direction? I've been looking at numerous list, array and .index tutorials but nothing seems to answer my question

谁能给我指出正确的方向吗?我已经看了很多的列表,数组和。index教程,但是似乎没有什么能回答我的问题

3 个解决方案

#1


3  

Convert your list2 to a dictionary that encodes the position given an item:

将你的清单2转换成字典,在给定条目的位置进行编码:

dic2 = dict((item,i) for i,item in enumerate(list2))

Now you can test for something being in the list by using x in dic2 and y in dic2 and use dic2[x] to get it's index in the list.

现在可以通过在dic2中使用x,在dic2中使用y,并使用dic2[x]获取列表中的索引来测试列表中的某物。

Edit: It goes against my better instincts, but here's the complete code. The first part is using what I showed above, turning a simple list into a lookup for the index. Next comes the standard if unintuitive way of initializing a 2D list. This is followed by your loops, using the ever handy enumerate function to assign an index to each name in the list.

编辑:这违背了我的直觉,但这里有完整的代码。第一部分使用了上面所示的方法,将一个简单的列表转换为索引的查找。接下来是初始化2D列表的标准方法(如果不是很直观的话)。接下来是循环,使用方便的enumerate函数为列表中的每个名称分配索引。

Names = ['John', 'James', 'Barry', 'Greg', 'Jenny']
Results1 = ['James', 'Barry', 'Jenny', 'Greg', 'John']
Results2 = ['Barry', 'Jenny', 'Greg', 'James', 'John']

Order1 = dict((name,order) for order,name in enumerate(Results1))
Order2 = dict((name,order) for order,name in enumerate(Results2))

score = [[0]*len(Names) for y in range(len(Names))]

for i,name1 in enumerate(Names):
    for j,name2 in enumerate(Names):
        if name1 in Order1 and name2 in Order1 and Order1[name1] > Order1[name2] and name1 in Order2 and name2 in Order2 and Order2[name1] > Order2[name2]:
            score[i][j] = 1

#2


2  

lis1=[1,2,3,4,5,6,7,8]
num1=lis1[1]
num2=lis1[4]
lis2=[11,12,13,14,2,7,5,34]
if num1 in lis2 and num2 in lis2:
    if lis2.index(num1)>lis2.index(num2):
        #do something here
    else:
        #do something else

#3


1  

IF I understand what you are trying to do, here is an approach:

如果我理解你的意图,这里有一个方法:

score={}

Names = ["John", "James", "Barry", "Greg", "Jenny"]
Results1 = ["James", "Barry", "Jenny", "Greg", "John"]
Results2 = ["Barry", "Jenny", "Greg", "James", "John"]

r1dict={name:i for i,name in enumerate(Results1)}
r2dict={name:i for i,name in enumerate(Results2)}

for i, ni in enumerate(Names):
    for j, nj in enumerate(Names):
        if r1dict[ni] > r2dict[nj]:
            score[(i,j)]=1

print(score)  

Prints:

打印:

{(0, 1): 1, (3, 2): 1, (4, 4): 1, (3, 3): 1, (2, 2): 1, 
 (4, 2): 1, (0, 3): 1, (0, 4): 1, (3, 4): 1, (0, 2): 1}

#1


3  

Convert your list2 to a dictionary that encodes the position given an item:

将你的清单2转换成字典,在给定条目的位置进行编码:

dic2 = dict((item,i) for i,item in enumerate(list2))

Now you can test for something being in the list by using x in dic2 and y in dic2 and use dic2[x] to get it's index in the list.

现在可以通过在dic2中使用x,在dic2中使用y,并使用dic2[x]获取列表中的索引来测试列表中的某物。

Edit: It goes against my better instincts, but here's the complete code. The first part is using what I showed above, turning a simple list into a lookup for the index. Next comes the standard if unintuitive way of initializing a 2D list. This is followed by your loops, using the ever handy enumerate function to assign an index to each name in the list.

编辑:这违背了我的直觉,但这里有完整的代码。第一部分使用了上面所示的方法,将一个简单的列表转换为索引的查找。接下来是初始化2D列表的标准方法(如果不是很直观的话)。接下来是循环,使用方便的enumerate函数为列表中的每个名称分配索引。

Names = ['John', 'James', 'Barry', 'Greg', 'Jenny']
Results1 = ['James', 'Barry', 'Jenny', 'Greg', 'John']
Results2 = ['Barry', 'Jenny', 'Greg', 'James', 'John']

Order1 = dict((name,order) for order,name in enumerate(Results1))
Order2 = dict((name,order) for order,name in enumerate(Results2))

score = [[0]*len(Names) for y in range(len(Names))]

for i,name1 in enumerate(Names):
    for j,name2 in enumerate(Names):
        if name1 in Order1 and name2 in Order1 and Order1[name1] > Order1[name2] and name1 in Order2 and name2 in Order2 and Order2[name1] > Order2[name2]:
            score[i][j] = 1

#2


2  

lis1=[1,2,3,4,5,6,7,8]
num1=lis1[1]
num2=lis1[4]
lis2=[11,12,13,14,2,7,5,34]
if num1 in lis2 and num2 in lis2:
    if lis2.index(num1)>lis2.index(num2):
        #do something here
    else:
        #do something else

#3


1  

IF I understand what you are trying to do, here is an approach:

如果我理解你的意图,这里有一个方法:

score={}

Names = ["John", "James", "Barry", "Greg", "Jenny"]
Results1 = ["James", "Barry", "Jenny", "Greg", "John"]
Results2 = ["Barry", "Jenny", "Greg", "James", "John"]

r1dict={name:i for i,name in enumerate(Results1)}
r2dict={name:i for i,name in enumerate(Results2)}

for i, ni in enumerate(Names):
    for j, nj in enumerate(Names):
        if r1dict[ni] > r2dict[nj]:
            score[(i,j)]=1

print(score)  

Prints:

打印:

{(0, 1): 1, (3, 2): 1, (4, 4): 1, (3, 3): 1, (2, 2): 1, 
 (4, 2): 1, (0, 3): 1, (0, 4): 1, (3, 4): 1, (0, 2): 1}