0
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

Im trying to get ['55.242.154.0 30', '55.242.33.0 24', '10.185.16.64 27'] as list1 at the end, but cant understand my mistake here. Could you help me with that?

3 Answers 3

2

You do not need to garner the initial spliced and joined IPs with regex; instead, just use 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)

Output:

['55.242.154.0 30', '55.242.33.0 24', '10.185.16.64 27']
Sign up to request clarification or add additional context in comments.

3 Comments

I hate it when one-line-code shows up like this. I find it hard to read if not at least formatted correctly. You need to have all the information you need to understand the code on screen. Your sorted function takes 3 argument, and your code could gain great readibility if you would take 1 line per argument there. Your line is 152 characters long, and is BY FAR too long. The usual maximum is in-between 80 to 100. Don't you agree ? + if not for you, it is sad to teach young coders bad manners of coding right ?
@IMCoins you certainly have a point regarding readability, although I think for young coders using Python, it makes sense to introduce them to the features that Python has to offer including the powerful builtins and overall conciseness. Additionally, we should be trying to encourage users to be as Pythonic as possible (although Pythonic is somewhat subjective). Python has so much power, why not use it and enable others to use it?
I fully agree with you on the point Additionally, we should be trying to encourage users to be as Pythonic as possible (although Pythonic is somewhat subjective).. And all the subject around this in your message. BUT. I must say that you could have shown everything the same way on your example, as pythonic as you've done, just by inserting two '\n' and few '\t'. And that we should ALL be careful with this rule. This is like our constitution ! <3
0

Obviously, it will print 30 at the end because your this code

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)

i is 30 after execution. And you are using old variable 'i' like this

for line in pref_network_find:
   listi.append(i)

So yes the code is doing its job well, i is 30 and it is appending 30 to your result.

Correct code goes like this.

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)

Correct me if I am wrong here, maybe you want something else, however, this is what I understood by your question.

1 Comment

Yes,man, thank you! I'm just a begginer and learn new things writing my first script.
0

Your code is wrong because you are appending with wrong index i at here :

for line in pref_network_find:
    listi.append(i)

We have last value in i = 55.242.154.0 from previous loop. You should use line instead of i or append in for loop directly

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.