【问题标题】:List of lists into table (Row, Column)列表中的列表列表(行,列)
【发布时间】:2019-08-27 09:18:00
【问题描述】:

我的灵感来自环形图案钻石。图案菱形被替换为类似此代码的列表列表。实际上,我已经打印了实际结果 不看行和列。预期结果看起来与实际结果相似。虚拟坐标(dum_coords)表示数据将放入一个表(行、列)中。

如何规则更改值无或列表内的值小于 3。我可以将值替换为新列表。列表中哪个更好用的值或索引。

我需要意见和建议

python,模式菱形,列表列表

l = 4

dum_coords = [None, [[1,2,3],6,7,8], [9,[1,2,3],11,12], [[1,2,3],14,15,16]]
for i in range(l):
   print("row:{}, {}".format(i, dumy_coords[i]))

实际结果:

row:0>> [None]
row:1>> [[1,2,3],6,7,8]
row:2>> [9,[1,2,3],11,12]
row:3>> [[1,2,3],14,15,16]

预期结果:

row:0>> [[None],[None],[None],[None]]
row:1>> [[None],[1,2,3],6,7]
row:2>> [9,[1,2,3],11,[1,2,3]]
row:3>> [[1,2,3],14,15,0]

更新问题:

@Shishir Naresha 建议我他的代码。因为结果与预期结果相似。但是,需要的不止于此。我可以替换值 None 或 not None。

这是我的代码并添加了@Shishir Naresha 的代码

dum_fish_space = [None]*5
dum_fish_pop = [None]*3
dum_fish_preying = [{"current":random.uniform(0,1)*2}]
dum_fish_following = [{"current":random.uniform(0,1)*1,"target":random.uniform(0,1)}]
dum_fish_swarming = [{"current":random.uniform(0,1)}]
dum_fish_randoming = [{"current":random.uniform(0,1)*3}]
dum_fish_battle = []

idxrange = []
new_idxrange = []

for i in range(len(dum_fish_pop)):
  temp = random.randrange(len(dum_fish_space))
  idxrange.append(random.randrange(len(dum_fish_space)))

  if temp is not idxrange:
    new_idxrange.append(temp)

l = 4

dumy_coords = [None, [[1, 2, 3], 6, 7], [9, [1, 2, 3], 11, 12], [[1, 2, 3], 14, 15, 16]]
  for i in range(l):

    if dumy_coords[i] is None:
      print("row:{}, {}".format(i, [None] * l))
    else:
      if len(dumy_coords[i]) < l:
        dif = l - len(dumy_coords[i])
        print("row:{}, {}".format(i, [None] * dif + dumy_coords[i]))
      else:
        print("row:{}, {}".format(i, dumy_coords[i]))

我认为表中的值会根据列表的索引而改变,我预计结果会是一样的

更改值前的预期结果:

row:0>> [[None],[None],[None],[None]]
row:1>> [[None],[1,2,3],6,7]
row:2>> [9,[1,2,3],11,[1,2,3]]
row:3>> [[1,2,3],14,15,0]

更改值后的预期结果:

row:0>> [[1,2,3],[1,2,3],[None],[None]]
row:1>> [[None],[None],6,7]
row:2>> [9,[None],11,[1,2,3]]
row:3>> [[1,2,3],14,15,0]

【问题讨论】:

    标签: arrays python-3.x list dictionary enumeration


    【解决方案1】:

    试试下面的代码。我希望这会有所帮助。

    l = 4
    
    dumy_coords = [None, [[1,2,3],6,7], [9,[1,2,3],11,12], [[1,2,3],14,15,16]]
    for i in range(l):
    
        if dumy_coords[i] is None:
             print("row:{}, {}".format(i, [None]*l))
        else:
            if len(dumy_coords[i]) < l:
                dif = l-len(dumy_coords[i])
                print("row:{}, {}".format(i, [None]*dif+dumy_coords[i]))
            else:
                print("row:{}, {}".format(i, dumy_coords[i]))
    

    输出如下:

    row:0, [None, None, None, None]
    row:1, [None, [1, 2, 3], 6, 7]
    row:2, [9, [1, 2, 3], 11, 12]
    row:3, [[1, 2, 3], 14, 15, 16]
    

    【讨论】:

    • 在尝试了您的代码后我还有一个问题,如何替换该值? even 是 None 或其他值
    • 您的代码给出了预期的结果,但只有一半的答案,请如何更改行内基于@shishir Naresh 内的值
    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 2021-04-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多