Python|第 4 组(字典, Python中的关键字)
在之前的两篇文章(Set 2 和 Set 3)中,我们讨论了Python的基础知识。在本文中,我们将更多地了解Python ,感受Python的强大。
Python中的字典
在Python中,字典类似于其他语言中的 hash 或 maps。它由键值对组成。该值可以通过字典中的唯一键访问。
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)
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)
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)
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)
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)
输出:
{'xyz': 123, 'abc': 345}
['xyz', 'abc']
[123, 345]
xyz 123
abc 345
0 xyz 123
1 abc 345
True
False
在Python中中断、继续、传递
- break:将你带出当前循环。
- continue:结束循环中的当前迭代并移动到下一个迭代。
- pass: 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 output
1 3 4
Continue method output
1 3 4 6 7
地图、过滤器、λ
- map: map()函数将一个函数应用于 iterable 的每个成员并返回结果。如果有多个参数,则 map() 返回一个由元组组成的列表,其中包含来自所有可迭代项的相应项。
- filter:它接受一个返回 True 或 False 的函数并将其应用于一个序列,返回一个列表,该列表仅包含该函数返回 True 的序列成员。
- lambda: Python提供了创建称为 lambda 函数的简单(内部不允许使用语句)匿名内联函数的能力。使用 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]
25
12
[-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://文档。 Python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
- http://www.u.arizona.edu/~erdmann/mse350/topics/list_comprehensions.html