python 中字符串中每隔一个逗号后的字符串拆分

发布于 2025-01-08 03:19:01 字数 170 浏览 1 评论 0原文

我有一个字符串,其中包含用逗号分隔的每个单词。我想在 python 中用每个其他逗号分割字符串。我该怎么做?

例如,"xyz,abc,jkl,pqr" 应该将 "xyzabc" 作为一个字符串,将 "jklpqr" 作为另一个字符串

I have string which contains every word separated by comma. I want to split the string by every other comma in python. How should I do this?

eg, "xyz,abc,jkl,pqr" should give "xyzabc" as one string and "jklpqr" as another string

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

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

发布评论

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

评论(5

╰つ倒转 2025-01-15 03:19:01

在每个逗号上拆分,然后重新连接对可能更容易,

>>> original = 'a,1,b,2,c,3'
>>> s = original.split(',')
>>> s
['a', '1', 'b', '2', 'c', '3']
>>> alternate = map(''.join, zip(s[::2], s[1::2]))
>>> alternate
['a1', 'b2', 'c3']

这是您想要的吗?

It's probably easier to split on every comma, and then rejoin pairs

>>> original = 'a,1,b,2,c,3'
>>> s = original.split(',')
>>> s
['a', '1', 'b', '2', 'c', '3']
>>> alternate = map(''.join, zip(s[::2], s[1::2]))
>>> alternate
['a1', 'b2', 'c3']

Is that what you wanted?

柠檬心 2025-01-15 03:19:01

分开,又重新结合。

因此,拆分:

In [173]: "a,b,c,d".split(',')
Out[173]: ['a', 'b', 'c', 'd']

并重新加入:

In [193]: z = iter("a,b,c,d".split(','))
In [194]: [a+b for a,b in zip(*([z]*2))]
Out[194]: ['ab', 'cd']

这是可行的,因为 ([z]*2) 是两个元素的列表,这两个元素都是相同的迭代器 z。因此,zip 从 z 中获取第一个元素,然后是第二个元素来创建每个元组。

这也可以用作单行代码,因为在 [foo]*nfoo 仅计算一次,无论它是变量还是更复杂的表达式:

In [195]: [a+b for a,b in zip(*[iter("a,b,c,d".split(','))]*2)]
Out[195]: ['ab', 'cd']

我已经还删除了一对括号,因为一元 * 的优先级低于二进制 *

感谢 @pillmuncher 指出,可以使用 izip_longest 进行扩展,以处理具有奇数个元素的列表:(

In [214]: from itertools import izip_longest

In [215]: [a+b for a,b in izip_longest(*[iter("a,b,c,d,e".split(','))]*2, fillvalue='')]
Out[215]: ['ab', 'cd', 'e']

请参阅:http://docs.python.org/library/itertools.html#itertools.izip_longest )

Split, and rejoin.

So, to split:

In [173]: "a,b,c,d".split(',')
Out[173]: ['a', 'b', 'c', 'd']

And to rejoin:

In [193]: z = iter("a,b,c,d".split(','))
In [194]: [a+b for a,b in zip(*([z]*2))]
Out[194]: ['ab', 'cd']

This works because ([z]*2) is a list of two elements, both of which are the same iterator z. Thus, zip takes the first, then second element from z to create each tuple.

This also works as a oneliner, because in [foo]*n foo is evaluated only once, whether or not it is a variable or a more complex expression:

In [195]: [a+b for a,b in zip(*[iter("a,b,c,d".split(','))]*2)]
Out[195]: ['ab', 'cd']

I've also cut out a pair of brackets, because unary * has lower precedence than binary *.

Thanks to @pillmuncher for pointing out that this can be extended with izip_longest to handle lists with an odd number of elements:

In [214]: from itertools import izip_longest

In [215]: [a+b for a,b in izip_longest(*[iter("a,b,c,d,e".split(','))]*2, fillvalue='')]
Out[215]: ['ab', 'cd', 'e']

(See: http://docs.python.org/library/itertools.html#itertools.izip_longest )

鸵鸟症 2025-01-15 03:19:01
str = "a,b,c,d".split(",")
print ["".join(str[i:i+2]) for i in range(0, len(str), 2)]
str = "a,b,c,d".split(",")
print ["".join(str[i:i+2]) for i in range(0, len(str), 2)]
清欢 2025-01-15 03:19:01

只需将每个逗号分开,然后将其组合回来:

splitList = someString.split(",")
joinedString = ','.join([splitList[i - 1] + splitList[i] for i in range(1, len(splitList), 2)]

Just split on every comma, then combine it back:

splitList = someString.split(",")
joinedString = ','.join([splitList[i - 1] + splitList[i] for i in range(1, len(splitList), 2)]
故人的歌 2025-01-15 03:19:01

像这样

"a,b,c,d".split(',')

Like this

"a,b,c,d".split(',')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文