简体   繁体   English

使用列表中的字符串进行切片分配

[英]Slice Assignment with a String in a List

I did quite a bit of perusing, but I don't have a definite answer for the concept that I'm trying to understand. 我做了很多细读,但对于我想要理解的概念 ,我没有明确的答案。

In Python, if I take a list, such as: 在Python中,如果我列出一个列表,例如:

L1=['muffins', 'brownies','cookies']

And then attempted to replace the first pointer to an object in the list, namely 'muffins' by using the code: 然后尝试使用代码替换第一个指向列表中对象的指针,即“松饼”:

L1[0:1] = 'cake'

I would get a list L1: 我会得到一个列表L1:

['c', 'a', 'k', 'e', 'brownies', 'cookies']

Yet if I took the same list and performed the operation (now with the 4 elements from the string cake): 然而,如果我采用相同的列表并执行操作(现在使用字符串蛋糕中的4个元素):

L1[0:4] = ['cake'] # presumably, it's now passing the string cake within a list? (it passed into the modified list shown above)

I get the output I initially desired: 我得到了我最初想要的输出:

['cake', 'brownies', 'cookies']

Can anyone explain why that is, exactly? 任何人都能解释为什么会这样吗? I'm assuming that when I take cake initially without it being in a "list", it breaks the string into its individual characters to be stored as references to those characters as opposed to a single reference to a string... 我假设当我最初拿蛋糕而不是在“列表”中时,它会将字符串分解为单个字符,以存储为对这些字符的引用,而不是对字符串的单个引用...

But I'm not entirely sure. 但我不完全确定。

Two important points: 两个要点:

  1. Slice assignment takes an iterable on the right-hand side, and replaces the elements of the slice with the objects produced by the iterable. 切片赋值在右侧采用可迭代 ,并使用iterable生成的对象替换切片的元素。
  2. In Python, strings are iterable: iterating over a string yields its characters. 在Python中,字符串是可迭代的:迭代字符串会产生字符。

Thus 从而

L1[0:1] = 'cake'

replaces the first element of L1 with the individual characters of 'cake' . 'cake' 的单个字符替换L1的第一个元素。

To replace the first element with the string 'cake' , simply write: 要用字符串'cake'替换第一个元素,只需写:

L1[0] = 'cake'

or, using the slice assignment syntax: 或者,使用切片赋值语法:

L1[0:1] = ['cake']

If you specify a slice, the righthand side is presumed to be a list/tuple (actually, any iterable - but watch out for generators that produce an indefinite number of values). 如果指定切片,则右侧被假定为列表/元组(实际上,任何可迭代 - 但要注意产生无限数量值的生成器)。

To replace an item in a list, use: 要替换列表中的项目,请使用:

my_list[0] = "cake"

(You could also do (你也可以这样做

my_list[0:1] = ["cake"]

if you really want to use a list slice. 如果你真的想使用列表切片。

Here are a couple other relevant refs: slice assignment more slice assignment 以下是其他几个相关的参考: 切片分配 更多切片分配

Think of strings as being a sequence container that stores characters. 将字符串视为存储字符的序列容器。 When you try to do assignments that way, it adds each item in the character sequence to the list. 当您尝试以这种方式进行分配时,它会将字符序列中的每个项目添加到列表中。 By wrapping "cake" in its own 1-element list first (let's call it L2), you're instead adding each element of L2 to L1 -- it does not recursively split up sequence containers beyond the outermost sequence. 通过首先将“cake”包装在自己的1元素列表中(让我们称之为L2),您将L2的每个元素添加到L1中 - 它不会递归地将序列容器拆分到最外层序列之外。

L1 = ['muffins', 'brownies','cookies']
L2 = ['cake']
L1[0:1] = L2
print L1
['cake', 'brownies', 'cookies']

This is also true for other nested sequence objects. 对于其他嵌套序列对象也是如此。 Try experimenting with more nested sequence objects like this: 尝试尝试更多嵌套的序列对象,如下所示:

L3 = [['pie', 'eclairs'], ['bacon', 'chocolate']]
L1[0:1] = L3
print L1
[['pie', 'eclairs'], ['bacon', 'chocolate'], 'brownies', 'cookies']

It's also worth noting that if you don't care about order/positioning in the list, you can use append() and not have to worry about your string getting split up: 还值得注意的是,如果你不关心列表中的顺序/定位,你可以使用append()而不必担心你的字符串被拆分:

L1 = ['muffins', 'brownies','cookies']
L1.append('cake')
print L1
['muffins', 'brownies', 'cookies', 'cake']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM