zoukankan      html  css  js  c++  java
  • python--list,str,dict,json,tuple互换用法实例

    这几种类型比较常见,在这里结合稍微总结一下。

    1. list 转 string

    str()方法不不能转出list列表值,会包含其他无关符号如‘[',用join的方法会将列表元素分隔开。

    2. string 转 list

    直接调用append方法插入list列表

    还有一种方法就是通过符号分割的方法,这种方法在很多场景下很管用

    3. dict 转 json数据

    1.  
      import json
    2.  
       
    3.  
      Dict = {"a":2, "b":3}
    4.  
       
    5.  
      jsonData = json.dumps(Dict)
    6.  
       
    7.  
      with open('data.json', 'w') as f:
    8.  
      f.write(jsonData)

    4. json 数据转 dict

    1.  
      import json
    2.  
       
    3.  
      d = {}
    4.  
      with open('data.json', 'r') as f:
    5.  
      f.read(d)
    6.  
       
    7.  
      print(d) # d 为json转dict后的对象

    5. tuple转list

    6. list转tuple

  • 相关阅读:
    Web基础 网页的血肉CSS
    18
    19
    20
    17
    16
    15
    13
    14
    12
  • 原文地址:https://www.cnblogs.com/zhangshijiezsj/p/14203130.html
Copyright © 2011-2022 走看看