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

发布于 2024-11-30 09:11:59 字数 246 浏览 1 评论 0原文

如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清君侧 2024-12-07 09:11:59

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

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.

绿萝 2024-12-07 09:11:59

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

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:

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

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

巴黎夜雨 2024-12-07 09:11:59

python 中的索引从 0 开始。您编写了 [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
回心转意 2024-12-07 09:11:59

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

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

滥情稳全场 2024-12-07 09:11:59

如果您的列表包含非字符串,例如 mylist = [0, [1, 's'], 'string'],那么此处的答案不一定有效。在这种情况下,使用 next() 通过 isinstance() 检查第一个字符串来查找第一个字符串就可以解决问题。

next(e for e in mylist if isinstance(e, str))[:1]

请注意,''[:1] 返回 '',而 ''[0] 吐出 IndexError,因此取决于在用例上,两者都可能有用。

如果 mylist 中没有字符串,以上结果将导致 StopIteration。在这种情况下,一种可能的实现是将默认值设置为 None 并仅在找到字符串时才取第一个字符。

first = next((e for e in mylist if isinstance(e, str)), None)
first_char = first[0] if first else 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, using next() to find the first string by checking for them via isinstance() would do the trick.

next(e for e in mylist if isinstance(e, str))[:1]

Note that ''[:1] returns '' while ''[0] spits IndexError, so depending on the use case, either could be useful.

The above results in StopIteration if there are no strings in mylist. In that case, one possible implementation is to set the default value to None and take the first character only if a string was found.

first = next((e for e in mylist if isinstance(e, str)), None)
first_char = first[0] if first else None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文