简体   繁体   中英

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)) . The sel_pos is the item's position from the unstable list. How to find the position every time?

Use l.index(n) to find index of n in list l .

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

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

indexNumber = MyList.Index(Element)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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