My Python for loop is causing a MemoryError. How can I optimize this?

后端 未结 5 1078
抹茶落季
抹茶落季 2021-01-19 06:45

I\'m trying to compile a list of all the MAC address Apple devices will have. oui.txt tells me Apple has been assigned 77 MAC ranges to use. These ranges come i

5条回答
  •  难免孤独
    2021-01-19 07:17

    range(1, 1291845633) creates a list of 1,291,845,632 elements (several GB) all at once. Use xrange(1, 1291845633) instead and it will generate elements as you need them instead of all at once.

    Regardless, it looks like you want something more like this:

    for mac in apple_mac_range: 
        for i in xrange(16777216): 
            print mac, i 
    

    Of course it's quite likely that a list of 1.3e+9 MAC addresses will not be very useful. If you want to see if a given MAC address is an Apple device, you should just check to see if the 3-byte prefix is in the list of 77. If you're trying to do access control by giving a router or something a list of all possible MAC addresses, it's unlikely that the device will accept 1.3e+9 items in its list.

提交回复
热议问题