首页 新闻 会员 周边 捐助

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

0
[已解决问题] 解决于 2021-03-31 14:52

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

[{'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}]

木三136的主页 木三136 | 初学一级 | 园豆:159
提问于:2021-03-30 19:45
< >
分享
最佳答案
0

这样?

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)))
奖励园豆:5
小小咸鱼YwY | 老鸟四级 |园豆:3312 | 2021-03-31 09:41

首先感谢您的回答,
{"code":0,"data":[{"ip":"42.59.103.60","port":4270,"expire_time":"2021-03-31 09:38:11"},{"ip":"125.111.151.126","port":4245,"expire_time":"2021-03-31 09:38:11"},{"ip":"123.181.151.223","port":4241,"expire_time":"2021-03-31 09:38:11"},{"ip":"122.239.165.94","port":4245,"expire_time":"2021-03-31 09:38:11"},{"ip":"223.156.85.248","port":4243,"expire_time":"2021-03-31 09:38:11"},{"ip":"182.107.233.82","port":4214,"expire_time":"2021-03-31 09:29:56"},{"ip":"125.78.217.167","port":4206,"expire_time":"2021-03-31 09:38:11"},{"ip":"124.112.4.207","port":4264,"expire_time":"2021-03-31 09:38:11"},{"ip":"120.38.34.115","port":4213,"expire_time":"2021-03-31 09:38:11"},{"ip":"114.239.0.168","port":4256,"expire_time":"2021-03-31 09:38:11"}],"msg":"0","success":true}
这是他原本的数据 是一个json 文件
我首先时使用pop将data数据取出
然后发现取出的数据是一个list 所以我才有了这个帖子
但是最后发现 这个list 可以按字典的方式取出数据
像是
ip = ip_list.pop('data')
num = random.randint(0, len(ip))
# print("{0}:{1}".format(ip[num].get('ip'), ip[num].get('port')))

木三136 | 园豆:159 (初学一级) | 2021-03-31 09:45

不清楚问什么,如果您知道的话,希望能帮我解答一下

木三136 | 园豆:159 (初学一级) | 2021-03-31 09:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册