简体   繁体   中英

Split a list every time the first element of a child list changes

I have a list that looks like this

[[6, 4, 163, 42],
 [6, 44, 163, 79],
 [6, 80, 163, 118],
 [6, 119, 163, 156],
 [6, 158, 163, 187],
 [6, 192, 163, 225],
 [6, 231, 163, 265],
 [169, 4, 260, 42],
 [169, 44, 260, 79],
 [169, 80, 260, 118],
 [169, 119, 260, 156],
 [169, 158, 260, 187],
 [169, 192, 260, 225],
 [169, 231, 260, 265],
 [273, 4, 385, 42],
 [273, 44, 385, 79],
 [273, 80, 385, 118],
 [273, 119, 385, 156],
 [273, 158, 385, 187],
 [273, 192, 385, 225],
 [273, 231, 385, 265]

I am trying to split the list every time the first element of the child list changes

I am trying to get the list to become like this

[[[6, 4, 163, 42],
 [6, 44, 163, 79],
 [6, 80, 163, 118],
 [6, 119, 163, 156],
 [6, 158, 163, 187],
 [6, 192, 163, 225],
 [6, 231, 163, 265]],
 [[169, 4, 260, 42],
 [169, 44, 260, 79],
 [169, 80, 260, 118],
 [169, 119, 260, 156],
 [169, 158, 260, 187],
 [169, 192, 260, 225],
 [169, 231, 260, 265]],
 [[273, 4, 385, 42],
 [273, 44, 385, 79],
 [273, 80, 385, 118],
 [273, 119, 385, 156],
 [273, 158, 385, 187],
 [273, 192, 385, 225],
 [273, 231, 385, 265]]]

I am not sure how to do this

how about something like this:

print([[i for i in lst if i[0] == k] for k in list(set([j[0] for j in lst]))])

where "lst" is the name of your original list

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