【问题标题】:Python Two-Dimensional ArrayPython 二维数组
【发布时间】:2020-01-22 16:20:44
【问题描述】:

你好! 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

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。请编辑您的问题,以完全清楚您的输入、预期输出以及您在实践中得到的结果。这将使社区能够帮助您。恐怕现在的文字很难理解。谢谢。

标签: python


【解决方案1】:

以下代码

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))

生产

嗨 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)

【讨论】:

  • 谢谢。有没有办法把它放在一个二维数组中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 2012-03-09
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多