Python Notes: List

This is to record some functions and attributes of Python I used before.

the length of list:len(list)
the type of list:type(…)

How to get the index of 'bar'?

["foo", "bar", "baz"].index("bar”)

How to find all indices of a repeated elements?

(找出多个元素所有的index位置)
[i for i, v in enumerate(resIndexList) if v==index]

Iteration

for i in range (0,len(list)): 
    print i ,list[i]

# enumerate, get index and text info at the same time
for index,text in enumerate(list)): 
   print index ,text

Remove repeated elements

set(list)
list intersection, union, subset
http://www.yihaomen.com/article/python/323.htm

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容