import logging
LOG_FORMAT = "%(asctime)s - %(threadName)s >>> %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
# filename='my.log',
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
# 使用: zip函数将每个迭代器的第一个元素配对在一起,然后将第二个元素配对在一起,依此类推
# 句法:zip(iterable0, iterable1, interable2, …)
# 将zip对象转换为元组---------------------------------
# 长度相等的列表
list_a = ['Bitcoin', 'Ethereum', 'Ripple', 'Litecoin', 'Bitcoin-cash']
list_b = ['BTC', 'ETH', 'XRP', 'LTC', 'BCH']
list_c = ['11605', '271', '0.335', '102', '347']
result = zip(list_a, list_b, list_c)
# print(tuple(result))
# 输出:(('Bitcoin', 'BTC', '11605'), ('Ethereum', 'ETH', '271'), ('Ripple', 'XRP', '0.335'), ('Litecoin', 'LTC', '102'), ('Bitcoin-cash', 'BCH', '347'))
# 长度相等的列表
float_list1 = [12.3, 10.99, 3.33, 2.97]
float_list2 = [78.13, 0.89, 4.6, 0.7]
float_zip = zip(float_list1, float_list2)
# print(tuple(float_zip))
# 输出:((12.3, 78.13), (10.99, 0.89), (3.33, 4.6), (2.97, 0.7))
# 长度不等的列表
list_a = [1, 2, 3, 4, 5]
list_b = ['one', 'two', 'three']
result = zip(list_a, list_b)
# print(tuple(result))
# 输出:((1, 'one'), (2, 'two'), (3, 'three'))
# 长度不等的元组
x = ("Joey", "Monica", "Ross")
y = ("Chandler", "Pheobe")
z = ("David", "Rachel", "Courtney")
result = zip(x, y, z)
# print(tuple(result))
# 输出:(('Joey', 'Chandler', 'David'), ('Monica', 'Pheobe', 'Rachel'))
# 压缩单个列表
list_1 = ['C', 'C++', 'Python', 'Java']
# print(tuple(list_1))
# 输出:('C', 'C++', 'Python', 'Java')
# 压缩单个列表
a = ['a1', 'a2', 'a3']
r = zip(*a)
# print(tuple(r))
# 输出:(('a', 'a', 'a'), ('1', '2', '3'))
# 压缩单个矩阵(一行三列)
mat = [[1,2,3]]
trans_mat = zip(*mat)
# print(tuple(trans_mat))
# 输出:((1,), (2,), (3,))
# 压缩单个矩阵(两行三列)
mat = [[1,2,3], [4,5,6]]
trans_mat = zip(*mat)
# print(tuple(trans_mat))
# 输出:((1, 4), (2, 5), (3, 6))
# 转换成字典
coin = ('Bitcoin', 'Ether', 'Ripple', 'Litecoin')
code = ('BTC', 'ETH', 'XRP', 'LTC')
# print(dict(zip(coin, code)))
# 输出:{'Bitcoin': 'BTC', 'Ether': 'ETH', 'Ripple': 'XRP', 'Litecoin': 'LTC'}
Zip函数教程(简单示例)
最新推荐文章于 2022-03-30 19:42:38 发布