If I have the following:
[(1,2),(2,3),(0,0),(4,0),(0,1),(3,9),(2,0),(2,4)]
How can I split it into:
[(1,2),(2,3)], [(0,1),(3,9)], [(2,4),]
were every time I see a tuple with a 0 at index 1, e.g. (1,0), I split the list.
If I have the following:
[(1,2),(2,3),(0,0),(4,0),(0,1),(3,9),(2,0),(2,4)]
How can I split it into:
[(1,2),(2,3)], [(0,1),(3,9)], [(2,4),]
were every time I see a tuple with a 0 at index 1, e.g. (1,0), I split the list.
特に滑らかではありませんが、次のようにループすることができます。
myList = [(1,2),(2,3),(0,0),(4,0),(0,1),(3,9),(2,0),(2,4)]
groupedList = []
subList = []
for tup in myList:
if tup[1] == 0:
if subList != []:
groupedList.append(subList)
subList = []
else:
subList.append(tup)
if subList != []:
groupedList.append(subList)
print groupedList
出力:
[[(1, 2), (2, 3)], [(0, 1), (3, 9)], [(2, 4)]]