栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python列表list解析操作示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python列表list解析操作示例

本文实例讲述了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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/32395.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号