令人惊叹的 Python 技巧
在这个教程中,我们将学习 Python 这门编程语言的强大之处。我们将讨论一些令人惊叹的技巧,使 Python 成为其他语言中最好的选择。
Python 的技巧
以下是一些令人惊叹的 Python 技巧,可以方便用户和开发人员的工作:
1. 列表推导式: 这是一种最好且高效的技术,可以消除编写无意义程序行的负担。
列表推导式由以下部分组成:
- 输出表达式
- 输入序列
- 由变量表示的输入序列的成员
- 可选的谓词部分
示例:
import functools as FT
# First, filter odd numbers
list_1 = filter(lambda K : K % 2 == 1, range(10, 30))
print ("List: ", list(list_1))
# Then we will filter the odd square which is divisible by 5
list_1 = filter(lambda K : K % 5 == 0,
[K ** 2 for K in range(1, 11) if K % 2 == 1])
print ("ODD SQUARE WHICH IS DIVISIBLE BY 5: ", list(list_1))
# Here, we will filter negative numbers
list_1 = filter((lambda K : K < 0), range(-10, 10))
print ("Filter negative numbers: ", list(list_1))
# Now, implement by using the max() function
print ("Maximum Number in the List: ")
print (FT.reduce(lambda S, T: S if (S > T) else T, [14, 11, 65, 110, 105]))
输出:
List: [11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
ODD SQUARE WHICH IS DIVISIBLE BY 5: [25]
Filter negative numbers: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Maximum Number in the List:
110
2. 打印列表: 列表不会按照我们的要求打印出来,它们总是会被打印成不必要的方括号和单引号。但是在Python中,我们可以通过使用字符串的join方法有效地打印列表。”join方法”可以将列表转换为字符串,将每个项目分类为字符串,并用join方法所用的字符串连接它们。
示例:
# First declare the list:
ABC = ['LPG', 'WWF', 'XYZ', 'MPG']
# Then, we will print the list:
print ("The Simple List: ", ABC)
# HEre, we will Print the list by using join method
print ('The List by using join method: %s' % ', ' .join(ABC))
# we can directly apply Join Function on the List:
print ('Directly applying the join method: ', (", " .join(ABC)))
输出:
The Simple List: ['LPG', 'WWF', 'XYZ', 'MPG']
The List by using join method: LPG, WWF, XYZ, MPG
Directly applying the join method: LPG, WWF, XYZ, MPG
3. 转置矩阵: 在Python中,用户可以将矩阵实现为嵌套列表,即列表内的列表。列表的每个元素都被视为矩阵的一行。
示例:
M_1 = [[5, 3], [1, 2], [9, 8]]
print ("Matrix 1: ")
for row in M_1 :
print (row)
rez_1 = [[M_1[K][L] for K in range(len(M_1))] for L in range(len(M_1[0]))]
print ("\n")
print ("Matrix 2: ")
for row in rez_1:
print (row)
输出:
Matrix 1:
[5, 3]
[1, 2]
[9, 8]
Matrix 2:
[5, 1, 9]
[3, 2, 8]
4. 将列表分为”N”组: 用户可以使用iter()函数作为序列的迭代器。
示例:
# First, we will Declare the list:
LIST_1 = ['E_1', 'E_2', 'E_3', 'E_4', 'E_5', 'E_6']
partition_1 = list(zip (*[iter(LIST_1)] * 2))
print ("List after partitioning into different of groups of two elements: ", partition_1)
输出:
List after partitioning into different of groups of two elements: [('E_1', 'E_2'), ('E_3', 'E_4'), ('E_5', 'E_6')]
解释:
在上面的代码中,我们使用了”[iter(LIST_1)] * 2″,它生成了包含’LIST_1[]’列表中两个元素的不同组。也就是说,将使用第一个列表的元素生成长度为两个的列表。
5. 同时打印多个列表项
示例:
list_1 = [11, 13, 15, 17]
list_2 = [12, 14, 16, 18]
# Here, we will use zip() function which will take 2 equal length list
# and then merge them together into pairs
for K, L in zip(list_1, list_2):
print (K, L)
输出:
11 12
13 14
15 16
17 18
6. 将字符串作为输入并将其转换为列表:
示例:
# Reading a string from input as int format
# after splitting it's elements by white-spaces
print ("Input: ")
formatted_list_1 = list(map (int, input().split()))
print ("Output as Formatted list: ", formatted_list_1)
输出:
Input:
10 12 14 16 18 20 22
Output as Formatted list: [10, 12, 14, 16, 18, 20, 22]
7. 将列表的列表转换为单个列表:
示例:
# importing the itertools
import itertools as IT
# Declaring the list geek
LIST_1 = [[1, 2], [3, 4], [5, 6]]
# chain.from_iterable() function will return the
# elements of nested list
# and iterate it from first list
# of iterable till the last
# end of the list
list_2 = list(IT.chain.from_iterable(LIST_1))
print ("Iterated list of 'LIST_1': ", list_2)
输出:
Iterated list of 'LIST_1': [1, 2, 3, 4, 5, 6]
8. 打印重复的字符: 假设我们的任务是打印类似于”122333444455555666666″的模式。我们可以在Python中轻松地打印出这个模式,而不使用for循环。
示例:
print ("1" + "2" * 2 + "3" * 3 + "4" * 4 + "5" * 5 + "6" * 6)
输出:
122333444455555666666
结论
在本教程中,我们讨论了8个不同的令人惊叹和酷炫的Python技巧,使得开发者和初学者都更容易使用。