如何"繁殖"在python中列出2个?

时间:2016-03-17 15:33:36

标签: python

我有2个名单:

{
'ip': ['10.100.1.38', '10.100.1.39', '10.100.1.35'], 
 'port': ['3380', '3381', '3382', '3383', '3384', '3385', '3386', '3387']
}

我想获得清单:

[
 "10.100.1.38::3380"... "10.100.1.38::3387",
 "10.100.1.39::3380"... "10.100.1.39::3387",
 "10.100.1.35::3380"... "10.100.1.38::3387" 
]

4 个答案:

答案 0 :(得分:5)

我会尝试:

output = []
for ip in dict['ip']:
    for port in dict['port']:
        output.append(ip + "::" + port)

答案 1 :(得分:1)

您可以使用列表理解在一行中整齐地完成此操作:

out = [ip + "::" + port for ip in input["ip"] for port in input["port"]]

答案 2 :(得分:1)

from itertools import product

res = [ip + "::" + port for ip, port in product(d['ip'], d['port'])]

答案 3 :(得分:0)

Caused by: java.net.ConnectException: Connection refused: connect
                at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_65]
                at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.8.0_65]
                at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_65]
                at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_65]
                at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_65]
                at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_65]
                at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_65]
                at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_65]
...