阿Sir永不为奴 2019-07-25 12:20 采纳率: 0%
浏览 11031

python用zip()列表转字典 返回为不重复 键 的字典.什么原因?

学习python..
想爬取免费代理,但是列表转换为字典的时候出现下面的情况.

prot_h=['HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTP', 'HTTPS', 'HTTPS', 'HTTP', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTPS', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTP', 'HTTPS']

add_ip=['115.219.104.72:8010', '113.121.21.165:9999', '120.83.104.102:9999', '1.197.16.57:9999', '117.91.130.62:9999', '120.83.104.50:9999', '121.233.227.52:9999', '222.189.191.253:9999', '182.96.243.35:8118', '175.155.77.103:61234', '120.84.100.109:9999', '1.198.73.127:9999', '58.253.154.112:9999', '113.121.22.151:9999', '115.219.35.38:8118', '1.197.204.118:9999', '171.12.112.52:9999', '118.181.226.216:36430', '61.178.149.237:59042', '175.155.140.195:1133', '117.91.132.252:9999', '117.91.133.113:9999', '120.83.106.49:9999', '1.197.11.236:9999', '218.73.135.248:61234', '1.198.73.187:9999', '115.207.36.195:61234', '115.53.20.254:9999', '58.253.157.221:9999', '120.84.101.233:9999', '59.32.37.109:8010', '1.198.73.236:9999', '49.86.182.47:9999', '1.199.30.231:9999', '121.233.227.153:9999', '117.91.251.92:9999', '112.85.131.147:9999', '121.236.8.216:9999', '144.123.69.118:9999', '123.163.97.164:9999', '144.123.70.245:9999', '120.83.109.57:9999', '120.83.108.181:9999', '1.198.73.160:9999', '120.83.111.9:9999', '121.233.207.30:9999', '222.189.190.200:9999', '1.197.203.80:9999', '125.109.192.59:9999', '125.110.75.200:8118', '120.83.105.15:9999', '123.163.97.238:9999', '120.83.97.131:9999', '49.86.176.144:9999', '120.83.109.18:9999', '27.43.190.188:9999', '120.79.147.254:9000', '59.52.185.7:808', '117.91.133.226:9999', '222.94.212.20:8118', '120.83.97.122:9999', '1.197.204.180:9999', '120.83.107.185:9999', '114.230.69.86:9999', '117.91.232.47:9999', '27.43.185.62:9999', '1.198.72.58:9999', '1.199.31.109:9999', '123.169.34.154:9999', '114.230.69.191:9999', '223.241.117.134:8010', '120.83.104.172:9999', '113.121.22.250:9999', '117.91.132.100:9999', '114.230.86.124:9999', '220.189.116.83:8888', '182.34.35.48:9999', '27.43.185.183:9999', '180.119.68.103:9999', '113.121.20.168:9999', '113.124.86.225:9999', '117.91.130.41:9999', '1.198.73.10:9999', '182.34.33.27:9999', '180.119.68.88:9999', '120.83.96.25:9999', '182.34.33.222:9999', '1.197.16.111:9999', '113.121.21.172:9999', '123.163.96.44:9999', '180.119.68.42:9999', '112.85.129.168:9999', '1.198.73.38:9999', '182.34.35.62:38922', '121.233.206.32:9999', '120.83.104.251:9999', '112.85.130.39:9999', '115.53.21.163:9999', '180.119.141.206:9999', '123.169.34.184:21526']

dic=zip(prot_h,add_ip)
print(dict(dic))

运行结果为 #{'HTTP': '180.119.141.206:9999', 'HTTPS': '123.169.34.184:21526'}

只有2个..按理应该全部啊..请问什么原因导致的呢?
或者有其他方法转变么?

  • 写回答

3条回答 默认 最新

  • BuXianShan 2019-07-25 14:17
    关注

    字典中的键是有类型限制的。1、不允许一个键对应多个值。必须明确一条原则:每个键只能对应一个项。也就是说:一键对应多个值是不允许的(像列表、元组和其他字典这样的容器对象是可以的)。 当有键发生冲突(即字典键重复赋值),取最后(最近)的赋值。Python并不会因字典中的键存在冲突而产生一个错误,它不会检查键的冲突是因为如果真这样做的话,在每个键-值对赋值的时候都会做检查,这将会占用一定量的内存。

    评论
  • 吃鸡王者 2019-07-26 01:39
    关注

    你可以这样做:
    from collection import defaultdict

    dic=defaultdict(list)
    for key,value in zip(prot_h,add_ip):
    dic[key].append(value)
    print(dic)

    评论
  • Kappuccinno 2021-04-12 13:13
    关注

    紧接上面的ip地址和对应url的描述,后面代码可以这么写,先创建一个空字典,遍历所有元素,如果已有ip在字典里,则往对应的值列表中添加元素,无ip则抛出keyerror错误,用except接着,创建一个列表放置第一个元素

    zipdict={}
    for each in range(len(prot_h)):
        try:
            zipdict[prot_h[each]].append(add_ip[each])
        except KeyError:
            zipdict[prot_h[each]]=[add_ip[each]]
    print(zipdict)

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部