获取列表中第一个字符串的第一个字符?
如何从Python列表中的第一个字符串中获取第一个字符?
看来我可以使用 mylist[0][1:]
但这并没有给我第一个字符。
>>> mylist = []
>>> mylist.append("asdf")
>>> mylist.append("jkl;")
>>> mylist[0][1:]
'sdf'
How would I get the first character from the first string in a list in Python?
It seems that I could use mylist[0][1:]
but that does not give me the first character.
>>> mylist = []
>>> mylist.append("asdf")
>>> mylist.append("jkl;")
>>> mylist[0][1:]
'sdf'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你几乎是对的。最简单的方法是
但
也可行。
您希望在第一个字符(字符零)之后结束,而不是在第一个字符(字符零)之后开始,这就是问题中的代码的含义。
You almost had it right. The simplest way is
but
would also work.
You want to end after the first character (character zero), not start after the first character (character zero), which is what the code in your question means.
获取裸Python字符串的第一个字符:
获取Python列表第一个位置的字符串的第一个字符:
Numpy操作与Python有很大不同列表操作。
Python 具有列表切片、索引和子集化功能。 Numpy 具有掩码、切片、子集化、索引功能。
这两个视频让我明白了一切。
PyCon 2015 的“失去循环,使用 NumPy 进行快速数值计算”:
https://youtu.be/EEUXKG97YRw?t=22m22s
“NumPy 初学者 | SciPy 2016 教程”作者:亚历山大·查伯特·勒克莱尔:
https://youtu.be/gtejJ3RCddE?t=1h24m54s
Get the first character of a bare python string:
Get the first character from a string in the first position of a python list:
Numpy operations are very different than python list operations.
Python has list slicing, indexing and subsetting. Numpy has masking, slicing, subsetting, indexing.
These two videos cleared things up for me.
"Losing your Loops, Fast Numerical Computing with NumPy" by PyCon 2015:
https://youtu.be/EEUXKG97YRw?t=22m22s
"NumPy Beginner | SciPy 2016 Tutorial" by Alexandre Chabot LeClerc:
https://youtu.be/gtejJ3RCddE?t=1h24m54s
python 中的索引从 0 开始。您编写了 [1:] 这在任何情况下都不会返回第一个字符 - 这将返回字符串的其余部分(第一个字符除外)。
如果您有以下结构:
并且想要获取第一个字符串(项目)的第一个字符:
如果所有第一个字符:
如果您有文本:
Indexing in python starting from 0. You wrote [1:] this would not return you a first char in any case - this will return you a rest(except first char) of string.
If you have the following structure:
And want to get fist char for the first one string(item):
If all first chars:
If you have a text:
尝试
mylist[0][0]
。这应该返回第一个字符。Try
mylist[0][0]
. This should return the first character.如果您的列表包含非字符串,例如
mylist = [0, [1, 's'], 'string']
,那么此处的答案不一定有效。在这种情况下,使用next()
通过isinstance()
检查第一个字符串来查找第一个字符串就可以解决问题。请注意,
''[:1]
返回''
,而''[0]
吐出IndexError
,因此取决于在用例上,两者都可能有用。如果
mylist
中没有字符串,以上结果将导致StopIteration
。在这种情况下,一种可能的实现是将默认值设置为 None 并仅在找到字符串时才取第一个字符。If your list includes non-strings, e.g.
mylist = [0, [1, 's'], 'string']
, then the answers on here would not necessarily work. In that case, usingnext()
to find the first string by checking for them viaisinstance()
would do the trick.Note that
''[:1]
returns''
while''[0]
spitsIndexError
, so depending on the use case, either could be useful.The above results in
StopIteration
if there are no strings inmylist
. In that case, one possible implementation is to set the default value toNone
and take the first character only if a string was found.