您好,登錄后才能下訂單哦!
本篇內容介紹了“簡化Python代碼的技巧有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
你可以將單行代碼視為壓縮在一起的代碼塊,使其適合一行。它是只包含在一行中的簡潔、有用的程序。
如果你并不喜歡寫單行代碼,或者你只是好奇為什么我們必須知道這些,那么下面是一些非常有說服力的理由。
理解 One-liners 將使你成為 Python 專家,因為你將更好地理解該語言。
這將幫助你更快地編寫代碼。你可以比其他人更快地編寫一段代碼,這將有助于你進行競爭性編程。
在線課程將提高你的基礎知識和編程基礎,因為它們會加強你的基礎知識。
你將更多地以 Pythonic 方式編寫代碼。通常,來自不同語言的人經常在 Python 中以非 Python 的方式編寫代碼,例如他們不使用列表推導、多重賦值和切片等。
優化前
if 3 < 2: var=21 else: var=42
優化后
var = 21 if 3<2 else 42
優化前
>>> x = 42 >>> if x > 42: >>> print("no") >>> elif x == 42: >>> print("yes") >>> else: >>> print("maybe") yes
優化后
>>> print("no") if x > 42 else print("yes") if x == 42 else print("maybe") yes
優化前
condition = True if condition: print('hi')
優化后
if condition: print('hello') print('hello') if condition else None
優化前
def f(x): return "hello "+ x
優化后
f = lambda x: "hello "+ x f = exec("def f(x):\n return 'hello '+ x")
優化前
squares = [] for i in range(10): squares.append(i**2)
優化后
squares=[i**2 for i in range(10)]
優化前
squares = [] for i in range(10): if i%2==0: squares.append(i**2)
優化后
squares = [i**2 for i in range(10) if i%2==0]
優化前
squares = [] for i in range(10): if i%2==0: squares.append(i**2) else: squares.append(False)
優化后
squares = [i**2 if i%2==0 else False for i in range(10)]
優化前
c=0 while c < 10: if c!=5: print(c) else: print("FIVE") c+=1
優化后
while c < 10: c+=1; print(c) if c!=5 else print("FIVE")
優化前
>>> def swap(x,y): x = x ^ y y = x ^ y x = x ^ y return x, y >>> swap(10,20) (20,10)
優化后
>>> x, y = 10, 20 >>> x, y = y, x (20, 10)
優化前
a="ONE" b=2 c=3.001
優化后
a, b, c = "One", 2, 3.001
優化前
text = "Helllloooooo" fileName = "hello.txt" f=open(fileName, "a") f.write(text) f.close()
優化后
text = "Helllloooooo" fileName = "hello.txt" print(text, file=open(fileName, 'a'))
優化前
def partition(array, start, end): pivot = array[start] low = start + 1 high = end while True: while low <= high and array[high] >= pivot: high = high - 1 while low <= high and array[low] <= pivot: low = low + 1 if low <= high: array[low], array[high] = array[high], array[low] else: break array[start], array[high] = array[high], array[start] return high def quick_sort(array, start, end): if start >= end: return p = partition(array, start, end) quick_sort(array, start, p-1) quick_sort(array, p+1, end) array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44] quick_sort(array, 0, len(array) - 1) print(array)
優化后
array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44] q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else [] print(q(array))
優化前
def fib(x): if x <= 2: return 1 return fib(x - 1) + fib(x - 2)
優化后
fib=lambda x: x if x<=1 else fib(x-1) + fib(x-2)
優化前
import http.server import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()
優化后
python -m http.server 8000
優化前
iter1 = [1, 2, 3, 4] iter2 = ['a', 'b', 'c'] for x in iter1: for y in iter2: print(x, y)
優化后
[print(x, y) for x in iter1 for y in iter2]
優化前
for i in range(1,5): print(i, end=" ")
優化后
print(*range(1,5))
優化前
class School(): fun = {}
優化后
School = type('School', (object,), {'fun':{}})
優化前
command = input("> ") while command != "quit": print("You entered:", command)
優化后
while (command := input("> ")) != "quit": print("You entered:", command)
“簡化Python代碼的技巧有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。