关于python:将带有冒号分隔项的拆分列表转换为元组列表

Split list with colon seperated items into list of tuples

如果我有以下列表:

['Host: www.perdu.com', 'Connection: keep-alive', 'Cache-Control:
max-age=0', 'Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111
Safari/537.36', 'DNT: 1', 'Accept-Encoding: gzip,deflate,sdch',
'Accept-Language: en-US,en;q=0.8,es;q=0.6,fr;q=0.4', '']

我怎样才能得到一个元组列表tuple_list,如:

1
2
3
4
5
tuples_list = [
    ('Host', 'www.perdu.com'),
    ('Connection', 'keep-alive'),
     ....
]


你听说过列表理解吗?

1
2
headers = ['Host: www.perdu.com', 'Connection: keep-alive', 'Cache-Control: max-age=0', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36', 'DNT: 1', 'Accept-Encoding: gzip,deflate,sdch', 'Accept-Language: en-US,en;q=0.8,es;q=0.6,fr;q=0.4', '']
headers = [i.split(': ') for i in headers]

会回报你:

1
[['Host', 'www.perdu.com'], ['Connection', 'keep-alive'], ['Cache-Control', 'max-age=0'], ['Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8'], ['User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'], ['DNT', '1'], ['Accept-Encoding', 'gzip,deflate,sdch'], ['Accept-Language', 'en-US,en;q=0.8,es;q=0.6,fr;q=0.4'], ['']]

如果你这样做,你可以获得一个元组列表:

1
2
headers = ['Host: www.perdu.com', 'Connection: keep-alive', 'Cache-Control: max-age=0', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36', 'DNT: 1', 'Accept-Encoding: gzip,deflate,sdch', 'Accept-Language: en-US,en;q=0.8,es;q=0.6,fr;q=0.4', '']
headers = [tuple(i.split(': ')) for i in headers]

这将返回您的期望:

1
[('Host', 'www.perdu.com'), ('Connection', 'keep-alive'), ('Cache-Control', 'max-age=0'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8'), ('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'), ('DNT', '1'), ('Accept-Encoding', 'gzip,deflate,sdch'), ('Accept-Language', 'en-US,en;q=0.8,es;q=0.6,fr;q=0.4'), ('',)]


怎么样:

1
tuples_list = [tuple(item.split(":")) for item in thelist]

这使用列表推导来迭代列表中的每个项目,将其拆分为:,并将每个结果数组转换为元组。


您可以在列表解析中使用split(':')来实现此目的。

1
2
3
4
5
6
7
8
9
10
>>> tuples_list = [tuple(i.split(':')) for i in l]
>>> tuples_list
[('Host', ' www.perdu.com'),
 ('Connection', ' keep-alive'),
 ('Cache-Control', ' max-age=0'),
 ('Accept', ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8'),
 ('User-Agent', ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'),
 ('DNT', ' 1'),
 ('Accept-Encoding', ' gzip,deflate,sdch'),
 ('Accept-Language', ' en-US,en;q=0.8,es;q=0.6,fr;q=0.4')]