我的Python for循环导致MemoryError.我该如何优化呢? [英] My Python for loop is causing a MemoryError. How can I optimize this?
问题描述
我正在尝试编译Apple设备将拥有的所有MAC地址的列表. oui.txt
告诉我,Apple已分配了77个MAC范围供使用.这些范围的形式为:
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 in the form of:
00:00:00
00:11:11
etc...
这给我留下了最后三个十六进制数字.那是16^6
.共有1291845632个Apple MAC地址.
This leaves me the last three HEX digits to append. That's 16^6
. A total of 1291845632 Apple MAC addresses.
我遇到的问题是编写程序来创建这些MAC地址的列表.这是我当前的代码:
The problem I'm having is writing a program to create a list of these MAC addresses. Here's my current code:
import re
apple_mac_range = []
apple_macs = []
# Parse the HTML of http://standards.ieee.org/cgi-bin/ouisearch to get the MACs
with open('apple mac list', 'r') as f:
for line in f.readlines():
match = re.search(r'[\w\d]{2}-[\w\d]{2}-[\w\d]{2}', line)
if match:
apple_mac_range.append(match.group().split('-'))
for mac in apple_mac_range:
for i in range(1, 1291845633):
print i
这给了我MemoryError
...我如何对其进行优化?
This gives me a MemoryError
... How can I optimize it?
推荐答案
range(1, 1291845633)
一次创建一个包含1,291,845,632个元素(几个GB)的列表.使用xrange(1, 1291845633)
代替,它将根据需要生成元素,而不是一次生成所有元素.
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
当然,很可能1.3e + 9 MAC地址列表不是很有用.如果要查看给定的MAC地址是否为Apple设备,则应检查3字节前缀是否在77列表中.如果要通过提供路由器或其他工具来进行访问控制,列表中列出的所有可能的MAC地址,设备不太可能接受其列表中的1.3e + 9项.
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.
这篇关于我的Python for循环导致MemoryError.我该如何优化呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!