Python中怎么把读进来的数据转成list啊,小白求问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])

输出结果:
图片说明

用字符串的split方法

str='2257,4018,1096'
target_list = [int(x) for x in str.split(',')]
print target_list
[2257, 4018, 1096]

或者

str='2257,4018,1096'
exec("target_list=[%s]"%str)
print target_list
[2257, 4018, 1096]

不知道对不对,为了积分,你懂的……

先创建原始字符串方便测试

mstr = "[\"33\", \"23\", \"56\", \"58\"]"
from ast import literal_eval
mlist = literal_eval(mstr)
print type(mlist)
print mlist

#!/usr/bin/python
import re
data = '''
1 33

1 23

1 56

1 58

2 11

2 66

2 88

3 98

3 66
'''

lists = re.findall("(\\d+)\\s+(\\d+)",data,re.MULTILINE)
datas = dict()
for i in lists:
    if i[0] in datas:
        datas[i[0]].append(i[1])
    else:
        datas[i[0]]=[i[1]]
print([ i for i in datas.values()])