有没有办法将行号和列号映射到 python 中的相应数字
is there a way to map the row and column numbers to corresponding numbers in python
嘿,所以我试图用数字绘制出第一行和第一列,但在这里发现了一些困难
output = " "
column_count = 0
row_count = 1
for row in range(10):
output = " "
for column in range(20):
if (row == 0):
while column_count <= 20:
print(column_count, end = " ")
column_count += 1
elif (column == 0):
while row_count <= 10:
print(row_count)
row_count += 1
else:
output += "x"
print(output)
这是我的输出
so something like this but mapped out perfectly
根据我对你问题的理解,你实际上是在寻找一个网格,就像你在乘法表中找到的一样,除了 'X' 而不是产品。
以下是 this answer to the question How to print a 10*10 times table as a grid? 的改编版。
# Set Number of Rows and Columns
cols = 21
rows = 11
# Determine width of each column based on largest column
space_fmt = f'{{:>{len(str(cols)) + 1}}}'
# Print Header Row
print(''.join(space_fmt.format(i) for i in range(0, cols)))
# Print Remaining Rows
for y in range(1, rows):
# Print First Number
print(space_fmt.format(y), end='')
# Print Xes
print(''.join([space_fmt.format('X') for _ in range(1, cols)]))
只有 for 循环的答案,没有加入列表理解
# Set Number of Rows and Columns
cols = 21
rows = 11
# Determine width of each column based on largest column
space_fmt = f'{{:>{len(str(cols)) + 1}}}'
# Print Header Row
for i in range(0, cols):
print(space_fmt.format(i), end='')
print() # New Line
# Print Remaining Rows
for y in range(1, rows):
# Print First Number
print(space_fmt.format(y), end='')
# Print Xes
for col in range(1, cols):
print(space_fmt.format('X'), end='')
print() # New Line
嘿,所以我试图用数字绘制出第一行和第一列,但在这里发现了一些困难
output = " "
column_count = 0
row_count = 1
for row in range(10):
output = " "
for column in range(20):
if (row == 0):
while column_count <= 20:
print(column_count, end = " ")
column_count += 1
elif (column == 0):
while row_count <= 10:
print(row_count)
row_count += 1
else:
output += "x"
print(output)
这是我的输出
so something like this but mapped out perfectly
根据我对你问题的理解,你实际上是在寻找一个网格,就像你在乘法表中找到的一样,除了 'X' 而不是产品。
以下是 this answer to the question How to print a 10*10 times table as a grid? 的改编版。
# Set Number of Rows and Columns
cols = 21
rows = 11
# Determine width of each column based on largest column
space_fmt = f'{{:>{len(str(cols)) + 1}}}'
# Print Header Row
print(''.join(space_fmt.format(i) for i in range(0, cols)))
# Print Remaining Rows
for y in range(1, rows):
# Print First Number
print(space_fmt.format(y), end='')
# Print Xes
print(''.join([space_fmt.format('X') for _ in range(1, cols)]))
只有 for 循环的答案,没有加入列表理解
# Set Number of Rows and Columns
cols = 21
rows = 11
# Determine width of each column based on largest column
space_fmt = f'{{:>{len(str(cols)) + 1}}}'
# Print Header Row
for i in range(0, cols):
print(space_fmt.format(i), end='')
print() # New Line
# Print Remaining Rows
for y in range(1, rows):
# Print First Number
print(space_fmt.format(y), end='')
# Print Xes
for col in range(1, cols):
print(space_fmt.format('X'), end='')
print() # New Line