python str与嵌套list相互转换

需求:

将[[1, 3, 456], [4, 5, 667], [9, 3, 457]] 与 "[[1, 3, 456], [4, 5, 667], [9, 3, 457]]"互相转换,list里面是int类型

代码如下:


if __name__ == "__main__":
    c = [[6, 6, 700], [6, 6, 581], [6, 6, 574], [6, 6, 588], [6, 6, 735], [6, 6, 693], [6, 6, 791], [6, 6, 798],
 [6, 6, 679], [6, 6, 553], [6, 6, 728], [6, 6, 609], [6, 6, 749], [6, 6, 805], [6, 6, 455], [6, 6, 518]]
    print(type(c), c)

    # 嵌套list 转 str
    str_c = str(c)
    print(type(str_c), str_c)

    # str 转回原来的 嵌套list
    list_c = eval(str_c)
    print(type(list_c), list_c)

输出:

用法:

可用于 嵌套list 的 socket 传输

你可能感兴趣的:(list)