-4

taking in subnet and ip address, performing a logical AND to retrieve the network address. My question is, how do I return a list of four octets, rather than just have all of the values stored into a list.

Example, my function is printing ['1','1','1','1'...], where as I want to print ['11111111','11111111','11111100','11111111']

def logicalAnd(ip, subnet):

test = list()
for ipOctect, subnetOctect in zip(ip, subnet):
    for i,j in zip(ipOctect, subnetOctect):
        octet = (i and j)
        test.append(octet)
print(test)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Lexie
  • 11

1 Answers1

0

Sounds to me you just want to chunk the data into groups of 8. This is done best here:

How do you split a list into evenly sized chunks?

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

mylist = ['1']*32        

print(["".join(i) for i in chunks(mylist,8)])

output:

['11111111', '11111111', '11111111', '11111111']
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28