[关闭]
@maorongrong 2016-09-18T13:11:44.000000Z 字数 916 阅读 540

python数据类型之间转换

Python


list tuple dict string之间转换

  1. #-*-coding:utf-8-*-
  2. #1、字典
  3. dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
  4. #字典转为字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'}
  5. print type(str(dict)), str(dict)
  6. #字典可以转为元组,返回:('age', 'name', 'class')
  7. print tuple(dict)
  8. #字典可以转为元组,返回:(7, 'Zara', 'First')
  9. print tuple(dict.values())
  10. #字典转为列表,返回:['age', 'name', 'class']
  11. print list(dict)
  12. #字典转为列表
  13. print dict.values
  14. #2、元组
  15. tup=(1, 2, 3, 4, 5)
  16. #元组转为字符串,返回:(1, 2, 3, 4, 5)
  17. print tup.__str__()
  18. #元组转为列表,返回:[1, 2, 3, 4, 5]
  19. print list(tup)
  20. #元组不可以转为字典
  21. #3、列表
  22. nums=[1, 3, 5, 7, 8, 13, 20];
  23. #列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20]
  24. print str(nums)
  25. #列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20)
  26. print tuple(nums)
  27. #列表不可以转为字典
  28. #4、字符串
  29. #字符串转为元组,返回:(1, 2, 3)
  30. print tuple(eval("(1,2,3)"))
  31. #字符串转为列表,返回:[1, 2, 3]
  32. print list(eval("(1,2,3)"))
  33. #字符串转为字典,返回:<type 'dict'>
  34. print type(eval("{'name':'ljq', 'age':24}"))

S.join(list)

string.join connects elements inside list of strings, not ints.

Use this generator expression instead :

values = ','.join(str(v) for v in value_list)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注