例子1
List1 = []
List1.append([1,2]) # Can not List1 = List1.append([1,2])
print(List1)
List2 = []
List2+=[1,2]
print(List2)
输出:
[[1, 2]]
[1, 2]
例子2
myList1 = [ ]
myList2 = [ ]
listA = [1,2,3]
listB = ["a","b","c"]
myList1.append(listA)
myList1.append(listB)
myList2 = myList2+listA+listB
print(myList1,'\n',myList2)
输出:
[[1, 2, 3], ['a', 'b', 'c']]
[1, 2, 3, 'a', 'b', 'c']
例子3
也可以让list之间的加法和 append 一样,通过人为加一层括号
myList1 = torch.tensor([[1,2,3],[4,5,6] ])
myList2 = torch.tensor([[7,8,9],[41,51,61] ])
myList = [myList1]+[myList2]
print(myList,'\n')
print(torch.cat(myList,0))
输出:
[tensor([[1, 2, 3],
[4, 5, 6]]), tensor([[ 7, 8, 9],
[41, 51, 61]])]
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[41, 51, 61]])
如果用 append
myList1 = torch.tensor([[1,2,3],[4,5,6] ])
myList2 = torch.tensor([[7,8,9],[41,51,61] ])
myList = []
myList.append(myList1)
myList.append(myList2)
print(myList,'\n')
print(torch.cat(myList,0))
输出:
[tensor([[1, 2, 3],
[4, 5, 6]]), tensor([[ 7, 8, 9],
[41, 51, 61]])]
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[41, 51, 61]])