日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python两个list合并成字典_Python将两个list合并为一个字典

發布時間:2025/5/22 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python两个list合并成字典_Python将两个list合并为一个字典 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

將list合并成dict,采集網站的代理IP格式如下:

ip_nodes=['177.220.136.22','195.178.157.216' ,'179.85.149.192']

port_nodes=['80','8080','3128']

方法一:

tmp = dict(zip(ip_nodes, port_nodes))

方法二(使用list表達式):

tmp={ ip_nodes[i]:port_nodes[i] for i in range(len(port_nodes))}

方法三:

combined = {}

for i in range(len(ip_nodes)) :

combined[ip_nodes[i]] = port_nodes[i]

方法四:lambda:

dict(map(lambda x,y:[x,y],ip_nodes,port_nodes))

#!/usr/bin/env python

# coding: utf-8

import requests, urllib2, time

from lxml import etree

url = "http://www.xicidaili.com/wt/"

headers = {'content-type': 'text/html',

'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}

ip_check_url = 'http://www.mshishang.com/a/20161027/149787.html'

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'

socket_timeout = 10

# Check proxy

def check_proxy(ip, port):

try:

ip = "%s:%s" % (ip, port)

proxy_ip = {"http": ip}

proxy_handler = urllib2.ProxyHandler(proxy_ip)

# proxy = urllib2.ProxyHandler(proxy_ip)

opener = urllib2.build_opener(proxy_handler)

# opener.addheaders = [('User-agent', user_agent)] #這句加上以后無法正常檢測,不知道是什么原因。

urllib2.install_opener(opener)

req = urllib2.Request(ip_check_url)

time_start = time.time()

conn = urllib2.urlopen(req)

# conn = urllib2.urlopen(ip_check_url)

time_end = time.time()

detected_pip = conn.read()

proxy_detected = True

except urllib2.HTTPError, e:

print "ERROR: Code ", e.code

return False

except Exception, detail:

print "ERROR: ", detail

return False

return proxy_detected

try:

r = requests.get(url, headers=headers)

r.raise_for_status() # 如果響應狀態碼不是 200,就主動拋出異常

tree = etree.HTML(r.content)

ip_nodes = tree.xpath("//tr//td[2]/text()")

port_nodes = tree.xpath("//tr//td[3]/text()")

# print port_nodes

"""

# blablabla...

combined = {}

for i in range(len(students)) :

combined[students[i]] = courses[i]

"""

# tmp = zip(ip_nodes, port_nodes)

# dict((y, x) for x, y in tmp)

tmp = dict(zip(ip_nodes, port_nodes))

print tmp

for (ip, port) in tmp.items():

proxy_detected = check_proxy(ip, port)

if proxy_detected:

print (" WORKING: " + ip+":"+port)

else:

print " FAILED: %s " % (ip,)

# tmp1={ ip_nodes[i]:port_nodes[i] for i in range(len(port_nodes))}

except requests.RequestException as e:

print(e)

總結

以上是生活随笔為你收集整理的python两个list合并成字典_Python将两个list合并为一个字典的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。