在前两篇文章中( 第二组 和 第三组 ),我们讨论了python的基础知识。在本文中,我们将进一步了解python,感受python的强大功能。
null
Python词典
在python中,字典类似于其他语言中的哈希或映射。它由键值对组成。可以通过字典中的唯一键访问该值。
Python3
# Create a new dictionary d = dict () # or d = {} # Add a key - value pairs to dictionary d[ 'xyz' ] = 123 d[ 'abc' ] = 345 # print the whole dictionary print (d) # print only the keys print (d.keys()) # print only values print (d.values()) # iterate over dictionary for i in d : print ( "%s %d" % (i, d[i])) # another method of iteration for index, key in enumerate (d): print (index, key, d[key]) # check if key exist print ( 'xyz' in d) # delete the key-value pair del d[ 'xyz' ] # check again print ( "xyz" in d) |
输出:
{'xyz': 123, 'abc': 345}['xyz', 'abc'][123, 345]xyz 123abc 3450 xyz 1231 abc 345TrueFalse
中断,继续,传入Python
- 休息: 带你走出当前的循环。
- 继续: 结束循环中的当前迭代并移动到下一个迭代。
- 通过: pass语句没有任何作用。它可以在需要语句时使用。但程序不需要任何动作。
它通常用于创建最小类。
Python3
# Function to illustrate break in loop def breakTest(arr): for i in arr: if i = = 5 : break print (i) # For new line print ("") # Function to illustrate continue in loop def continueTest(arr): for i in arr: if i = = 5 : continue print (i) # For new line print ("") # Function to illustrate pass def passTest(arr): pass # Driver program to test above functions # Array to be used for above functions: arr = [ 1 , 3 , 4 , 5 , 6 , 7 ] # Illustrate break print ( "Break method output" ) breakTest(arr) # Illustrate continue print ( "Continue method output" ) continueTest(arr) # Illustrate pass- Does nothing passTest(arr) |
输出:
Break method output1 3 4Continue method output1 3 4 6 7
地图,过滤器,lambda
- 地图: 函数的作用是:将一个函数应用于iterable的每个成员,并返回结果。如果有多个参数,map()将返回一个由元组组成的列表,其中包含所有iterables中的相应项。
- 过滤器: 它接受一个返回True或False的函数,并将其应用于一个序列,只返回该函数返回True的序列中的那些成员的列表。
- 拉姆达: Python提供了创建一个名为lambda function的简单匿名内联函数(内部不允许使用任何语句)的能力。使用lambda和map,一行中可以有两个for循环。
Python3
# python program to test map, filter and lambda items = [ 1 , 2 , 3 , 4 , 5 ] #Using map function to map the lambda operation on items cubes = list ( map ( lambda x: x * * 3 , items)) print (cubes) # first parentheses contains a lambda form, that is # a squaring function and second parentheses represents # calling lambda print ( ( lambda x: x * * 2 )( 5 )) # Make function of two arguments that return their product print (( lambda x, y: x * y)( 3 , 4 )) #Using filter function to filter all # numbers less than 5 from a list number_list = range ( - 10 , 10 ) less_than_five = list ( filter ( lambda x: x < 5 , number_list)) print (less_than_five) |
输出:
[1, 8, 27, 64, 125]2512[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
为了更清楚地了解map、filter和lambda,您可以查看以下示例:
Python3
# code without using map, filter and lambda # Find the number which are odd in the list # and multiply them by 5 and create a new list # Declare a new list x = [ 2 , 3 , 4 , 5 , 6 ] # Empty list for answer y = [] # Perform the operations and print the answer for v in x: if v % 2 : y + = [v * 5 ] print (y) |
输出:
[15, 25]
使用map、filter和lambda可以在两行中执行相同的操作,如下所示:
Python3
# above code with map, filter and lambda # Declare a list x = [ 2 , 3 , 4 , 5 , 6 ] # Perform the same operation as in above post y = list ( map ( lambda v: v * 5 , filter ( lambda u: u % 2 , x))) print (y) |
输出:
[15, 25]
参考资料:
- https://docs.python.org/2/tutorial/controlflow.html#break-循环中的continue语句和else子句
- http://www.u.arizona.edu/~erdmann/mse350/topics/list_comprehension。html
本文由 尼希尔·库马尔·辛格
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END