Python:获取列表中第一个字符串的第一个字符?

问题:Python:获取列表中第一个字符串的第一个字符?

如何从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'

回答 0

你几乎是对的。最简单的方法是

mylist[0][0]   # get the first character from the first item in the list

mylist[0][:1]  # get up to the first character in the first item in the list

也可以。

你想结束的第一个字符(字符零)后,未启动的第一个字符(字符零),这是以后有什么在你的问题的手段的代码。

You almost had it right. The simplest way is

mylist[0][0]   # get the first character from the first item in the list

but

mylist[0][:1]  # get up to the first character in the first item in the list

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.


回答 1

获取裸python字符串的第一个字符:

>>> mystring = "hello"
>>> print(mystring[0])
h
>>> print(mystring[:1])
h
>>> print(mystring[3])
l
>>> print(mystring[-1])
o
>>> print(mystring[2:3])
l
>>> print(mystring[2:4])
ll

从python列表的第一个位置的字符串中获取第一个字符:

>>> myarray = []
>>> myarray.append("blah")
>>> myarray[0][:1]
'b'
>>> myarray[0][-1]
'h'
>>> myarray[0][1:3]
'la'

许多人在这里绊倒了,因为他们混淆了Python列表对象的运算符和Numpy ndarray对象的运算符:

Numpy操作与python列表操作非常不同。

绕过Python的“列表切片,索引,子集”和Numpy的“掩码,切片,子集,索引,然后是numpy的增强式花式索引”这两个相互冲突的世界。

这两个视频为我清除了一切:

PyCon 2015撰写的“使用NumPy消除循环,快速进行数值计算”:https ://youtu.be/EEUXKG97YRw ? t = 22m22s

Alexandre Chabot LeClerc撰写的“ NumPy初学者| SciPy 2016教程”:https ://youtu.be/gtejJ3RCddE ? t = 1h24m54s

Get the first character of a bare python string:

>>> mystring = "hello"
>>> print(mystring[0])
h
>>> print(mystring[:1])
h
>>> print(mystring[3])
l
>>> print(mystring[-1])
o
>>> print(mystring[2:3])
l
>>> print(mystring[2:4])
ll

Get the first character from a string in the first position of a python list:

>>> myarray = []
>>> myarray.append("blah")
>>> myarray[0][:1]
'b'
>>> myarray[0][-1]
'h'
>>> myarray[0][1:3]
'la'

Many people get tripped up here because they are mixing up operators of Python list objects and operators of Numpy ndarray objects:

Numpy operations are very different than python list operations.

Wrap your head around the two conflicting worlds of Python’s “list slicing, indexing, subsetting” and then Numpy’s “masking, slicing, subsetting, indexing, then numpy’s enhanced fancy 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


回答 2

从0开始在python中建立索引。您编写了[1:]在任何情况下都不会返回第一个字符-这将为您返回其余的字符串(第一个字符除外)。

如果具有以下结构:

mylist = ['base', 'sample', 'test']

并希望为第一个字符串(项目)获取拳头字符:

myList[0][0]
>>> b

如果所有第一个字符:

[x[0] for x in myList]
>>> ['b', 's', 't']    

如果您有文字:

text = 'base sample test'
text.split()[0][0]
>>> b

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:

mylist = ['base', 'sample', 'test']

And want to get fist char for the first one string(item):

myList[0][0]
>>> b

If all first chars:

[x[0] for x in myList]
>>> ['b', 's', 't']    

If you have a text:

text = 'base sample test'
text.split()[0][0]
>>> b

回答 3

尝试mylist[0][0]。这应该返回第一个字符。

Try mylist[0][0]. This should return the first character.