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个..按理应该全部啊..请问什么原因导致的呢?
或者有其他方法转变么?

2个回答

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

u012186186
阿Sir永不为奴 谢谢,说的很明白了
6 个月之前 回复

你可以这样做:
from collection import defaultdict

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问