如何解决Python3:如何使用 enumerate() 打印列表中的总项目?
我没有使用 len
,而是使用 enumerate
函数。在其中,我如何获得项目总数?
示例代码:-
data = ['bat','cat','eat','fat','தொப்பி','எலி']
for itemNum,item in enumerate(data):
print(itemNum+1,end='')
结果:123456
预期结果:6
解决方法
您可以通过以下代码实现这一点
data = ['bat','cat','eat','fat','தொப்பி','எலி']
count = 0
for itemNum,item in enumerate(data):
count = count + 1
print(count)
,
i=0
for itemNum,item in enumerate(data):
i=i+1
print(i)
,
使用enumerate
是为了避免上面所说的count方法。比较时,我认为,枚举功能是高级的。希望我在这个问题上是正确的。
data = ['bat','எலி']
py_no= [ 0,1,2,3,4,5 ]
human= [ 1,5,6 ]
for itemNum,item in enumerate(data):
itemNum2tuple = tuple(str(itemNum+1)) # immutable; for human number adding +1
itemNum2tuple_string = itemNum2tuple[0] # to avoid brackets,...
print(itemNum2tuple_string) # indentation for the loop end.
print(int(itemNum2tuple_string)) # to further need,changing as integer
预期输出: 6
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。