【问题标题】:python program to reverse the list iterable problem [duplicate]python程序反转列表可迭代问题[重复]
【发布时间】:2019-11-29 01:31:56
【问题描述】:

列表旋转包括获取第一个元素并将其移动到末尾。例如,如果我们旋转列表 [1,2,3,4,5],我们会得到 [2,3,4,5,1]。如果我们再次旋转它,我们会得到 [3,4,5,1,2]。

这里有一些例子来说明你的函数应该如何工作。

rotatelist([1,2,3,4,5],1)
[2, 3, 4, 5, 1]

rotatelist([1,2,3,4,5],3)
[4, 5, 1, 2, 3]

rotatelist([1,2,3,4,5],12)
[3, 4, 5, 1, 2]

我尝试成功编写代码但遇到问题,当我连接列表时,我收到错误:int iterable error 但是当我使用append 程序成功执行时,请解释一下这里的概念是我的python 代码:

def rotatelist(l,k):
    if k<0:
        return l
    new_list=l[::]    
    while k>0:
        temp=new_list[0]
        new_list=new_list[1:]
        new_list=new_list+list(temp)
        k-=1
   return new_list

【问题讨论】:

  • list 将一个可迭代对象作为参数,而您将传递给它一个 int。您应该写 [temp] 而不是 list(temp),以便获取包含此唯一值的列表。

标签: python list


【解决方案1】:
def rotatelist(l, n):
    n = n % len(l)
    return l[n:]  + l[:n]

print(rotatelist([1,2,3,4,5], 12))

打印:

[3, 4, 5, 1, 2]

【讨论】:

    【解决方案2】:

    这段代码:

    def rotatelist(l,k):
        if k<0:
            return l
        new_list=l[::]    
        while k>0:
            temp=new_list[0]
            new_list=new_list[1:]
            new_list.append(temp)
            k-=1
        return new_list
    

    或者这个:

    def rotatelist(l,k):
        if k<0:
            return l
        new_list=l[::]    
        while k>0:
            temp=new_list[0]
            new_list=new_list[1:]
            new_list=new_list+[temp]
            k-=1
        return new_list
    

    将产生以下输出:

    rotatelist([1,2,3,4,5],1)
    [2, 3, 4, 5, 1]                                                                                                                                               
    
    rotatelist([1,2,3,4,5],3)
    [4, 5, 1, 2, 3]                                                                                                                                               
    
    rotatelist([1,2,3,4,5],12)
    [3, 4, 5, 1, 2]   
    

    您遇到错误是因为您尝试使用整数创建列表:

    temp = 1
    print(list(temp))
    

    输出:

    TypeError: 'int' object is not iterable 
    

    下面的示例可以正常工作,因为您将整数放入列表中:

    temp = 1
    print([temp])
    

    输出:

    [1]
    

    【讨论】:

    • 为什么不使用 list(temp) 列表函数还将其中存在的项目转换为列表
    • 因为您正在尝试使用整数创建列表。我在答案中添加了另外 2 个示例。
    • 好的,我在 list() 中得到了它,我们只能处理字符串、元组、dict 等可迭代对象,因为 int 是不可迭代的,我们不能在 list() 中使用,但我们要么将其设为列表使用 [] 或使用 append ,谢谢合作
    【解决方案3】:

    您可以使用deque 及其rotate 方法来做到这一点。

    from collections import deque
    
    
    def rotatelist(l, k):
        dq = deque(l)
        dq.rotate(-k)
        return list(dq)
    

    【讨论】:

      【解决方案4】:
      from collections import deque
      d = deque([1,2,3,4,5])
      d.rotate(-3)
      Ouput:
      [4, 5, 1, 2, 3]
      

      或者在你的情况下

      def rotatelist(l,k):
          if k<0:
              print(l)
          new_list=l[::]    
          while k>0:
              temp=new_list[0]
              new_list=new_list[1:]
              new_list=new_list+[temp]
              k-=1
          print(new_list)
      
      rotatelist([1,2,3,4,5],3)
      

      输出:

      [4, 5, 1, 2, 3]
      

      【讨论】:

      • 为什么不使用 list(temp) 列表函数还将其中存在的项目转换为列表
      • @AnkushRasgon 您的 deque 示例不会产生预期结果 - .rotate() 中必须使用负数。
      • @PranavSharma [] 只是将 temp 包装在一个列表中。 list(temp) 将其转换为列表。
      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 2016-10-29
      • 2021-10-16
      • 2013-05-09
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多