比如说读进来的数据如下
1 33
1 23
1 56
1 58
2 11
2 66
2 88
3 98
3 66
怎么转成[[33,23,56,58][11,66,88][98,66]]这种list?正在做毕设,以前从来没学过python,努力学习中,求问QAQ
比如说读进来的数据如下
1 33
1 23
1 56
1 58
2 11
2 66
2 88
3 98
3 66
怎么转成[[33,23,56,58][11,66,88][98,66]]这种list?正在做毕设,以前从来没学过python,努力学习中,求问QAQ
不知道你原本的数据是怎么读的,我这里暂且当做是txt文件吧:
def text2list(path):
f = open(path, 'r')
lineInfo = f.readlines()
dataset = []
for line in lineInfo:
temp1 = line.strip('\n')
temp2 = temp1.split()
dataset.append(temp2)
datas = dict()
for i in dataset:
if i[0] in datas:
datas[i[0]].append(i[1])
else:
datas[i[0]]=[i[1]]
print(datas)
f.close()
return datas
if __name__ == '__main__':
path = "C:\\Users\\15330\\Desktop\\test.txt"
datas = text2list(path)
lists = [ i for i in datas.values()]
print(lists[0])
print(lists[1])
print(lists[2])