List comprehension with multiple expressions and multiple conditions

Viewed 53

Before this wode was written that iterates over a file (ipaddress) using nested loops and a module (ipaddress). Now i would like to optimize using list comprehension.

import ipaddress
tmp1=open('Path\\Test-ip.txt', 'r+')
tmp2=open('Path\\-ip.txt', 'w+')
tmp1=tmp1.readlines()

for i in tmp1:
    i="".join(i.split())
     i=ipaddress.ip_network(i,False)
     for j in tmp1:
         j="".join(j.split())
         j=ipaddress.ip_network(j,False)
         if j != i:
             if ipaddress.IPv4Network(j).supernet_of(i):
                tmp2.write(str(i))
                tmp2.write('\n')

#Using List Comprehension

tmp1=open('Path\\Test-ip.txt', 'r+')
tmp2=open('Path\\Result-ip.txt', 'w+')
tmp1=tmp1.readlines()
tmp3=[ (("").join(i.split())) (("").join(j.split())) (ipaddress.ip_network(i)) (ipaddress.ip_network(j)) (tmp2.(write(str(i)))) for i in tmp1 for j in tmp1 if i!=j if ipaddress.IPv4Network(j).supernet_of(i)]
1 Answers

While generally a comprehension is faster than a for-loop, that is valid only if you are not doing things that are slow in the loop.

And (again in general) file I/O is much slower than doing calculations.

Note that you should always measure before trying to optimize. The bottleneck might not be where you think it is.

Run your script with python -m cProfile -s tottime yourscript.py your arguments and observe the output. That will tell you where the program is spending its time. On my website, I have a list of articles detailing profiling of real-world python programs. You can find the first in the list here. The others are referenced at the bottom of that page.

If file I/O turns out to be the bottleneck, using memory mapped files (the memmap module) will probably improve things.

Related