是否可以在单个语句中拆分和分配字符串?

发布于 2024-12-19 00:39:15 字数 209 浏览 0 评论 0原文

是否可以拆分字符串并将某些单词分配给一个元组?

例如:

a = "Jack and Jill went up the hill"
(user1, user2) = a.split().pick(1,3) # picks 1 and 3 element in the list.

这样的单衬可能吗?如果是的话,语法是什么。

Can a string be split and some of the words be assigned to a tuple?

For instance:

a = "Jack and Jill went up the hill"
(user1, user2) = a.split().pick(1,3) # picks 1 and 3 element in the list.

Is such a one liner possible? If so what is the syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

凉墨 2024-12-26 00:39:15

如果你想变得更奇特,你可以使用 operator.itemgetter

返回一个可调用对象,该对象使用操作数的 __getitem__() 方法从其操作数中获取项目。如果指定了多个项目,则返回查找值的元组。

示例:

>>> from operator import itemgetter
>>> pick = itemgetter(0, 2)
>>> pick("Jack and Jill went up the hill".split())
('Jack', 'Jill')

或者作为单行(没有导入):

>>> user1, user2 = itemgetter(0, 2)("Jack and Jill went up the hill".split())

If you want to get fancy, you could use operator.itemgetter:

Return a callable object that fetches item from its operand using the operand’s __getitem__() method. If multiple items are specified, returns a tuple of lookup values.

Example:

>>> from operator import itemgetter
>>> pick = itemgetter(0, 2)
>>> pick("Jack and Jill went up the hill".split())
('Jack', 'Jill')

Or as a one-liner (w/o the import):

>>> user1, user2 = itemgetter(0, 2)("Jack and Jill went up the hill".split())
冷…雨湿花 2024-12-26 00:39:15

您可以执行类似的操作

a = "Jack and Jill went up the hill"
user1, _, user2, _ = a.split(" ", 3)

,其中 _ 表示我们不关心该值,而 split(" ", 3) 将字符串拆分为 4 段。

You can do something like this

a = "Jack and Jill went up the hill"
user1, _, user2, _ = a.split(" ", 3)

where _ means that we don't care of the value, and split(" ", 3) split the string in 4 segments.

初懵 2024-12-26 00:39:15

切片支持步骤参数

a = "Jack and Jill went up the hill"
(user1 , user2) = a.split()[0:4:2] #picks 1 and 3 element in the list

,但虽然可以用 Python 编写时髦的单行代码,但它肯定不是此类练习的最佳语言。

Slicing supports a step parameter

a = "Jack and Jill went up the hill"
(user1 , user2) = a.split()[0:4:2] #picks 1 and 3 element in the list

but while it's possible to write funky oneliners in Python for sure it's not the best language for that kind of exercise.

↙温凉少女 2024-12-26 00:39:15

这样就可以解决问题:

user1, user2 = a.split()[0::2][:2]

选择序列的前两个元素,从 2 in 2 开始计数。

This does the trick:

user1, user2 = a.split()[0::2][:2]

Pick the first two elements of the sequence counting from 2 in 2.

慕巷 2024-12-26 00:39:15

我宁愿用两行来做,但这是一行:

user1, user2 = [token for (i, token) in enumerate(a.split()) if i in (0, 2)]

这就是我会做的(只是为了可读性和减少在将来需要更改时引入错误的机会)。

tokens = a.split()
user1 = tokens[0]
user2 = tokens[2]

I'd rather do this in two lines, but here's a one-liner:

user1, user2 = [token for (i, token) in enumerate(a.split()) if i in (0, 2)]

Here's what I would do instead (just for readability and less chance of introducing bugs if needs to be changed in the future).

tokens = a.split()
user1 = tokens[0]
user2 = tokens[2]
凹づ凸ル 2024-12-26 00:39:15

我首先想到的是:

>>> a = "Jack and Jill went up the hill"
>>> [e for n, e in enumerate(a.split()) if n in (0, 2)]
['Jack', 'Jill']

如果你想知道:enumerate 生成元组,其中第一个元素是渐进数,第二个元素是枚举可迭代元素。

编辑:正如@kindall的评论中所说,最后一步是:

>>> user1, user2 = [e for n, e in enumerate(a.split()) if n in (0, 2)]
>>> user1
'Jack'
>>> user2
'Jill'

但我选择不做作业只是为了让示例更切题(抱歉,如果这让某人感到困惑)。

The first that comes to my mind is:

>>> a = "Jack and Jill went up the hill"
>>> [e for n, e in enumerate(a.split()) if n in (0, 2)]
['Jack', 'Jill']

In case you wonder: enumerate generates tuples with a progressive number as first element and an element of the enumerated iterable as second.

EDIT: As said in the comments by @kindall, the final step would be:

>>> user1, user2 = [e for n, e in enumerate(a.split()) if n in (0, 2)]
>>> user1
'Jack'
>>> user2
'Jill'

but I chose not to do the assignment just to keep the example more to the point (sorry if this confused somebody).

心不设防 2024-12-26 00:39:15

Python 3.8 开始,并引入赋值表达式 (PEP 572):= 运算符),我们可以首先命名 text.split() 表达式,然后在同一行中使用它的各个部分并创建这(user1, user2) 元组:

# text = 'Jack and Jill went up the hill'
_, (user1, user2) = (parts := text.split()), (parts[0], parts[2])
# (user1, user2) = ('Jack', 'Jill')

Starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), we can first name the text.split() expression in order to then use its parts within the same line and create the (user1, user2) tuple:

# text = 'Jack and Jill went up the hill'
_, (user1, user2) = (parts := text.split()), (parts[0], parts[2])
# (user1, user2) = ('Jack', 'Jill')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文