Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python编写字典和列表_Python_Python 2.7 - Fatal编程技术网

用Python编写字典和列表

用Python编写字典和列表,python,python-2.7,Python,Python 2.7,我知道可以通过重新初始化新对象来清空列表/字典,但我希望保留初始对象 在Python3中,也可以使用clear()将列表中的所有元素清空(删除)。但是如何在Python2.7中实现呢 是否从字典中删除所有键 有关列表: l[:] = [] 保留同一对象但清空内容是否尝试使用dict.clear?有关列表,请参阅存在于…@deceze中,而不是list.clear,这就是列表try del命令del list[:]

我知道可以通过重新初始化新对象来清空列表/字典,但我希望保留初始对象

  • 在Python3中,也可以使用
    clear()
    将列表中的所有元素清空(删除)。但是如何在Python2.7中实现呢

  • 是否从字典中删除所有键

  • 有关列表:

    l[:] = [] 
    

    保留同一对象但清空内容

    是否尝试使用
    dict.clear
    ?有关列表,请参阅存在于…@deceze中,而不是
    list.clear
    ,这就是列表try del命令del list[:]