Python列表list解析操作示例【整数操作、字符操作、矩阵操作】

yipeiwu_com6年前Python基础

本文实例讲述了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 OpenCV实现鼠标画框效果

Python OpenCV实现鼠标画框效果

使用Python+OpenCV实现鼠标画框的代码,供大家参考,具体内容如下 # -*-coding: utf-8 -*- """ @Project: IntelligentMan...

python实现的读取网页并分词功能示例

python实现的读取网页并分词功能示例

本文实例讲述了python实现的读取网页并分词功能。分享给大家供大家参考,具体如下: 这里使用分词使用最流行的分词包jieba,参考:https://github.com/fxsjy/j...

JavaScript实现一维数组转化为二维数组

啥也不说了,上代码吧! <!DOCTYPE html> <html lang="en"> <head> <meta charse...

使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”

使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”

本文记录了笔者用 Python 爬取淘宝某商品的全过程,并对商品数据进行了挖掘与分析,最终得出结论。 项目内容 本案例选择>> 商品类目:沙发; 数量:共100页 ...

Python contextlib模块使用示例

看这个模块要先看with as的用法,最常用的方法就是打开一个文件: 复制代码 代码如下: with open(“filename”) as f: f.read() with可以调用一...