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程序设计有所帮助。

相关文章

详解Python2.x中对Unicode编码的使用

我确定有很多关于Unicode和Python的说明,但为了方便自己的理解使用,我还是打算再写一些关于它们的东西。   字节流 vs Unicode对象 我们先来用Python定...

PyQt5实现简易计算器

PyQt5实现简易计算器

本文实例为大家分享了PyQt5实现简易计算器的具体代码,供大家参考,具体内容如下 效果图: 界面代码 calc_interface.py # -*- coding: utf-8...

Django自带日志 settings.py文件配置方法

Django settings.py文件配置部分: # logging配置 log_file = '/home/nagain/learn/log' log_file_path = o...

利用aardio给python编写图形界面

利用aardio给python编写图形界面

前阵子在用python写一些小程序,写完后就开始思考怎么给python程序配一个图形界面,毕竟控制台实在太丑陋了。 于是百度了下python的图形界面库,眼花缭乱的一整页,拣了几件有“特...

从Python的源码浅要剖析Python的内存管理

从Python的源码浅要剖析Python的内存管理

Python 的内存管理架构(Objects/obmalloc.c): 复制代码 代码如下:     _____   ______&nb...