python 3中的zip函数,以及一些巧妙应用

首先书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)     # 测试集解压缩
相对于每个都去分割,然后分别形成的形式,这样相对会简单很多。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值