炫意html5
最早CSS3和HTML5移动技术网站之一

python 想到一个好玩的东西 就是如何将一个长得像字典的列表转换为字典

像是这一个列表 有没有什么简单的方法直接转换为字典呢

[{‘ip’: ‘123.181.150.193’, ‘port’: 4241}, {‘ip’: ‘114.238.223.98’, ‘port’: 4237}, {‘ip’: ‘114.233.50.103’, ‘port’: 4257}, {‘ip’: ‘221.8.243.135’, ‘port’: 4275}, {‘ip’: ‘183.147.208.157’, ‘port’: 4234}, {‘ip’: ‘117.24.80.213’, ‘port’: 4228}, {‘ip’: ‘111.127.117.242’, ‘port’: 4285}, {‘ip’: ‘27.150.95.69’, ‘port’: 4213}, {‘ip’: ‘114.238.197.108’, ‘port’: 4247}, {‘ip’: ‘49.74.57.244’, ‘port’: 4268}]

回答

这样?

a  =[{'ip': '123.181.150.193', 'port': 4241}, {'ip': '114.238.223.98', 'port': 4237}, {'ip': '114.233.50.103', 'port': 4257}, {'ip': '221.8.243.135', 'port': 4275}, {'ip': '183.147.208.157', 'port': 4234}, {'ip': '117.24.80.213', 'port': 4228}, {'ip': '111.127.117.242', 'port': 4285}, {'ip': '27.150.95.69', 'port': 4213}, {'ip': '114.238.197.108', 'port': 4247}, {'ip': '49.74.57.244', 'port': 4268}]
l_1= [x['ip'] for x in a]
l_2= [x['port'] for x in a]
print(dict(zip(l_1,l_2)))

炫意HTML5 » python 想到一个好玩的东西 就是如何将一个长得像字典的列表转换为字典

Java基础教程Android基础教程