【问题标题】:How to get list from specific input of numbers (x,y axis)?如何从特定输入的数字(x,y 轴)中获取列表?
【发布时间】:2019-05-10 04:53:53
【问题描述】:
Input= 2 2 2 1 2 0 1 0 0 0 0 1

第一个数字是正常XY轴的X坐标(未列出),第二个Y坐标,第三个X等等;所以从这个输入来看,它看起来像:

Y
2    *
1*   *
0* * *
 0 1 2 X

(第一个*:2,2,第二个*:2,1,第三个*:2,0 - 从右侧开始)。

我需要得到如下所示的输出:

 output=
[[0,0,1],
 [1,0,1],
 [1,1,1]]

到目前为止,我尝试了这个,但不知道如何继续:

inp=[2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
maxx=0
maxy=0

for i in range(1,len(inp),2): #yaxis
    if inp[i]>maxy:
        maxy=inp[i]
        continue
    else:
        continue

for j in range(0,len(inp),2): #xaxis
    if inp[j]>maxx:
        maxx=inp[j]
        continue
    else:
        continue

part=[]
for i in range(maxy+1):
    part.append([0 for j in range (maxx+1)])
for k in range(0,len(inp),2):
    for j in range(1,len(inp),2):
        for i in range(len(part)):
            part[i][j]=

【问题讨论】:

  • 到目前为止你尝试了什么?你到底有什么问题?
  • inp=[2,2,2,1,2,0,1, 0, 0, 0, 0, 1] maxx=0 maxy=0 for i in range(1,len(inp),2): #yaxis if inp[i]>maxy: maxy=inp[i] continue else: continue for j in range(0,len(inp),2): #xaxis if inp[j]>maxx: maxx=inp[j] continue else: continue part=[] for i in range(maxy+1): part.append([0 for j in range (maxx+1)]) for k in range(0,len(inp),2): for j in range(1,len(inp),2): for i in range(len(part)): part[i][j]= 到目前为止我试过这个,但不知道如何继续。
  • 我试图将其编辑到您的帖子中。请检查缩进是否与您的实际代码匹配。

标签: python-3.x list input output coordinates


【解决方案1】:
inp = [2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
tuples = [(inp[i], inp[i+1]) for i in range(0, len(inp), 2)]
print(tuples) # [(2, 2), (2, 1), (2, 0), (1, 0), (0, 0), (0, 1)]

# Define the dimensions of the matrix 
max_x_value = max([i[0] for i in tuples])+1
max_y_value = max([i[1] for i in tuples])+1

# Build the matrix - fill all cells with 0 for now
res_matrix = [[0 for _ in range(max_y_value)] for _ in range(max_x_value)]

# Iterate through the tuples and fill the 1's into the matrix
for i in tuples:
    res_matrix[i[0]][i[1]]=1

print(res_matrix) # [[1, 1, 0], [1, 0, 0], [1, 1, 1]]

# Rotate the matrix by 90 to get the final answer
res = list(map(list, list(zip(*res_matrix))[::-1]))
print(res) # [[0, 0, 1], [1, 0, 1], [1, 1, 1]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 2021-12-20
    • 2020-02-09
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多