Python中关于列表list的整数操作与字符操作以及矩阵操作的实例分析


本文摘自php中文网,作者黄舟,侵删。

这篇文章主要介绍了Python列表list解析操作,结合实例形式分析了Python列表针对整数、字符及矩阵的解析操作实现技巧,需要的朋友可以参考下

本文实例讲述了Python列表list解析操作。分享给大家供大家参考,具体如下:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

#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中关于列表list的整数操作与字符操作以及矩阵操作的实例分析的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python什么时候出现的

Python如何提高运行速度

Python是一种什么类型的编程语言

anaconda多版本和环境下Python的不同配置操作方法

Python类的继承是什么?类的继承有什么样的规则?

Python如何读取txt文件

Python中单下划线和双下划线有什么区别

Python是汇编语言吗

Python 2.7 pandas 中的read_excel详解

Python可以做app么

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...