Python列表list解析操作示例【整数操作、字符操作、矩阵操作】,pythonlist
Python列表list解析操作示例【整数操作、字符操作、矩阵操作】,pythonlist
本文实例讲述了Python列表list解析操作。分享给大家供大家参考,具体如下:
#coding=utf8 print ''''' Python在一行中使用一个for循环将所有值放到一个列表中。 列表解析的语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable if cond_expr] ----------------------------------------------------------------- ''' print "把0到8的数字依次加上五,并把结果值放在linList中" intList=[x+5 for x in range(8)] for ele in intList: print ele, print print "从0到8的数字中挑出奇数,并把奇数进行乘方操作,结果保存在powerLIst" powerList=[x **2 for x in range(8) if x%2] for pl in powerList: print pl, print print "把字符串ewang转换成大写字母,并把结果保存在upperList中" upperList=[char.upper() for char in "ewang" ] for up in upperList: print up, print print ''''' 把字符串EwAaNg中的大写字母转换成小写,并记录相应的索引的值。 把需要转换的字母和索引值保存在matrixList ''' str='EwAaNg' matrixList=[(char.lower(),index) for char in str if char.isupper() for index in range(len(str)) if str[index].isupper() and str[index]==char] for mat in matrixList: print mat, print
运行结果:
更多Python相关内容感兴趣的读者可查看本站专题:《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
相关内容
- Python学习入门之区块链详解,python区块详解
- Python简单定义与使用字典dict的方法示例,pythondict
- python基础之入门必看操作,python入门必看
- 老生常谈Python之装饰器、迭代器和生成器,老生常谈
- Python列表和元组的定义与使用操作示例,python示例
- Python中Selenium模拟JQuery滑动解锁实例,seleniumjquery
- Python基于tkinter模块实现的改名小工具示例,pythontkint
- Python开发SQLite3数据库相关操作详解【连接,查询,插入
- Python判断文件或文件夹是否存在的三种方法,
- Python更新数据库脚本两种方法及对比介绍,python两种方
评论关闭