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

时间:2022-01-07 20:25:20

How would I get the first character from the first string in a list in Python?

如何从Python的列表中的第一个字符串中获取第一个字符?

It seems that I could use mylist[0][1:] but that does not give me the first character.

似乎我可以使用mylist [0] [1:],但这不会给我第一个字符。

>>> mylist = []
>>> mylist.append("asdf")
>>> mylist.append("jkl;")
>>> mylist[0][1:]
'sdf'

4 个解决方案

#1


83  

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.

您希望在第一个字符(字符零)之后结束,而不是在第一个字符(字符零)之后开始,这是您的问题中的代码所意味着的。

#2


13  

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.

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

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


10  

Get the first character of a bare python string:

获取裸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

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

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

>>> 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:

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

Numpy operations are very different than python list operations.

Numpy操作与python list操作非常不同。

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".

围绕Python的“列表切片,索引,子集化”这两个相互冲突的世界,然后是Numpy的“屏蔽,切片,子集化,索引,然后是numpy的增强型花式索引”。

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

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

"NumPy Beginner | SciPy 2016 Tutorial" by Alexandre Chabot LeClerc: https://youtu.be/gtejJ3RCddE?t=1h24m54s

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

#4


4  

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

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

#1


83  

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.

您希望在第一个字符(字符零)之后结束,而不是在第一个字符(字符零)之后开始,这是您的问题中的代码所意味着的。

#2


13  

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.

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

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


10  

Get the first character of a bare python string:

获取裸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

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

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

>>> 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:

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

Numpy operations are very different than python list operations.

Numpy操作与python list操作非常不同。

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".

围绕Python的“列表切片,索引,子集化”这两个相互冲突的世界,然后是Numpy的“屏蔽,切片,子集化,索引,然后是numpy的增强型花式索引”。

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

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

"NumPy Beginner | SciPy 2016 Tutorial" by Alexandre Chabot LeClerc: https://youtu.be/gtejJ3RCddE?t=1h24m54s

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

#4


4  

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

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