Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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,我有一个代码,我想打印这个列表向后,但它没有工作 它打印[2,4,6,8,10,10] def printBackwards (list) : i = len(list) while (i > 0): list.append(list[i-1]) i = i +1 print(list) return myslist = [2 , 4 , 6 , 8 , 10] printBackwards(myslist) 我想要的是打印它[10,8,6,4,2]怎么做

我有一个代码,我想打印这个列表向后,但它没有工作

它打印
[2,4,6,8,10,10]

def printBackwards (list) :
  i = len(list) 
  while (i > 0):
  list.append(list[i-1])
   i = i +1
  print(list)
  return

myslist = [2 , 4 , 6 , 8 , 10]
printBackwards(myslist)
我想要的是打印它
[10,8,6,4,2]
怎么做

编辑:我想使用我的代码,而不是
[::-1]
reverse()
或我看到的其他帖子中的内容。所以它不是复制品。我不想只从任何代码工作,但我想编辑我的代码工作。谢谢

def printBackwards(list_1): 
    list_2 = [] 
    i = len(list_1)  
    while (i > 0): 
        list_2.append(list_1[i-1]) 
        i = i -1 
    return list_2 


myslist = [2 , 4 , 6 , 8 , 10] 
print(printBackwards(myslist))                                                
创建本地列表并附加到该列表

输入和输出


您可以使用
insert

def print_backwards(in_list):
    out = []
    for x in in_list:
        out.insert(0, x)
    print(out)
使用
i-=1
减少索引,您可能需要创建一个新变量来存储结果

或者,您也可以将结果附加到现有列表中,并打印出循环后的第二部分

def printBackwards(x) :
    i = len(x)
    n = i
    while (i > 0):
        x.append(x[i-1])
        i -= 1
    print(x[n:])

i=i-1
i=i+1
我不使用
list
作为变量名您提供的代码无法打印
[2,4,6,8,10,10]
。您有一个无限循环,缩进不正确。另外,
返回在这里什么也不做。
def printBackwards(x) :
    i = len(x)
    n = i
    while (i > 0):
        x.append(x[i-1])
        i -= 1
    print(x[n:])