列表理解是在python中定义和创建列表的一种优雅方法。我们可以像创建数学语句一样仅在一行中创建列表。列表理解的语法更容易掌握。
列表理解通常包括以下部分:
- 输出表达式
- 输入顺序
- 代表输入序列成员的变量, 以及
- 可选谓词部分。
For example :lst=[x ** 2for x in range (1, 11)ifx % 2 == 1] here, x ** 2 is output expression, range (1, 11)is input sequence, x is variable and
if x % 2 == 1 is predicate part.
示例1:
# Python program to demonstrate list comprehension in Python # below list contains square of all odd numbers from
# range 1 to 10
odd_square = [x * * 2 for x in range ( 1 , 11 ) if x % 2 = = 1 ]
print odd_square# for understanding, above generation is same as, odd_square = []
for x in range ( 1 , 11 ):
if x % 2 = = 1 :
odd_square.append(x * * 2 )
print odd_square# below list contains power of 2 from 1 to 8
power_of_2 = [ 2 * * x for x in range ( 1 , 9 )]
print power_of_2# below list contains prime and non-prime in range 1 to 50
noprimes = [j for i in range ( 2 , 8 ) for j in range (i * 2 , 50 , i)]
primes = [x for x in range ( 2 , 50 ) if x not in noprimes]
print primes# list for lowering the characters
print [x.lower() for x in [ "A" , "B" , "C" ]]# list which extracts number
string = "my phone number is : 11122 !!"print ( "\nExtracted digits" )
numbers = [x for x in string if x.isdigit()]
print numbers# A list of list for multiplication table
a = 5
table = [[a, b, a * b] for b in range ( 1 , 11 )]print ( "\nMultiplication Table" )
for i in table:
print i
输出如下:
[1, 9, 25, 49, 81]
[1, 9, 25, 49, 81]
[2, 4, 8, 16, 32, 64, 128, 256]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
['a', 'b', 'c']Extracted digits
['1', '1', '1', '2', '2']Multiplication Table
[5, 1, 5]
[5, 2, 10]
[5, 3, 15]
[5, 4, 20]
[5, 5, 25]
[5, 6, 30]
[5, 7, 35]
[5, 8, 40]
[5, 9, 45]
[5, 10, 50]
【Python列表理解和切片用法介绍】获取列表后, 我们可以使用python的切片运算符(具有以下语法)来获取列表的一部分:
[start : stop : steps]which means that slicing will start from index start
will go up to stop in step of steps.
Default value of start is 0, stop is last index of list
and for step it is 1
So[: 停]从开始到停止索引将切片列表, 并且[开始:]从开始索引到结束将切片列表步骤的负值显示从右到左遍历而不是从左到右遍历, 这就是为什么[::-1]以相反的顺序打印列表。
示例2:
# Let us first create a list to demonstrate slicing
# lst contains all number from 1 to 10
lst = list ( range ( 1 , 11 ))
print (lst)#below list has numbers from 2 to 5
lst1_5 = lst[ 1 : 5 ]
print (lst1_5)#below list has numbers from 6 to 8
lst5_8 = lst[ 5 : 8 ]
print (lst5_8)#below list has numbers from 2 to 10
lst1_ = lst[ 1 : ]
print (lst1_)#below list has numbers from 1 to 5
lst_5 = lst[: 5 ]
print (lst_5)#below list has numbers from 2 to 8 in step 2
lst1_8_2 = lst[ 1 : 8 : 2 ]
print (lst1_8_2)#below list has numbers from 10 to 1
lst_rev = lst[ : : - 1 ]
print (lst_rev)#below list has numbers from 10 to 6 in step 2
lst_rev_9_5_2 = lst[ 9 : 4 : - 2 ]
print (lst_rev_9_5_2)
输出如下:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 4, 5]
[6, 7, 8]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5]
[2, 4, 6, 8]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 8, 6]
我们可以用过滤函数根据提供的某些条件过滤列表lambda表达式作为第一个参数, 列表作为第二个参数, 示例如下所示:
示例3:
import functools#filtering odd numbers
lst = filter ( lambda x : x % 2 = = 1 , range ( 1 , 20 ))
print ( list (lst))#filtering odd square which are divisible by 5
lst = filter ( lambda x : x % 5 = = 0 , [x * * 2 for x in range ( 1 , 11 ) if x % 2 = = 1 ])
print ( list (lst))#filtering negative numbers
lst = filter (( lambda x: x <
0 ), range ( - 5 , 5 ))
print ( list (lst))#implementing max() function, using
print (functools. reduce ( lambda a, b: a if (a>
b) else b, [ 7 , 12 , 45 , 100 , 15 ]))
输出如下:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[25]
[-5, -4, -3, -2, -1]
100
本文由Utkarsh Trivedi提供。如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python语言入门简单介绍
- Python列表理解用法详细介绍
- Python NLTK的词法化用法示例
- Python使用TextBlob进行词法化示例
- Python Kivy线画布(Canvas)用法实例
- Python使用sklearn的线性回归详细指南
- Python如何在Kivy中使用多个KV文件
- Python使用keras进行图像分类项目示例
- Python使用Tkinter的贷款计算器详细介绍