首先书zip()函数的使用:>>> help(zip)
Help on class zip in module builtins:
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
zip()函数的形式:zip(iter1 [,iter2 [...]])
参数:一个至多个迭代器。
返回值:元祖列表>>> a = list(zip([1,2,3],[4,5,6],[4,5,6,7,8]))
>>> a
[(1, 4, 4), (2, 5, 5), (3, 6, 6)]
>>> c,d,e = zip(*a)
>>> c
(1, 2, 3)
>>> d
(4, 5, 6)
>>> e
(4, 5, 6)
其中zip(*a)与zip(a)相反,理解为解压
因为Python语言一般都很简洁,当我们有一个元素列表,和一个与之对应的标签列表,然而要在某一个位置分割他们,然后将两边的都单独拿出来形成单独的列表,我们就可以使用zip函数进行操作,可以简化代码data_class_list = list(zip(data_list, class_list)) # zip压缩合并,将数据与标签对应压缩
random.shuffle(data_class_list) # 将data_class_list乱序
index = int(len(data_class_list) * test_size) + 1 # 训练集和测试集切分的索引值
train_list = data_class_list[index:] # 训练集
test_list = data_class_list[:index] # 测试集
train_data_list, train_class_list = zip(*train_list) # 训练集解压缩
test_data_list, test_class_list = zip(*test_list) # 测试集解压缩相对于每个都去分割,然后分别形成的形式,这样相对会简单很多。