Python list 常用操作
2015-06-24 11:23
459 查看
测试版本:
python 2.7获取第一个、最后一个元素
list1 = ["a", "b", "c"] len1 = len(list1) print list1[0] if 0 < len1 else None print list1[-1] if 0 < len1 else None
获取列表最小值、最大值(数字最小,字符串比其他类型大)
list1 = ["a", "v", ["x", "y"], 11, 22] print max(list1) print min(list1)
获取列表最小值、最大值下标
list1 = ["aaa", "bbb", "ccc", "555", "666", 111, 999] print list1.index(max(list1)) print list1.index(min(list1))
遍历获取列表元素下标、值
# 使用列表长度值遍历 list1 = ["aaa", "bbb", "ccc", "555", "666", 111, 999] for i in range(list1): print i, ":", list1[i] # enumerate(sequence,start=0) # 参数sequence是一个可迭代的对象,可以是列表,字典,文件对象等等。enumerate返回的是下标和item组成的元组 for i, v in enumerate(list1): print i, ":", v
原文地址:
http://www.cnblogs.com/zhangbo127/p/4597228.html
参考地址:
http://www.w3cschool.cc/python/python-lists.html
http://blog.sina.com.cn/s/blog_82697f580101cdpp.html
相关文章推荐
- Python装饰器使用实例:验证参数合法性
- Python的字符串处理函数split & join的使用一例
- Python函数式编程指南(四):生成器详解
- python + mysql
- Python安装pip的几种方式
- python模块 - re模块使用示例
- python编辑器对比和推荐
- 学习Python6(临时计算器+字符串编码)
- Python函数式编程指南(三):迭代器详解
- MIT python 第二课第四十分钟 取最小值的例子
- Python如何进行中文注释
- Python函数式编程指南(二):从函数开始
- python 中面向对象编程简单总结1
- Python函数式编程指南(一):函数式编程概述
- 我的Python与Android学习之路
- 关于Python中闭包与装饰器的理解
- python学习笔记一
- 【转】python3.3.3右键菜单Edit with IDLE不能启动,打开IDLE后新建和打开闪退问题
- python字典
- Python实现LRU算法的2种方法