有两个列表,phone_list 有2000个元素,a_list 有20个以内的元素。以phone_list的元素为主,每取一个phone就需要取一个a来配对,需求是,a_list中的元素,被取得机率尽可能是接近相同的。写一个python的演示代码。
import itertools
# 示例数据
phone_list = [1,2,3,4,5,6,7]
a_list = ['a1', 'a2', 'a3', 'a4', 'a5']
# 创建一个循环迭代器
a_cycle = itertools.cycle(a_list)
for phone in phone_list:
print(phone,next(a_cycle))
# 删除对迭代器的引用
del a_cycle
Python 中,itertools.cycle 创建的迭代器不会自动释放内存,因为它是一个无限循环的迭代器。然而,当你不再引用这个迭代器时,Python 的垃圾回收机制会自动回收这些对象占用的内存。