简体   繁体   English

python的列表方法`index()`是否可以在列表中找到列表元素

[英]can python's list method `index()` find a list element in a list

update 0 更新0

My test code was very naive and I have to enhance it as follows ... 我的测试代码非常幼稚,我必须对其进行如下增强...

n=2
times=intervals([9,30],[11,30])
u=[]
v=[]

for t in times:
    u+= [[t[0],t[1]]]
    for i in range(0,n):
       t.append("")
    t[3]=3
    v+=[t]
    print "t:",t

print "v:",v

print "u.index:",u.index([10, 30])

... so from my test code I need to use the value of variable v for variable times and I need to use the result of u.index([10, 30]) for my index. ...因此,从测试代码中,我需要将变量v的值用于可变times并且需要将u.index([10, 30])用于索引。 BrenBarn finally got through to me, I hope. 我希望BrenBarn终于联系了我。

My test code that follows ... 我的测试代码如下...

n=2
times=intervals([9,30],[11,30])
for t in times:
    for i in range(0,n):
       t.append("")
    t[3]=3
    print t

... produces the following print result ...产生以下打印结果

[9, 30, '09:30AM', 3, '']
[10, 0, '10:00AM', 3, '']
[10, 30, '10:30AM', 3, '']
[11, 0, '11:00AM', 3, '']

Can the index() method be used to find, for example, the index in list t of the "element" [10, 30, ... ]? 例如,可以使用index()方法查找“元素” [10、30,...]的列表t中的索引吗? Or do I need to construct the integer list [930,100,1030,110] and index() that list, instead? 还是我需要构造整数列表[930,100,1030,110]和该列表的index()? Or are there other suggestions? 还是有其他建议?

Did you even try it, or am I misunderstanding your question? 您甚至尝试过,还是我误解了您的问题?

>>> t = [[9, 30, '09:30AM', 3, ''],
... [10, 0, '10:00AM', 3, ''],
... [10, 30, '10:30AM', 3, ''],
... [11, 0, '11:00AM', 3, '']]
>>> t.index([10, 30, '10:30AM', 3, ''])
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM