字符串中有列表如何把列表提取出来?


file = ['[1,2,3],[4,5,6],[7,8,9]']
data = file[0]

number = []
alist = []
count = 0 
i = 0  


char = ['[', ',', ']']

while i < len(data):
    if data[i] in char:
        i += 1
        
    elif count < 3:
        alist.append(int(data[i]))
        count += 1
        i += 1 
    
    if count == 3:
        number.append(alist)
        alist = []
        count = 0 

print(number)

输出:number = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM