2017-05-26 42 views
-1

我對編程非常陌生,我的第一語言是Python。我打印的IP地址的列表:如何在python中打印與列表中的對象匹配的東西

import socket 
dandanakka = ["madrasdeals.com","google.com","yahoo.com"] 
for i in dandanakka: 

    ip = socket.gethostbyname(i) 

    print ip 

結果是好的:

139.59.1.111 
172.217.26.174 
98.138.253.109 

有什麼辦法來打印我的結果匹配域名?

像 google.com - 172.217.26.174

回答

0

很簡單:你已經有一個變量i

import socket 
dandanakka = ["madrasdeals.com","google.com","yahoo.com"] 
for i in dandanakka: 

    ip = socket.gethostbyname(i) 
    print i, " - ", ip 
0

附加您的變量的字符串。內把這個你的: print('{domain} - {ip}'.format(domain=i, ip=ip))

0

你可以只遍歷列表,並打印在價值和socket.gethostbyname()結果:

for i in dandanakka: 
    ip = socket.gethostbyname(i) 
    print("{} - {}".format(i, ip))