无法在Python中对列表的特定范围使用.reverse方法。

huangapple go评论83阅读模式
英文:

Why can't I use .reverse on a specific range in a list python?

问题

I am writing a simple script, where I have a list of characters, and I want to loop over this list and reverse the order of 3 elements at a time. Ex. List: a, b, c, d, e, f; Reversed list: c, b, a, f, e, d.

I tried to use
print(list[0:2].reverse())
however when I tried printing this, it outputted NONE.
My question is why does this happen?

英文:

I am writing a simple script, where I have a list of character and I want to loop over this list and reverse the ordered of 3 elements at a time. Ex. List: a,b,c,d,e,f ; Reversed list: c,b,a,f,e,d.

I tried to use
print(list[0:2].reverse())
however when I tried printing this it outputted NONE
My question is why does this happen?

FYI: I know there are other ways of doing this, I am just curious as to why this did not work.

答案1

得分: 3

以下是翻译好的内容:

lst[0:2].reverse()

这段代码对原始列表 lst 并没有太多影响,因为切片操作 lst[0:2] 返回一个新的独立列表对象,然后通过 reverse 方法原地反转它。原始的 lst 并不受影响。如果要改变原始列表,你需要使用切片赋值来将反转后的切片重新赋值给原始列表。例如:

lst[0:2] = lst[0:2][::-1]

你还可以使用 list.reverse 的兄弟方法 reversed,它会返回一个对象(一个反向顺序的迭代器):

lst[0:2] = reversed(lst[0:2])
英文:
lst[0:2].reverse()

does not do much to the original list lst, because the slice operation lst[0:2] returns a new independent list object which is then reversed in-place by reverse. The original lst is unimpressed by this. You have to feed the reversed slice back with slice assignment to mutate the original. E.g.:

lst[0:2] = lst[0:2][::-1]

You could also use list.reverse's brother reversed , which does return an object (an reverse order iterator):

lst[0:2] = reversed(lst[0:2])

答案2

得分: 1

当你执行切片操作,例如list[0:2]时,实际上创建了一个新的列表。

由于reverse操作作用于你提供的对象,但不返回任何内容,所以如果你执行print(list.reverse()),它仍然会打印出None,因为reverse不返回任何内容,但会修改你使用它的对象。

现在,由于切片创建了一个新的列表,当你执行list[0:2].reverse()时,你正在修改切片创建的列表,但这个修改会立即丢失,因为你没有将它分配给任何变量(而且你也不能这样做,因为reverse不返回任何值)。实质上,你正在创建一个切片列表,对其进行修改,然后永远失去它。

另外,你可以将切片分配给一个变量,然后对其使用reverse,但在你的情况下这可能没有用:

listed = ["a", "b", "c", "d", "e", "f"]
sliced = listed[0:2]
sliced.reverse()
print(sliced)  # 打印结果为 ['b', 'a']

希望这清楚了。

(最后的建议,不要将你的列表命名为"list",因为它会掩盖内置名称。)

英文:

When you do a slicing operation e.g list[0:2], you essentially create a new list

Since reverse operates on the object you give it, but doesn't return anything, if you were to do print(list.reverse()) it would still print None because reverse doesn't return anything, but modify the object you use it on.

Now, since slicing creates a new list, when you do list[0:2].reverse() , you are modifying the list created by the slicing, which is also immediately lost since you don't assign it to any variable (and you can't since reverse doesn't return a value). Essentially, you are creating a list with the slicing, modying it, and then losing it forever

Additionally, you can assign the slicing to a variable and then use reverse on it, but it wouldn't be useful in your case

listed= ["a", "b", "c", "d", "e", "f"]
sliced = listed[0:2]
sliced.reverse()
print(sliced) prints ['b', 'a']

Hope this is clear

(Last advice, don't call your list "list", it shadows the built-in name)

huangapple
  • 本文由 发表于 2023年6月15日 17:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定