我在很多地方看到对list使用切片赋值。我能够理解它在与(非默认)索引一起使用时的用法,但我不能理解它的用法,比如:
a_list[:] = ['foo', 'bar']这有什么不同
a_list = ['foo', 'bar']发布于 2012-04-15 02:04:33
差别是相当大的!在……里面
a_list[:] = ['foo', 'bar']您可以修改绑定到名称a_list的现有列表。另一方面,
a_list = ['foo', 'bar']为名称a_list指定一个新列表。
也许这会有所帮助:
a = a_list = ['foo', 'bar'] # another name for the same list
a_list = ['x', 'y'] # reassigns the name a_list
print a # still the original list
a = a_list = ['foo', 'bar']
a_list[:] = ['x', 'y'] # changes the existing list bound to a
print a # a changed too since you changed the objecthttps://stackoverflow.com/questions/10155951
复制相似问题