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

后端 未结 5 1050
抹茶落季
抹茶落季 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:32

    Others have answered your actual question, but I'm not really sure that's what's warranted here. Why don't you just create a class which implements __contains__ to test MAC address algorithmically? I presume you're getting a MAC and you want to test if it's possibly an iPhone MAC, so you could implement that class and then just do something like:

    if found_mac in MACTester:
      ...do work...
    

    Alternatively if you really do want an iterable sequence, you should at least use a generator instead of actually trying to fit them all in memory.

提交回复
热议问题