在python中遍历两个列表
我是python编程新手,很难遍历两个列表。我的两个列表的格式如下:在python中遍历两个列表,python,list,Python,List,我是python编程新手,很难遍历两个列表。我的两个列表的格式如下: L = [['cat', '1', '5'], ['cat', '7', '15'],['cat', '17', '20']] A = [['coordOne', '1', '3'],['coordTwo', '8', '9'],['coordThree', '11', '13'],['coordFour', '18', '21']] for i in range(len(L)): 每个列表中的两个数字是起始坐标和结束坐标
L = [['cat', '1', '5'], ['cat', '7', '15'],['cat', '17', '20']]
A = [['coordOne', '1', '3'],['coordTwo', '8', '9'],['coordThree', '11', '13'],['coordFour', '18', '21']]
for i in range(len(L)):
每个列表中的两个数字是起始坐标和结束坐标,我想看看在比较两个列表时,起始坐标和结束坐标之间是否有重叠,然后将信息保存到新列表中。程序运行后,我所需的列表将输出:
newList:[['cat', 'coordOne'],['cat', 'coordTwo', 'coordThree'], ['cat', 'coordFour']]
到目前为止,我的代码是
newList = []
for i in range(len(L)):
for j in range (len(A)):
if i[1] >= j[1] and i[1] <= j[2] or i[2] >= j[1] and i[2] <= j[2] or i[1] <= j[1] and i[2] >= j[2] and j[2] >= i[2] or i[1] <= j[1] and i[2] >= j[2]:
newList.append(L[i][0], A[j][0])
print (newList)
newList=[]
对于范围内的i(len(L)):
对于范围内的j(len(A)):
如果i[1]>=j[1]和i[1]=j[1]和i[2]=i[2]或i[1]=j[2]:
追加(L[i][0],A[j][0])
打印(新列表)
我收到一个错误,“int”对象不可调用
i
和j
是int
s。在条件中使用L[i][…]
和A[j][…]
for i in range(len(L)):
for j in range (len(A)):
i
和j
这两种类型都是int
,如果希望它们是L
和A
中的元素,请使用:
for i in L:
for j in A:
因此,改变:
newList.append(L[i][0], A[j][0])
致:
代码现在运行,但是结果不是预期的,这将需要更多的工作。我收到一个不同的错误:
索引器:列表索引超出范围。
。发生这种情况的原因是该测试:
if [i][1] >= [j][1]
[i]
是一个仅包含i
的列表,您可以尝试访问不存在的索引1(它只有一个元素,位于索引0处)。还有一些东西是i[1]
和j[1]
,它们将与“int
对象不可下标”中断。你的意思似乎是:
if L[i][j] >= A[j][1]
修复所有这些问题会产生另一个错误:
Traceback (most recent call last):
File "test.py", line 8, in <module>
newList.append(L[i][0], A[j][0])
TypeError: append() takes exactly one argument (2 given)
但是有更好的办法。首先,像这样迭代:
L = [['cat', '1', '5'], ['cat', '7', '15'],['cat', '17', '20']]
A = [['coordOne', '1', '3'],['coordTwo', '8', '9'],['coordThree', '11', '13'],['coordFour', '18', '21']]
for i in range(len(L)):
几乎总是一种反模式。您只能使用i
和j
对这两个列表进行索引;只要做:
for l in L:
(但请选择更好的变量名-通常将列表命名为复数,如things
和dofor things in things:
)
您有两个嵌套循环来考虑<代码> L>代码>的每一个元素对<代码> A/<代码>的每个元素。这称为笛卡尔积,惯用的方法是使用:
您可能还想考虑使用一个这样的方法,以便您可以给每个列表中的对象命名,这样您就可以测试诸如<>代码>如果L.Stc>= ActS:。这将使您更容易准确地解释代码正在执行的操作,以便您能够正确地处理您的条件。
您可以使用“zip”函数L = [['cat', '1', '5'], ['cat', '7', '15'],['cat', '17', '20']]
A = [['coordOne', '1', '3'],['coordTwo', '8', '9'],['coordThree', '11', '13'],['coordFour', '18', '21']]
newList = []
for (i,j) in zip(L, A):
if i[1] >= j[1] and i[1] <= j[2] or i[2] >= j[1] and i[2] <= j[2] or i[1] <= j[1] and i[2] >= j[2] and j[2] >= i[2] or i[1] <= j[1] and i[2] >= j[2]:
newList.append( (i[0], j[0]) )
L=['cat','1','5'],['cat','7','15'],['cat','17','20']]
A=['coordOne','1','3'],['coordTwo','8','9'],['coordThree','11','13'],['coordFour','18','21']
新列表=[]
对于zip(L,A)中的(i,j):
如果i[1]>=j[1]和i[1]=j[1]和i[2]=i[2]或i[1]=j[2]:
追加((i[0],j[0]))
您的比较必须与整数比较,注意'2'<'12'==False
您可以更改复杂条件以仅测试边界条件:
if int(i[1]) <= int(j[2]) and int(j[1]) <= int(i[2]):
是的,我的意思是,我[1]>=j[1]和我[1]你期望我和j是什么?
range
返回什么?范围(6)内i的会发生什么情况:打印i
?这不会将L中的每个元素与A中的每个元素进行比较,您只会将L[0]与A[0]进行检查,将L[1]与A[1]进行检查,以此类推(A[4]永远不会进行比较)。
if int(i[1]) <= int(j[2]) and int(j[1]) <= int(i[2]):
if max(int(i[1]), int(j[1])) <= min(int(i[2]), int(j[2])):
>>> [(l, [a for a, a_start, a_end in A
... if int(l_start) <= int(a_end) and int(a_start) <= int(l_end)])
... for l, l_start, l_end in L]
[('cat', ['coordOne']),
('cat', ['coordTwo', 'coordThree']),
('cat', ['coordFour'])]
[['cat', 'coordOne'], ['cat', 'coordTwo'], ['cat', 'coordThree'], ['cat', 'coordFour']]