我正在使用代码,我有一个列表:
listA = (
['name1', 'A11,A12,A13', 'B11,B12,B13', 'C11,C12,C13'],
['name2', 'A21,A22', 'B21,B22', 'C21,C22'],
['name3', 'A31,A32,A33,A34,A35', 'B31,B32,B33,B34,B35', 'C31,C32,C33,C34,C35' ],
)
我需要得到:
listA = (
['name1', 'A11', 'B11', 'C11'],
['name1', 'A12', 'B12', 'C12'],
['name1', 'A13', 'B13', 'C13'],
['name2', 'A21', 'B21', 'C21'],
['name2', 'A22', 'B22', 'C22'],
['name3', 'A31', 'B31', 'C31'],
['name3', 'A32', 'B32', 'C32'],
['name3', 'A33', 'B33', 'C33'],
['name3', 'A34', 'B34', 'C34'],
['name3', 'A35', 'B35', 'C35'],
)
请帮助我,我已经赌了。
谢谢你的时间。
答案 0 :(得分:4)
list_b = []
for x in list_a:
i = iter(x)
name = next(i)
list_b.extend((name,) + t for t in zip(*(y.split(",") for y in i)))
答案 1 :(得分:1)
有点难看,但是......
listB = []
for tup in listA:
tmptup = []
for elt in tup:
splt = elt.split(',')
for n in splt:
tmptup.append(n)
listB.append(tmptup)
答案 2 :(得分:1)
listA = tuple([name, a, b, c] for (name, aas, bbs, ccs) in listA
for (a, b, c) in zip(aas.split(','), bbs.split(','), ccs.split(',')))
答案 3 :(得分:1)
def uncollapse(L):
temp = []
answer = []
for item in L:
temp= [item[0]] + [i.split(',') for i in item]
for i in range(len(temp[1])):
answer.append([temp[0]] + [zip(*temp[1:])[i]])
return answer
经过测试和工作