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批量处理文件或文件夹

本文实例为大家分享了python批量处理文件或文件夹的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- import os,shutil impor...

浅谈Python 列表字典赋值的陷阱

今天在用python刷leetcode 3Sum problem时,调入到了一个大坑中,检查半天并没有任何逻辑错误,但输出结果却总是不对,最终通过调试发现原来python中list和di...

tensorflow: 查看 tensor详细数值方法

问题 tensor详细数值 不能直接print打印: import tensorflow as tf x = tf.constant(1) print x 输出: Tensor...

执行Django数据迁移时报 1091错误及解决方法

执行Django数据迁移时报 1091错误及解决方法

问题描述   今天在Pycharm 中的Terminal下,执行数据迁移操作时,第一步: Python manage.py makemigrations ,是没有任何问题,但就是在执行真...

pandas中去除指定字符的实例

pandas中去除指定字符的实例

例表: 假如想要去掉表中的‘#',‘;'而且以‘#'和‘;'为分割线切割数据: #将dfxA_2的每一个分隔符之间的数据提出来 col1=dfxA_2['travel_seq']...