散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表
# coding=utf-8
"""
需求:读取文件,每行为一段 json 字符串,总共100 行,有些字符串中包含 real 字段,有些未包含
将包含、未包含的 real 字段均匀分为 5 份(容器)中
"""
a1, a2, a3, a4, a5 = [], [], [], [], []
def process(data_list):
for index, i in enumerate(data_list):
if index % 5 == 0:
a1.append(i)
elif index % 5 == 1:
a2.append(i)
elif index % 5 == 2:
a3.append(i)
elif index % 5 == 3:
a4.append(i)
else:
a5.append(i)
if __name__ == '__main__':
# 读取文件,将包含 real、未包含 real 字段的数据分别添加到 m、n 中
m = list(range(1, 31)) # 含 real 字段
n = list(range(31, 101)) # 未含 real 字段
process(m)
process(n)
print(a1)
print(a2)
print(a3)
print(a4)
print(a5)
运行结果:
[1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96]
[2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 82, 87, 92, 97]
[3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58, 63, 68, 73, 78, 83, 88, 93, 98]
[4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79, 84, 89, 94, 99]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]