Home >Backend Development >Python Tutorial >Example analysis of integer operations, character operations and matrix operations on lists in Python
This article mainly introduces the Python list parsing operation, and analyzes the Python list parsing operation implementation skills for integers, characters and matrices in the form of examples. Friends in need can refer to the following
The examples in this article describe Python list parsing operation. Share it with everyone for your reference, the details are as follows:
#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
Running results:
The above is the detailed content of Example analysis of integer operations, character operations and matrix operations on lists in Python. For more information, please follow other related articles on the PHP Chinese website!