简体   繁体   English

如何在python中找到列表项的位置?

[英]How to find the position of an item of a list in python?

I have two lists. 我有两个清单。 The first list has a specific length and the second list has unstable length. 第一个列表具有特定的长度,第二个列表具有不稳定的长度。 I want to assign the items from two lists. 我想从两个列表中分配项目。 I use colour_index = sel_pos%(len(colours)) . 我使用colour_index = sel_pos%(len(colours)) The sel_pos is the item's position from the unstable list. sel_pos是项目在不稳定列表中的位置。 How to find the position every time? 每次如何找到位置?

Use l.index(n) to find index of n in list l . 使用l.index(n)在列表l找到n索引。

>>> x = [1,3,7]
>>> x.index(3)
1
colour_index = selections.index(txt)%(len(colours))

您可以使用Index函数,该函数获取元素并在列表中返回其索引:

indexNumber = MyList.Index(Element)

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

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