python list初始化

a = [1, 2, 3]
b = [1, 2, 3,]

没看懂,这两个数组有什么区别?在django中有非常多的以“逗号”结束的数组。求指教

阅读 3.1k
2 个回答

有这样的吗?我知道tuple当只有一个元素时是需要加的,list加不加都一样。

a=[1,2,3]
b=[1,2,3,]
print(a)
print(b)
print(a==b)
a.append(1)
b.append(1)
print(a)
print(b)
print(a==b)
def test(c):
    print(type(c))
test((1,))
test((1))
test(a)
test(b)

俩者结果没有任何区别,但是在函数传参的时候,加上,Python会做不同的处理
比如(12)和(12,),但是列表传参没有发现不同,运行结果如下

[Running] python "e:\Rare\python\demo_1\demo_1.py"
[1, 2, 3]
[1, 2, 3]
True
[1, 2, 3, 1]
[1, 2, 3, 1]
True
<class 'tuple'>
<class 'int'>
<class 'list'>
<class 'list'>

[Done] exited with code=0 in 0.116 seconds
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题