示例:
我们在网上查找zip用法,自己却打印不出结果:
import os
a = [1, 2, 3]
b = ['a', 'b', 'c']
c = zip(a, b)
print(c) #<zip object at 0x00000000038421C8>
这时候我们可以先看下c有哪些属性dir(变量名)
import os
a = [1, 2, 3]
b = ['a', 'b', 'c']
c = zip(a, b)
print(c) #<zip object at 0x00000000038421C8>
print(dir(c))
'''['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']'''
可以看到有__iter__属性,所以我们可以用遍历方式打印出来
import os
a = [1, 2, 3]
b = ['a', 'b', 'c']
c = zip(a, b)
print(c) #<zip object at 0x00000000038421C8>
print(dir(c)) #['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
for arg in c:
print(arg) #(1, 'a') (2, 'b') (3, 'c')