如何在Python中重复字符串中的单个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38273353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:35:40  来源:igfitidea点击:

How to repeat individual characters in strings in Python

pythonstring

提问by Jason S

I know that

我知道

"123abc" * 2

evaluates as "123abc123abc", but is there an easy way to repeat individual letters N times, e.g. convert "123abc"to "112233aabbcc"or "111222333aaabbbccc"?

评估为"123abc123abc",但是有没有一种简单的方法可以将单个字母重复 N 次,例如转换"123abc""112233aabbcc""111222333aaabbbccc"

回答by Bahrom

What about:

关于什么:

>>> s = '123abc'
>>> n = 3
>>> ''.join([char*n for char in s])
'111222333aaabbbccc'
>>> 

(changed to a list comp from a generator expression as using a list comp inside join is faster)

(从生成器表达式更改为列表 comp,因为在 join 内使用列表 comp更快

回答by alecxe

An alternative itertools-problem-overcomplicating-style option with repeat(), izip()and chain():

itertools使用repeat(),izip()和的替代-problem-overcomplicating-style 选项chain()

>>> from itertools import repeat, izip, chain
>>> "".join(chain(*izip(*repeat(s, 2))))
'112233aabbcc'
>>> "".join(chain(*izip(*repeat(s, 3))))
'111222333aaabbbccc'

Or, "I know regexes and I'll use it for everything"-style option:

或者,“我知道正则表达式,我会用它做所有事情”风格的选项:

>>> import re
>>> n = 2
>>> re.sub(".", lambda x: x.group() * n, s)  # or re.sub('(.)', r'' * n, s) - thanks Eduardo
'112233aabbcc'

Of course, don't use these solutions in practice.

当然,不要在实践中使用这些解决方案。

回答by sarnthil

Or another way to do it would be using map:

或者另一种方法是使用map

"".join(map(lambda x: x*7, "map"))

回答by Gerges

And since I use numpy for everything, here we go:

因为我对所有东西都使用 numpy,所以我们开始:

import numpy as np
n = 4
''.join(np.array(list(st*n)).reshape(n, -1).T.ravel())

回答by khappe khappe

here is my naive solution

这是我天真的解决方案

text = "123abc"
result = ''
for letters in text:
    result += letters*3

print(result)

output: 111222333aaabbbccc

输出:111222333aaabbbccc

回答by Ano. Nymous

If you want to repeat individualletters you can just replace the letter with n letters e.g.

如果你想重复单个字母,你可以用 n 个字母替换字母,例如

>>> s = 'abcde'
>>> s.replace('b', 'b'*5, 1)
'abbbbbcde'

回答by julienc

@Bahrom's answer is probably clearer than mine, but just to say that there are many solutions to this problem:

@Bahrom 的答案可能比我的更清楚,但只是说这个问题有很多解决方案:

>>> s = '123abc'
>>> n = 3
>>> reduce(lambda s0, c: s0 + c*n, s, "")
'111222333aaabbbccc'

Note that reduceis not a built-in in python 3, and you have to use functools.reduceinstead.

请注意,这reduce不是 python 3 中的内置函数,您必须使用它functools.reduce

回答by Eduardo Cuesta

Or using regular expressions:

或者使用正则表达式:

>>> import re
>>> s = '123abc'
>>> n = 3
>>> re.sub('(.)', r'' * n, s)
'111222333aaabbbccc'

回答by af3ld

Another way:

其它的办法:

def letter_repeater(n, string):
    word = ''
    for char in list(string):
        word += char * n
    print word

letter_repeater(4, 'monkeys')


mmmmoooonnnnkkkkeeeeyyyyssss