簡體   English   中英

Python 二維數組循環

[英]Python 2d array loop

這可能是個愚蠢的問題,但我從 python 開始,我不知道如何寫它。

所以我想像在屏幕上一樣循環打印這個表然后我希望它可用但是我很難把它寫下來(請看屏幕):

table = [print([random.randint(0,1) for x in range(10)]) for y in range(10)]
a = table
print(a)

控制台 output

使用列表中的print值。 但是方法print什么都不返回, None ,所以你在外部列表中保存了 10 None

table = [[random.randint(0, 1) for x in range(10)] for y in range(10)]
for row in table:
    print(row)
a = table
print(a)
[1, 0, 0, 1, 1, 0, 1, 1, 1, 1]
[0, 1, 1, 0, 0, 1, 0, 1, 1, 0]
[1, 0, 1, 0, 0, 0, 0, 1, 1, 0]
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0]
[0, 1, 0, 0, 1, 1, 0, 1, 0, 0]
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1]
[0, 1, 0, 1, 1, 1, 0, 0, 0, 0]
[0, 1, 0, 1, 0, 1, 1, 1, 1, 1]
[1, 0, 0, 0, 1, 1, 1, 0, 1, 1]
[1, 1, 0, 0, 0, 0, 1, 1, 0, 0]
[[1, 0, 0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 0, 0, 1, 0, 1, 1, 0], [1, 0, 1, 0, 0, 0, 0, 1, 1, 0], [1, 1, 1, 1, 0, 1, 1, 1, 0, 0], [0, 1, 0, 0, 1, 1, 0, 1, 0, 0], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 1, 1, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1, 1, 1], [1, 0, 0, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 0, 0, 0, 1, 1, 0, 0]]

暫無
暫無

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

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