Python编程语言使一切变得简单而直接。 Ëffective利用其内置库可以节省大量的时间和帮助更快的意见,在做竞争程序。以下是一些有用的技巧,每个Pythonist都应该唾手可得:
- 使用map()函数将数字转换为数字列表:
以下是将给定数字转换为数字列表的实现:
Python3
# Python program to convert a
# number to a list of digits
# Given number
n = 123456
# Stores the list of digits
lis = list(map(int, str(n)))
# Print the digits
print(lis)
Python3
# Python program to convert
# a sentence to a list of words
# Given sentece
sentence = "GeeksforGeeks is the computer science portal for geeks"
# Convert the sentence
# into a list of words
lis = list(sentence.split())
# Print the list of words
print(lis)
Python3
# Python program to take
# newline-separated input
# in the form of a list
# Given input
n = int(input())
lis = [int(input()) for _ in range(n)]
Python3
# Python program to demonstrate gcd() function
import math
a = 8
b = 24
# Print the GCD of a and b
print(math.gcd(a, b))
Python3
# Python program to print all
# permutations using library function
from itertools import permutations
# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3])
# Print the obtained permutations
for i in list(perm):
print (i)
Python3
# Python program to print
# a string given number of times
# Given string
str ="India"
# Print the string 2 times
print(str * 2)
Python3
# Python program to print a list with spaces without loop
lis = [1, 2, 3, 4]
# Printing list elements with spaces
print(*lis)
Python3
# Python program to convert a
# binary string to its decimal
# equivalent using int() function
# Given string
binary = "1010"
# Print decimal equivalent
print(int(binary, 2))
Python3
# Python program to print a sorted list with
# spaces using sorted() function
lis = [6, 2, 7, 3, 4]
# Print the sorted sequence
print(*sorted(lis))
Python3
# Python program to print common elements in
# both the list using intersection() function
array1 = [4, 5, 6, 7, 8]
array2 = [3, 4, 5, 1, 72]
# Print the common elements
print(set(array1).intersection(set(array2)))
输出:
[1, 2, 3, 4, 5, 6]
- 转换一个句子译成采用分体式(单词列表)函数:下面是一个句子转换成一个单词列表的实现:
Python3
# Python program to convert
# a sentence to a list of words
# Given sentece
sentence = "GeeksforGeeks is the computer science portal for geeks"
# Convert the sentence
# into a list of words
lis = list(sentence.split())
# Print the list of words
print(lis)
输出:
['GeeksforGeeks', 'is', 'the', 'computer', 'science', 'portal', 'for', 'geeks']
- 将以换行符分隔的整数作为List :可以使用List Comprehension以List的形式获取来自控制台的以换行符分隔的输入。下面是将以换行符分隔的整数的输入作为列表的实现:
Python3
# Python program to take
# newline-separated input
# in the form of a list
# Given input
n = int(input())
lis = [int(input()) for _ in range(n)]
- 计算两个数字的GCD / HCF :可以使用Python Math Module提供的内置函数gcd()在Python计算两个数字的Gcd。
下面是演示gcd()函数:
Python3
# Python program to demonstrate gcd() function
import math
a = 8
b = 24
# Print the GCD of a and b
print(math.gcd(a, b))
输出:
8
输出
8
- 打印数组的排列:可以使用内置的permutations()有效地生成数组的所有排列 来自itertools包的方法。此方法需要一个列表作为输入并返回元组的对象列表,其中包含的所有排列。
下面是该方法的实现:
Python3
# Python program to print all
# permutations using library function
from itertools import permutations
# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3])
# Print the obtained permutations
for i in list(perm):
print (i)
输出:
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
- 多次打印一个字符串而不循环:以下是使用字符串乘法技术多次打印一个字符串而不循环的实现:
Python3
# Python program to print
# a string given number of times
# Given string
str ="India"
# Print the string 2 times
print(str * 2)
输出:
IndiaIndia
输出
IndiaIndia
- 要打印带有空格且没有循环的列表:通过使用Python的*运算符,可以在不运行循环的情况下打印列表。
下面是打印带有空格且没有循环的列表的实现:
Python3
# Python program to print a list with spaces without loop
lis = [1, 2, 3, 4]
# Printing list elements with spaces
print(*lis)
输出:
1 2 3 4
输出
1 2 3 4
- 转换二进制字符串为十进制:一个二进制字符串可被转换为使用内置的INT()函数相应的十进制数。
下面是上述方法的实现:
Python3
# Python program to convert a
# binary string to its decimal
# equivalent using int() function
# Given string
binary = "1010"
# Print decimal equivalent
print(int(binary, 2))
输出:
10
输出
10
- 要打印带有空格的排序列表:在Python,使用内置方法sorted()并使用*符号来打印带有空格的列表,对任何序列进行排序非常容易。 Sorted ()对任何序列(列表,元组)进行排序,并始终以排序方式返回包含元素的列表,而无需修改原始序列。
下面是打印带有空格的排序列表的实现:
Python3
# Python program to print a sorted list with
# spaces using sorted() function
lis = [6, 2, 7, 3, 4]
# Print the sorted sequence
print(*sorted(lis))
输出:
2 3 4 6 7
输出
2 3 4 6 7
- 在两个数组中查找公共元素:在两个数组/列表中的公共元素可以使用集以更简单的方式完成。 Python的交集()函数提供了两个数组/列表中的公共元素。
以下是演示交集()函数:
Python3
# Python program to print common elements in
# both the list using intersection() function
array1 = [4, 5, 6, 7, 8]
array2 = [3, 4, 5, 1, 72]
# Print the common elements
print(set(array1).intersection(set(array2)))
输出:
{4, 5}
输出
{4, 5}
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。