zip
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。
我们可以使用 list() 转换来输出列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
#以下是使用zip的方法
re_ip_address = ['115.231.5.230', '114.239.147.135', '123.163.97.18', '223.243.255.167', '121.232.148.178',
'121.233.109.15', '114.239.250.93', '58.23.230.24', '220.168.52.245', '183.166.102.92',
'219.159.38.209', '47.100.21.174', '163.204.245.172', '118.180.166.195', '182.35.86.221']
re_port = ['44524', '9999', '9999', '65309', '9000', '9999', '9999', '8118', '39107', '9999', '56210', '8021', '9999',
'8060', '9999']
for ip, port in zip(re_ip_address, re_port):
print(ip + ':' + port)
运行结果
#以下是使用下标的方法
re_ip_address = ['115.231.5.230', '114.239.147.135', '123.163.97.18', '223.243.255.167', '121.232.148.178',
'121.233.109.15', '114.239.250.93', '58.23.230.24', '220.168.52.245', '183.166.102.92',
'219.159.38.209', '47.100.21.174', '163.204.245.172', '118.180.166.195', '182.35.86.221']
re_port = ['44524', '9999', '9999', '65309', '9000', '9999', '9999', '8118', '39107', '9999', '56210', '8021', '9999',
'8060', '9999']
for i in range(len(re_ip_address)):
ip = re_ip_address[i]
port = re_port[i]
ip_one = ip + ":" + port
print(ip_one)