pythonmemoryerror的时候扩大虚拟内存有用吗_我的Python for循环导致MemoryError。我该如何优化呢?...

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...

This leaves me the last three HEX digits to append. That's 16^6. A total of 1291845632 Apple MAC addresses.

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

This gives me a MemoryError... How can I optimize it?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值