简体   繁体   中英

Split the list into sub-lists based on first value in first item

I am appending two values in a list ie A = [(1, (2, 3, 4, 5)), (1, (20, 33, 41, 15)), (2, (12, 5, 34, 10)), and so on... ], where 1st value is a page number and next four values are bounding box co-ordinates in that particular page. Now I have to split the list into sub-lists using first value (page number) in an item (1, 2 in given example). The result should look like [[(1, (2, 3, 4, 5)), (1, (20, 33, 41, 15))], [2, (12, 5, 34, 10)], and so on... ], so that all bounding box co-ordinates of a page will be in a sub-list to perform certain logics. How to do this?

I have edited this to fit in your requirement:

A = [(1, (2, 3, 4, 5)), (1, (20, 33, 41, 15)), (2, (12, 5, 34, 10))]
final = []
final_dict = {}
for (key, value) in A:
    if key not in final_dict:
        final_dict[key] = [(key,value)]
    else:
        final_dict[key].append((key,value))
#print(final_dict)

for key,value in final_dict.items():
    final.append(value)

#print(final)

In the for loop I'm checking with the previous and current page number, if the difference is zero appending them into same sub-lists. The code is as follows:

res, last = [[ ]], None

for x, y in A:

if last is None or abs(last - x) == 0:

    res[-1].append((x, y) )

else:

    res.append([x])

last = x

Maybe easier to use dictionary to group the sublists and format them later.

A = [(1, (2, 3, 4, 5)), (1, (20, 33, 41, 15)), (2, (12, 5, 34, 10))]

memo = {}
for (page, bbox) in A:
    if page not in memo:
        memo[page] = [bbox]
    else:
        memo[page].append(bbox)

# memo => {1: [(2, 3, 4, 5), (20, 33, 41, 15)], 2: [(12, 5, 34, 10)]}


result = []
for page, bbox_list in memo.items():
    page_list = []
    for bbox in bbox_list:
        page_list.append((page, bbox))
    result.append(page_list)

# result => [[(1, (2, 3, 4, 5)), (1, (20, 33, 41, 15))], [(2, (12, 5, 34, 10))]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM