簡體   English   中英

如何使用 1 行代碼打印由 1 到 9(來自示例)的數字組成的正方形?

[英]How can I print a square consisting of numbers from 1 to 9 (from the example) using a 1 line code?

我需要在 1 行中編寫一個代碼,它將打印:

0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678

我這樣做了:

print([print(i) for i in range(10)])

但是這段代碼是完全錯誤的,輸出是:

0
1
2
3
4
5
6
7
8
9
[None, None, None, None, None, None, None, None, None, None]

我想我應該使用map()lambda但我不知道該怎么做,我只是在學習...

print("\n".join([("".join(str((i+j)%10) for i in range(10))) for j in range(10)]))

請注意,打印函數返回None這是你有原因None印了這么多次,你需要嵌套循環打印此二維數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM