python zip方法用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangxingfan316/article/details/80057972

参考:https://blog.csdn.net/xlinsist/article/details/51346523
http://www.runoob.com/python/python-func-zip.html

1.zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表

x=[1,2,3]
y=[4,5,6]
zipd = zip(x,y)
list(zipd)
#Out[42]: [(1, 4), (2, 5), (3, 6)]
y1=[4,5,6,7]
zipd1 = zip(x,y1)
list(zipd1)
#Out[46]:[(1, 4), (2, 5), (3, 6)]#匹配短的

2.numpy.bincount,输出索引出现的次数组成的数组

0索引出现了2次,1索引出现了3次,2索引出现了1,同样3出现1,4出现1
所以输出值的数量为list中的最大值+1,即索引[0,max+1]

list1 = [1,0,1,0,1,2,3,4]
np.bincount(list1)
#Out[54]: array([2, 3, 1, 1, 1])

在python机器学习基础教程中,对癌症的预测
即使用上述两个知识点

print('{}'.format({a:b for a ,b in zip(cancer.target_names,np.bincount(cancer.target))}))
#{'benign': 357, 'malignant': 212}
cancer.target_names
#array(['malignant', 'benign'],dtype='<U9')
np.bincount(cancer.target)
#Out[56]: array([212, 357])

猜你喜欢

转载自blog.csdn.net/wangxingfan316/article/details/80057972