在嵌套列表中添加元素
我有这个问题,我想计算列表和子列表中有多少个整数。
我有这段代码,但似乎不起作用:
x = [[1, 3], [0], [2, -4]]
count = 0pos = 0
for i in x[pos]:count = count + len(x[pos])pos = pos + 1
print(count)
预期 5,因为有 5 个元素。
I have this problem, I want to count how many integers in a list AND sub-lists.
I have this code but doesn't seem to be working:
x = [[1, 3], [0], [2, -4]]
count = 0pos = 0
for i in x[pos]:count = count + len(x[pos])pos = pos + 1
print(count)
Expected 5 because there are 5 elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已将您的代码编辑为以下内容:
I've edited your code to the following: