python创建列表_python创建与遍历List二维列表的方法
python 創(chuàng)建List二維列表
lists = [[] for i in range(3)] # 創(chuàng)建的是多行三列的二維列表
for i in range(3):
lists[0].append(i)
for i in range(5):
lists[1].append(i)
for i in range(7):
lists[2].append(i)
print("lists is:", lists)
# lists is: [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6]]
使用二維列表索引遍歷二維列表
?注意python中二維列表和matlab以及C和JAVA中一樣,不需要每行中列的數(shù)量相等
sum_0 = 0
for i in range(len(lists)):
for j in range(len(lists[i])):
print(lists[i][j])
sum_0 += lists[i][j]
print("The sum_0 of Lists:", sum_0)
# 0
# 1
# 2
# 0
# 1
# 2
# 3
# 4
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# The sum of Lists: 34
使用二維列表句柄遍歷二維列表
sum_1 = 0
for i in lists:
for j in i:
sum_1 += j
print("The sum_1 of Lists:", sum_1)
# The sum_1 of Lists: 34
總結(jié)
以上所述是小編給大家介紹的python創(chuàng)建與遍歷List二維列表的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
本文標(biāo)題: python創(chuàng)建與遍歷List二維列表的方法
本文地址: http://www.cppcns.com/jiaoben/python/268585.html
總結(jié)
以上是生活随笔為你收集整理的python创建列表_python创建与遍历List二维列表的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模型训练 准确率下降_手写批量线性回归算
- 下一篇: python 常用内置函数_Python