从循环输出中创建列表

时间:2018-01-02 17:25:16

标签: python python-2.7 list for-loop

data = ['network 10.185.16.64 255.255.255.224','network 55.242.33.0 255.255.255.0','network 55.242.154.0 255.255.255.252']

pref_network_find = re.findall('(\S+\s+255.255.255.\w+)',str(data))

mydict = {"255.255.255.0":24,"255.255.255.128":25,"255.255.255.192":26,"255.255.255.224":27,"255.255.255.240":28,"255.255.255.248":29,"255.255.255.252":30}

for i in pref_network_find:
 splitlines = i.split()
 for word in splitlines:
    if word in mydict:
        i = i.replace(word,str(mydict[word]))
        pref = print (i)
listi = []
for line in pref_network_find:
   listi.append(i)
print (listi)

10.185.16.64 27
55.242.33.0 24
55.242.154.0 30
['55.242.154.0 30', '55.242.154.0 30', '55.242.154.0 30']

Process finished with exit code 0

我试图在最后将['55.242.154.0 30', '55.242.33.0 24', '10.185.16.64 27']作为list1,但在这里无法理解我的错误。你能帮帮我吗?

3 个答案:

答案 0 :(得分:2)

您不需要使用正则表达式获取初始拼接和加入的IP;相反,只需使用str.split()

import re
data = ['network 10.185.16.64 255.255.255.224','network 55.242.33.0 255.255.255.0','network 55.242.154.0 255.255.255.252']
mydict = {"255.255.255.0":24,"255.255.255.128":25,"255.255.255.192":26,"255.255.255.224":27,"255.255.255.240":28,"255.255.255.248":29,"255.255.255.252":30}

final_list = sorted(['{} {}'.format(b, mydict[c]) for a, b, c in [i.split() for i in data]], key=lambda x:map(int, re.split('\.|\s', x)), reverse=True)

输出:

['55.242.154.0 30', '55.242.33.0 24', '10.185.16.64 27']

答案 1 :(得分:0)

您的代码错误,因为您在此处附加了错误的索引i

for line in pref_network_find:
    listi.append(i)

我们在前一个循环中的i = 55.242.154.0中有最后一个值。您应该使用line代替i或直接追加for循环

data = ['network 10.185.16.64 255.255.255.224','network 55.242.33.0 255.255.255.0','network 55.242.154.0 255.255.255.252']

pref_network_find = re.findall('(\S+\s+255.255.255.\w+)',str(data))

mydict = {"255.255.255.0":24,"255.255.255.128":25,"255.255.255.192":26,"255.255.255.224":27,"255.255.255.240":28,"255.255.255.248":29,"255.255.255.252":30}

listi = []

for i in pref_network_find:
    splitlines = i.split()
    for word in splitlines:
        if word in mydict:
            listi.append(i.replace(word, str(mydict[word])))

print(listi)

答案 2 :(得分:0)

显然,它会在最后打印30,因为你的代码

for i in pref_network_find:
 splitlines = i.split()
 for word in splitlines:
    if word in mydict:
        i = i.replace(word,str(mydict[word]))
        pref = print (i)

执行后我才30岁。你正在使用旧变量' i'像这样

for line in pref_network_find:
   listi.append(i)

所以是的,代码正在完成它的工作,我是30,并且它会在你的结果中附加30。

正确的代码是这样的。

import re

data = ['network 10.185.16.64 255.255.255.224','network 55.242.33.0 255.255.255.0','network 55.242.154.0 255.255.255.252']
pref_network_find = re.findall('(\S+\s+255.255.255.\w+)',str(data))
mydict = {"255.255.255.0":24,"255.255.255.128":25,"255.255.255.192":26,"255.255.255.224":27,"255.255.255.240":28,"255.255.255.248":29,"255.255.255.252":30}
listi = []
for i in pref_network_find:
 splitlines = i.split()
 for word in splitlines:
    if word in mydict:
        i = i.replace(word,str(mydict[word]))
        pref = print (i)
        listi.append(i)

print (listi)

如果我在这里错了,请纠正我,也许你想要别的东西,但是,这就是我对你的问题所理解的。