【发布时间】:2019-07-03 19:51:55
【问题描述】:
我有下面的嵌套列表称为row_list:
[
[{
'text': 'Page 1, col 1.',
'top': Decimal('83.640')
}],
[{
'text': 'Page 1, col 2.',
'top': Decimal('112.920')
}],
[{
'text': 'Page 1, col 3',
'top': Decimal('127.560'),
}]
]
现在,我正在尝试迭代此列表列表。
但是,当我迭代时,我想获取 n 列表中的每个项目,并向右“看” - 这意味着我想检查 以下 列表(除非它是最后一个列表),相同的n 项目的值。
例如,对于我的列表的第一次迭代,如下:
[{
'text': 'Page 1, col 1.',
'top': Decimal('83.640')
}]
这里我想检查其他列表中其他nitem 的top 值。如果该最高值不相同(或在 10 的容差范围内),我想在同一 n 位置添加一个新项目 - 但在另一个列表中。
所以按照上面的例子,83.640 的顶层不在其他两个列表的接受范围内,所以我们应该为每个列表添加一个项目:
[
[{
'text': 'Page 1, col 1.',
'top': Decimal('83.640')
}],
[{
'text': '', #added item
'top': Decimal('83.640')
},{
'text': 'Page 1, col 2.',
'top': Decimal('112.920')
}],
[{
'text': '', #added item
'top': Decimal('83.640')
},{
'text': 'Page 1, col 3',
'top': Decimal('127.560'),
}]
]
我试图弄清楚如何从左到右而不是从上到下查看列表。这是我目前所拥有的:
for col_no, col in enumerate(row_list):
#Don't compare the last list, as there are no more lists to compare to.
if col != row_list[-1]:
for line in col:
currentTopValue = line['top']
nextColValue = row_list[col_no + 1][0]['top']
if abs(currentTopValue - nextColValue) >= float(10):
row_list.insert(0, [{'text': '', 'top': currentTopValue}])
如您所见,以上内容是相当静态的(硬编码索引)。行列表可以在每个列表中包含许多项。
由于某种原因,当我执行代码时,它没有运行(似乎它挂了 - 可能是瓶颈?)
谁能指引我正确的方向?
【问题讨论】:
标签: python python-3.x python-3.7