简体   繁体   English

如何在python2.7的列表中弹出()元素

[英]How do I pop() elements inside the list in python2.7

I just want to ask if how should I pop certain elements inside the list. 我只想问是否应该在列表中弹出某些元素。

Let's say I have this list: 假设我有以下列表:

c = ['123','456','789']

When I type this: 当我输入以下内容时:

print c[0][0]

It prints a value '1' , 它输出值'1'

And somehow I want to delete the first element of the first value. 而且我想以某种方式删除第一个值的第一个元素。

So that the output will be: 这样输出将是:

c = ['23','456','789']

But I have a problem in using pop() . 但是我在使用pop()遇到问题。

I tried this but no luck: 我尝试了这个,但是没有运气:

c.pop(0, 0) # takes only one argument

Or 要么

c[0].pop(0) # string doesn't have an attribute pop

Is there a way to solve my dilemma? 有办法解决我的困境吗?

If this problem has a duplicate, please let me know. 如果此问题重复,请告诉我。

Strings are immutable. 字符串是不可变的。 As such, they can't be modified once created. 因此,它们一旦创建就无法修改。

If all you want to do is "remove" the first character of the first string in the list c you can use slicing (that returns a new string): 如果您要做的只是“删除”列表c第一个字符串的第一个字符,则可以使用切片(返回新字符串):

c[0] = c[0][1:]

Read more on slicing here: Explain Python's slice notation 在此处阅读更多有关切片的信息: 解释Python的切片符号

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

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