zoukankan      html  css  js  c++  java
  • Python中字符串转换列表

      前言:在使用Python完成工作中一些任务的时候,常常要对数据类型进行转换,比如字符串转列表,列表转字符串,字符串转元组等等,下面分享一下常用的字符串转列表

    字符串转列表

    第一种字符串转列表:
    t_str="abc"
    输出类型:
    print(type(t_str))
    结果:
    输出类型:<class 'str'> 
    转换为列表:
    t_str = list(t_str)
    结果:
    输出为:['a', 'b', 'c']
    输出类型:
    print(type(list(t_str)))
    结果:
    输出类型:<class 'list'>
    
    第二种字符串转列表:
    t_str="a,b,c"
    分割:
    new_list = t_str.split(',')
    查看结果和类型
    print(new_list,type(new_list))
    输出结果:
    ['a', 'b', 'c'] <class 'list'>
    
    第三种字符串转列表:
    import json
    t_str='[1,2,3,4]'
    转换类型:
    t_str=json.loads(t_str)
    print(t_str)
    输出结果:
    [1, 2, 3, 4] 
    查看类型:
    print(type(json.loads(t_str)))
    结果:
    <class 'list'>
    

      

      

      

  • 相关阅读:
    while循环学习之统计流量
    MySQL的启动脚本
    UVA 725 Division
    UVA 712 S-tree
    UVA 514
    字典树
    UVA 1595 multimap 的应用
    C++ map 和 multimap
    浮点数
    UVA 227
  • 原文地址:https://www.cnblogs.com/lucktomato/p/14779184.html
Copyright © 2011-2022 走看看