我有一個字符串數組,看起來像這樣Itertools打破一個數組子陣
['a', 'b', 'c.1', 'c.2', 'c.3', 'c.4', 'd.1', 'd.2', 'd.3']
我想打破弄成這個樣
[['a', 'b', 'c.1'], ['a', 'b', 'c.2'], ['a', 'b', 'c.3'],
['a', 'b', 'd.1'], ['a', 'b', 'd.2'], ['a', 'b', 'd.3'],
['a', 'b', 'd.4']]
我如何去這樣做?我的想法是使用GROUPBY在itertools 要達到這樣的
['a', 'b', ['c.1', 'c.2', 'c.3'], ['d.1', 'd.2', 'd.3', 'd.4']]
,然後大概就像
result = []
for elem in nestedList:
if isinstance(elem, list):
temp = []
for elem in nestedList:
if not isinstance(elem, list):
temp.append(elem):
temp.append(elem)
result.append(temp)
爲了做到這一點,我需要訪問下一個元素GROUPBY
(Something like lambda x: '.' not in x and nextelement.split('.')[0] != x)
我該怎麼做?
什麼是'c .1'?這是無效的語法 – RomanPerekhrest
'c.1'不是一個正確的Python語法。它應該是一個字符串?邏輯是什麼?爲什麼要對'a'和'b'進行區別對待? –
那些應該是字符串嗎? –