python 中怎么把,list,字典dict转换为字符串 如何将python字符串转换为包含字典的列表

Python\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u5178\uff1f

\u5148\u6253\u5f00Python\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u7a97\u53e3\uff0c\u8fd9\u662f\u5fc5\u8981\u7684\u4e00\u6b65
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u8f93\u5165 dict1={'1':'a','2':'b','3':'c'} \uff0c\u7136\u540e\u56de\u8f66\uff0c\u8fd9\u91cc\u6211\u5c06\u5b83\u547d\u540d\u4e3adict1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5176\u4ed6\u7684\u540d\u5b57
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

\u56de\u8f66\u4e4b\u540e\u5c31\u6253\u5370\u51fa\u4e86{'1': 'a', '2': 'b', '3': 'c'}\uff0c\u8bf4\u660e\u521b\u5efa\u6210\u529f\u4e86\u3002
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

\u63a5\u7740\u6211\u4eec\u7528Python\u7684\u5185\u7f6e\u51fd\u6570 type\uff08object\uff09\u67e5\u770b\u5b83\u7684\u7c7b\u578b\uff0c\u8f93\u5165type(dict1)\u540e\u56de\u8f66\uff0c\u51fa\u73b0\u8bf4\u660e\u662f\u5b57\u5178\u7c7b\u578b\u3002
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

\u91cd\u70b9\u6765\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u89c1\u8bc1\u5947\u8ff9\u7684\u65f6\u523b\uff0c\u8f93\u5165str1 =str(dict1)\u540e\u56de\u8f66\uff0c\u548c\u4e0a\u9762\u4e00\u773c\uff0cstr1\u53ef\u4ee5\u81ea\u5df1\u547d\u540d\uff0c\u8fd9\u91cc\u7528\u5230\u4e86Python\u7684\u5185\u7f6e\u51fd\u6570str\uff08\uff09\u3002
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

\u6700\u540e\u8f93\u5165type(str1)\u540e\u56de\u8f66\uff0c\u51fa\u73b0\u8bf4\u660e\u662f\u5b57\u5178\u7c7b\u578b\u3002\u5728Python\u4e2dstr\u662f\u5b57\u7b26\u4e32\uff0cdict\u662f\u5b57\u5178\u3002
Python\u4e2d\u5b57\u5178\u600e\u4e48\u8f6c\u5316\u6210\u5b57\u7b26\u4e32

#-*-coding:utf-8-*-
#1\u3001\u5b57\u5178
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
#\u5b57\u5178\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\uff1a {'age': 7, 'name': 'Zara', 'class': 'First'}
print type(str(dict)), str(dict)
#\u5b57\u5178\u53ef\u4ee5\u8f6c\u4e3a\u5143\u7ec4\uff0c\u8fd4\u56de\uff1a('age', 'name', 'class')
print tuple(dict)
#\u5b57\u5178\u53ef\u4ee5\u8f6c\u4e3a\u5143\u7ec4\uff0c\u8fd4\u56de\uff1a(7, 'Zara', 'First')
print tuple(dict.values())
#\u5b57\u5178\u8f6c\u4e3a\u5217\u8868\uff0c\u8fd4\u56de\uff1a['age', 'name', 'class']
print list(dict)
#\u5b57\u5178\u8f6c\u4e3a\u5217\u8868
print dict.values
#2\u3001\u5143\u7ec4
tup=(1, 2, 3, 4, 5)
#\u5143\u7ec4\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\uff1a(1, 2, 3, 4, 5)
print tup.__str__()
#\u5143\u7ec4\u8f6c\u4e3a\u5217\u8868\uff0c\u8fd4\u56de\uff1a[1, 2, 3, 4, 5]
print list(tup)
#\u5143\u7ec4\u4e0d\u53ef\u4ee5\u8f6c\u4e3a\u5b57\u5178
#3\u3001\u5217\u8868
nums=[1, 3, 5, 7, 8, 13, 20];
#\u5217\u8868\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\uff1a[1, 3, 5, 7, 8, 13, 20]
print str(nums)
#\u5217\u8868\u8f6c\u4e3a\u5143\u7ec4\uff0c\u8fd4\u56de\uff1a(1, 3, 5, 7, 8, 13, 20)
print tuple(nums)
#\u5217\u8868\u4e0d\u53ef\u4ee5\u8f6c\u4e3a\u5b57\u5178
#4\u3001\u5b57\u7b26\u4e32
#\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5143\u7ec4\uff0c\u8fd4\u56de\uff1a(1, 2, 3)
print tuple(eval("(1,2,3)"))
#\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5217\u8868\uff0c\u8fd4\u56de\uff1a[1, 2, 3]
print list(eval("(1,2,3)"))
#\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5b57\u5178\uff0c\u8fd4\u56de\uff1a
print type(eval("{'name':'ljq', 'age':24}"))

字典用values()函数转化成值的列表,用items转换成(key,value)的元组列表。
列表转换成字典,需要用2个列表转化成字典,一个是key,一个是value。比如:
>>>dict(zip(['a','b','c'], range(5)))
{'a': 0, 'c': 2, 'b': 1}

>>> a=[1,2,3]
>>> ''.join( [ str(x) for x in a])
'123'
>>> b={1:11, 2:22, 3:33}
>>> ''.join( [ str(x) for x in b.values()])
'112233'
>>> ''.join( [ str(x) for x in b])
'123'
>>> ''.join( [ str(x) for x in b.items()])
'(1, 11)(2, 22)(3, 33)'
>>>

a = {'id':1}
b = [1,2]
c = str(a)
d = str(b)
print(c) => "{'id': 1}"
print(d) => "[1,2]"

扩展阅读:python入门 ... python中li 1 什么作用 ... 学python后到底能干什么 ... python ai ... python初学编程必背 ... python中append ... c++和python先学哪个 ... python 判断是否含有字母 ... python &用法 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网