此代碼塊提供了正確的輸出,但是生成數(shù)字替換的表達式效率低下,因為它重復(fù)相同的表達式兩次。有沒有辦法通過不重復(fù)這個短語來使它更簡潔?代碼:numphrase = {3: "three", 5: "five"}result = [''.join([numphrase.get(key) for key in numphrase if not num % key]) if ''.join([numphrase.get(key) for key in numphrase if not num % key]) else num for num in range(101)]print(*result, sep="\n")輸出:threefive12three4five...
如何在列表理解中引用未命名變量?
幕布斯7119047
2023-03-30 16:48:32