Python教程-Python的惊人技巧
在这个教程中,我们将学习Python语言在编程中有多么出色。我们将讨论一些令人惊叹的技巧,使Python在其他编程语言中脱颖而出。
Python的技巧
以下是Python的一些令人惊叹的技巧,可以让用户和开发人员的工作变得更加轻松:
1. 列表推导(List Comprehensions): 这是一种最好和高效的技术,用于消除不必要的程序行。
列表推导包括以下部分:
- 输出表达式
- 输入序列
- 输入序列中的一个变量表示的成员
- 可选的谓词部分。
示例:
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
结论
在本教程中,我们讨论了Python的8个不同的令人惊叹和酷炫的技巧,这使得它对于开发人员和初学者来说更容易使用。