目标是查看两列之间的差异,每列包含相同的行条目,只是在不同的行上;
[
["ENSCAFG00000008901","ENSCAFG00000013762"],
["ENSCAFG00000029470","ENSCAFG00000003029"],
["ENSCAFG00000013782","ENSCAFG00000007249"],
["ENSCAFG00000000806","ENSCAFG00000012468"],
["ENSCAFG00000013341","ENSCAFG00000018167"],
["ENSCAFG00000003376","ENSCAFG00000003376"],
["ENSCAFG00000000812","ENSCAFG00000018164"],
["ENSCAFG00000012468","ENSCAFG00000001591"],
["ENSCAFG00000031786","ENSCAFG00000013782"],
["ENSCAFG00000000803","ENSCAFG00000030793"],
["ENSCAFG00000003029","ENSCAFG00000015177"],
["ENSCAFG00000011565","ENSCAFG00000005750"]
]
这个列表有更多行,所以我不能手工完成。例如,我们在第一列的第11行和第二列的第2行看到ENSCAFG00000003029。我想创建一些输出,显示第一列和第二列之间相对于第一列的行变化。所以在ENSCAFG00000003029的情况下,我想在ENSCAFG00000003029出现在第一列的行之后看到+9(或类似的东西)。
我希望我的目标很明确,这是提出问题的正确位置。
答案 0 :(得分:1)
对于很长的列表来说它不会有效,但这应该有效:
lst = [
["ENSCAFG00000008901","ENSCAFG00000013762"],
["ENSCAFG00000029470","ENSCAFG00000003029"],
["ENSCAFG00000013782","ENSCAFG00000007249"],
["ENSCAFG00000000806","ENSCAFG00000012468"],
["ENSCAFG00000013341","ENSCAFG00000018167"],
["ENSCAFG00000003376","ENSCAFG00000003376"],
["ENSCAFG00000000812","ENSCAFG00000018164"],
["ENSCAFG00000012468","ENSCAFG00000001591"],
["ENSCAFG00000031786","ENSCAFG00000013782"],
["ENSCAFG00000000803","ENSCAFG00000030793"],
["ENSCAFG00000003029","ENSCAFG00000015177"],
["ENSCAFG00000011565","ENSCAFG00000005750"]
]
col_1 = [x[0] for x in lst]
col_2 = [x[1] for x in lst]
idx_offset = [None] * len(col_1)
for col_1_idx, val_1 in enumerate(col_1):
try:
col_2_idx = col_2.index(val_1)
except ValueError:
continue
idx_offset[col_1_idx] = col_2_idx - col_1_idx
简而言之,对于第一列中的每个值,在第二列中找到相同值的索引(如果存在)。获取此索引,并从中扣除第一列中值的索引,以及您的输出。对于第一列中不会出现在第二列中的元素,输出(idx_offset
)将为None
。在这种情况下,输出将变为:
[None, None, 6, None, None, 0, None, -4, None, None, -9, None]
答案 1 :(得分:0)
我认为你必须经过两次。例如,如果您简化假设第一列中的条目是唯一的,则可以执行以下操作:
cols = [['ENSCAFG00000008901', 'ENSCAFG00000013762'],
['ENSCAFG00000029470', 'ENSCAFG00000003029'],
['ENSCAFG00000013782', 'ENSCAFG00000007249'],
['ENSCAFG00000000806', 'ENSCAFG00000012468'],
['ENSCAFG00000013341', 'ENSCAFG00000018167'],
['ENSCAFG00000003376', 'ENSCAFG00000003376'],
['ENSCAFG00000000812', 'ENSCAFG00000018164'],
['ENSCAFG00000012468', 'ENSCAFG00000001591'],
['ENSCAFG00000031786', 'ENSCAFG00000013782'],
['ENSCAFG00000000803', 'ENSCAFG00000030793'],
['ENSCAFG00000003029', 'ENSCAFG00000015177'],
['ENSCAFG00000011565', 'ENSCAFG00000005750']]
positions = dict((x[0], i) for i, x in enumerate(cols))
[positions[x[1]]-i for i, x in enumerate(cols) if x[1] in positions]
输出:
[9, 4, 0, -6]
答案 2 :(得分:0)
l1 =[]
l2 =[]
for item in cols:
l1.append(item[0])
l2.append(item[0])
for item in l1:
if item in l2:
print str(l2.index(item)) + ':' + str(item)