Python 怎么将列表类字典组字符串转换为列表? python列表怎么转换成列表里的元素是字典的形式

\u5982\u4f55\u5c06python\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868

#-*-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}"))

\u5b57\u6bb5\u4e4b\u95f4\u662f\u7a7a\u683c\u8fd8\u662f\u5236\u8868\u7b26\u5206\u9694\u7684\uff1f\u5982\u679c\u662f\u7a7a\u683c\u90a3\u5c31\u592a\u9ebb\u70e6\u4e86\u3002

如果是你发的那一串肯定是可以用json loads的,如果不能,发下原文字符串。

list1=[{"id":100220748688221696701923815,"ts":1614576522050,"tradeId":7500734,"amount":1152.43,"price":0.01852,"direction":"sell"}]
list_new=[]
d=list1[0]
print(d)
for key in d:
list_new.append(key)
list_new.append(d[key])
print(list_new)



不知理解得对不对,你看看:

test = [
[{'id':11,
'ts':12,
'tradeId':13,
'amount':14,
'price':15,
'direction':'1'
}],
[{'id':21,
'ts':22,
'tradeId':23,
'amount':24,
'price':25,
'direction':'2'
}],
[{'id':31,
'ts':32,
'tradeId':33,
'amount':34,
'price':35,
'direction':'3'
}],
]
result = {'id':[],'ts':[],'tradeId':[],'amount':[],'price':[],'direction':[]}

for row in test:
for sub_row in row:
result['id'].append(sub_row['id'])
result['ts'].append(sub_row['ts'])
result['tradeId'].append(sub_row['tradeId'])
result['amount'].append(sub_row['amount'])
result['price'].append(sub_row['price'])
result['direction'].append(sub_row['direction'])

由于太复杂的功能不会用列表推导式,所以就做成循环了,即最后是一个字典,字典中的值是列表:



扩展阅读:python符号含义大全 ... python代码自动生成器 ... python将字典转换为列表 ... python中将两个列表合并 ... python 多个列表合并 ... python 列表生成式 ... python字典怎么变成列表 ... python编程入门自学 ... 将两个列表合并成一个列表python ...

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