簡體   English   中英

Python 二維數組

[英]Python Two-Dimensional Array

在此處輸入圖像描述

你好。 1列“HELLO”中有一個垂直書寫的鍵。 有必要按照表中的字母順序垂直填充字母。

矩陣應該是方形的(行數與列數相同)。 你能幫忙嗎? 我將鑰匙垂直放置,但無法水平書寫字母表中的字母。

key='HELLO'
f=[]
count = 1
A = [['' for i in key] for i in key]
for i in range(len(A)):
    A[i][0]=key[i]
for i in range(0,len(A)):
  for j in range(0,len(A[i])):
      if A[i][0]==key[i]:
            n=count+ord(key[i])
            A[j][i]=n

以下代碼

from itertools import cycle 
from string import ascii_uppercase 
letter = cycle(ascii_uppercase)

column1 = 'HELLO'
len_other_columns = len(column1)-1
other_columns = range(len_other_columns)

for letter1 in column1: 
    while next(letter) != letter1: ... 
    print(letter1, ' '.join(next(letter) for _ in other_columns))

生產

H I J K L
E F G H I
L M N O P
L M N O P
O P Q R S

要將結果放在列表列表或二維數組中,

from itertools import cycle 
from string import ascii_uppercase 
letter = cycle(ascii_uppercase)

column1 = 'HELLO'
len_other_columns = len(column1)-1
other_columns = range(len_other_columns)
# Initialize the 2D array
two_d_array = [[c] for c in column1]
# similar to the previous
for sublist in two_d_array:
    while next(letter) != sl[0]: ...
    for _ in other_columns: sublist.append(next(letter))
print(two_d_array)

暫無
暫無

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

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