社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python生成排列时添加到列表的方式不正确

atkayla • 5 年前 • 1492 次点击  

在本例中,为什么会生成正确的结果:

nxt = path + [nums[i]]

但不是:

nxt = path ; nxt += [nums[i]]
nxt = path ; nxt.append(nums[i])
def permute(nums):
    def dfs(nums, path):
      if not nums: # empty, done with path
          all_perms.append(path)
          return

      for i in range(len(nums)):
        nxt = path + [nums[i]]
        # nxt = path ; nxt += [nums[i]]
        # nxt = path ; nxt.append(nums[i])
        print(nxt)
        dfs(nums[:i] + nums[i+1:], nxt)

    all_perms = []
    dfs(nums, [])
    return all_perms
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54764
 
1492 次点击  
文章 [ 1 ]  |  最新文章 5 年前