📜  惊人的Python hack

📅  最后修改于: 2022-05-13 01:55:08.700000             🧑  作者: Mango

惊人的Python hack

Python确实是最聪明和最流行的语言之一。这里有一些很酷的技巧,它们使Python在所有其他语言中都非常出色。

  • 列表推导:列表推导是摆脱编写不必要的代码行的最佳且有效的技术。阅读文章以了解更多信息。
  • 打印清单:根据用户要求不打印清单。它们总是打印在不需要的方括号和单引号中。但是有一个简单的解决方案可以通过使用字符串的连接方法来有效地打印列表。
    join 方法通过将每个项目转换为字符串并将它们与调用 join 的字符串连接,将列表转换为字符串
Python
# Declaring the list geek
geek = ['Geeks', 'Programming', 'Algorithm', 'Article']
   
# Directly printing the list
print ("Simple List:", geek)
   
# Printing the list by join method
print ('List by using join method: %s' % ', ' .join(geek))
   
# Direct use of join method
print ('Direct apply the join method:',(", " .join(geek)))


Python3
# Declaring the list geek
geek = ['Sun', 'Flowers', 'Peoples', 'Animals', 'Day', 'Night']
 
partition = list(zip (*[iter(geek)] * 2))
print (partition)


Python
list1 = [1, 3, 5, 7]
list2 = [2, 4, 6, 8]
 
# Here zip() function takes two equal length list and merges them
# together in pairs
for a, b in zip(list1,list2):
    print (a, b)


Python3
# Reads a string from input and type case them to int
# after splitting to white-spaces
 
formatted_list = list(map(int, input().split()))
print(formatted_list)


Python3
# import the itertools
import itertools
 
# Declaring the list geek
geek = [[1, 2], [3, 4], [5, 6]]
 
# chain.from_iterable() function returns the
# elements of nested list
# and iterate from first list
# of iterable till the last
# end of the list
 
lst = list(itertools.chain.from_iterable(geek))
print(lst)


Python
# + used for string concatenation
# To repeat the character n times, just multiply n 
# with that character  
print ("G" + "e"*5 + "k"*4 + "s"*2)


Output: 
Simple List: ['Geeks', 'Programming', 'Algorithm', 'Article']
List by using join method: Geeks, Programming, Algorithm, Article
Direct apply the join method: Geeks, Programming, Algorithm, Article

酷拉链技巧

  • 转置矩阵:您可以在此处阅读有关此内容的信息。
  • 将列表分成 N 组:我们使用 iter() 作为序列上的迭代器。

Python3

# Declaring the list geek
geek = ['Sun', 'Flowers', 'Peoples', 'Animals', 'Day', 'Night']
 
partition = list(zip (*[iter(geek)] * 2))
print (partition)
Output: 
[('Sun', 'Flowers'), ('Peoples', 'Animals'), ('Day', 'Night')]

解释: [iter(geek)] * 2 生成一个包含 2 项 geek[] 列表的列表,即长度为 2 的列表。 *arg 将序列解压缩为函数调用的参数。因此,我们将相同的迭代器 2 次传递给 zip()。

  • 同时打印多个列表项

Python

list1 = [1, 3, 5, 7]
list2 = [2, 4, 6, 8]
 
# Here zip() function takes two equal length list and merges them
# together in pairs
for a, b in zip(list1,list2):
    print (a, b)
Output: 
1 2
3 4
5 6
7 8
  • 将字符串作为输入并将其转换为列表:

Python3

# Reads a string from input and type case them to int
# after splitting to white-spaces
 
formatted_list = list(map(int, input().split()))
print(formatted_list)
Input:
2 4 5 6
Output:
[2, 4, 5, 6] 
  • 将列表列表转换为单个列表

Python3

# import the itertools
import itertools
 
# Declaring the list geek
geek = [[1, 2], [3, 4], [5, 6]]
 
# chain.from_iterable() function returns the
# elements of nested list
# and iterate from first list
# of iterable till the last
# end of the list
 
lst = list(itertools.chain.from_iterable(geek))
print(lst)
Output: 
[1, 2, 3, 4, 5, 6]
  • 打印重复的字符:任务是打印这样的图案 Geeeeekkkkss。所以我们可以很容易地打印这个模式,而不用循环使用它。

Python

# + used for string concatenation
# To repeat the character n times, just multiply n 
# with that character  
print ("G" + "e"*5 + "k"*4 + "s"*2)
Output:
Geeeeekkkkss

阅读更多:关于Python的 10 个有趣的事实
参考: https://www.quora.com/What-are-some-cool-Python-tricks