在嵌套列表中搜索范围之间的数字
Search in nested list where number between range
我有下面的嵌套列表 row_list
:
[[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1:' Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1': Decimal('51.600')
}
],[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1': Decimal('51.600')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1': Decimal('51.600')
},
{
'text': 'Col',
'x0': Decimal('200.736'),
'x1': Decimal('210.296')
},
{
'text': '2',
'x0': Decimal('230.600'),
'x1': Decimal('240.920')
}
]]
其中每个嵌套列表,代表一个文本行。所以上面代表:
Col 1
Col 1 Col 2
现在考虑我有两个定义的区域 (x, y, w, h)
,我想将它们用于 "split" 列表(很像 table 列)。例如:
areas = {}
areas[0] = (0, 0, 100, 792)
areas[1] = (100, 0, 300, 792)
有了上面的内容,我想 select 定义区域内的所有文本(无论它属于哪个嵌套列表)。那应该给我:
[[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1': Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1:' Decimal('51.000')
},
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1:' Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1:' Decimal('51.600')
}
],[
{
'text': 'Col',
'x0': Decimal('200.736'),
'x1': Decimal('210.296')
},
{
'text': '2',
'x0': Decimal('230.600'),
'x1': Decimal('240.920')
}
]]
我有点不确定如何 search/select 嵌套列表和 "remap" 数据。我试过类似的东西:
finalCols = []
for i, area in enumerate(areas):
area = areas[i]
for line in row_list:
for word in line:
if word['x0'] >= area[0] and word['x1'] <= area[2]:
finalCols[].append(word)
但这只是将每个单词附加到列表中,并没有像上面那样创建嵌套列表结构(我的预期输出)。
你很接近。应该是这样的:
finalCols = []
for area in areas:
for line in area:
newWords = []
for word in line:
if word['x0'] >= area[0] and word['x1'] <= area[2]:
newWords.append(word)
finalCols.append(newWords)
我有下面的嵌套列表 row_list
:
[[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1:' Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1': Decimal('51.600')
}
],[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1': Decimal('51.600')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1': Decimal('51.600')
},
{
'text': 'Col',
'x0': Decimal('200.736'),
'x1': Decimal('210.296')
},
{
'text': '2',
'x0': Decimal('230.600'),
'x1': Decimal('240.920')
}
]]
其中每个嵌套列表,代表一个文本行。所以上面代表:
Col 1
Col 1 Col 2
现在考虑我有两个定义的区域 (x, y, w, h)
,我想将它们用于 "split" 列表(很像 table 列)。例如:
areas = {}
areas[0] = (0, 0, 100, 792)
areas[1] = (100, 0, 300, 792)
有了上面的内容,我想 select 定义区域内的所有文本(无论它属于哪个嵌套列表)。那应该给我:
[[
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1': Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1:' Decimal('51.000')
},
{
'text': 'Col',
'x0': Decimal('21.600'),
'x1:' Decimal('30.000')
},
{
'text': '1',
'x0': Decimal('41.600'),
'x1:' Decimal('51.600')
}
],[
{
'text': 'Col',
'x0': Decimal('200.736'),
'x1': Decimal('210.296')
},
{
'text': '2',
'x0': Decimal('230.600'),
'x1': Decimal('240.920')
}
]]
我有点不确定如何 search/select 嵌套列表和 "remap" 数据。我试过类似的东西:
finalCols = []
for i, area in enumerate(areas):
area = areas[i]
for line in row_list:
for word in line:
if word['x0'] >= area[0] and word['x1'] <= area[2]:
finalCols[].append(word)
但这只是将每个单词附加到列表中,并没有像上面那样创建嵌套列表结构(我的预期输出)。
你很接近。应该是这样的:
finalCols = []
for area in areas:
for line in area:
newWords = []
for word in line:
if word['x0'] >= area[0] and word['x1'] <= area[2]:
newWords.append(word)
finalCols.append(newWords)