简体   繁体   English

在python中替换字符串中的每个字符

[英]Replacing every character in a string in python

I'm working on a script where I'm trying to create a list of strings that, at each subsequent character, have a sub for an input character. 我正在编写一个脚本,试图在该脚本中创建一个字符串列表,在每个后续字符处都有一个用于输入字符的子项。

my_replacement = input("Replacement character?: ")
orig_strng = ABCDEFGHIJKL
lst =[]
for i in range(len(orig_strng)):
    a = function to replace orig_strng[i] with my_replacements
    lst.append(a)

If my input is X, the appended lst output from each iteration would look like: XBCDEFGHIJKL, AXCDEFGHIJKL, ABXDEFGHIJKL, etc. 如果我的输入是X,则每次迭代后附加的第一个输出将类似于:XBCDEFGHIJKL,AXCDEFGHIJKL,ABXDEFGHIJKL等。

I get that strings themselves are immutable - so i can't directly edit them. 我知道字符串本身是不可变的-因此我无法直接对其进行编辑。 How would I be able to tackle this in a python-ic way? 我将如何以python-ic的方式解决这个问题?

def replace_each(s,letter):
    for i,original_letter in enumerate(s):
        yield "{0}{1}{2}".format(s[:i],letter,s[i+1:])

print(list(replace_each("ABCDEFGHI","x")))

is pretty pythonic I think 我认为是相当pythonic

You can convert the string into a list, perform the replacement, then convert it back into a string with join 您可以将字符串转换为列表,执行替换,然后使用join将其转换回字符串

def foo(my_string, i, replacement):
    my_list = list(my_string)
    my_list[i] = replacement
    return "".join(my_list)

foo(orig_strng, 1, "X")
#'AXCDEFGHIJKL'

The string method replace() works nicely here for the example case but not necessarily for all cases. 字符串方法replace()在这里适用于示例情况,但不一定适用于所有情况。

my_replacement = input("Replacement character?: ")
orig_strng = ABCDEFGHIJKL
lst =[]
for i in range(len(orig_strng)):
    a = orig_strng.replace(orig_string[i], my_replacement)
    lst.append(a)

If you wanted to, you could do this with a list comprehension: 如果您愿意,可以通过列表理解来做到这一点:

a = [orig_string.replace(orig_string[i], my_replacement) for i in orig_string]

Is this what you mean? 你是这个意思吗?

def foo(string,i,replacement):
    return string[:i]+replacement+string[i+1:]

I find that translation tables and the translate function often come in handy. 我发现翻译表和翻译功能通常派上用场。 Like you said, strings are immutable, so the translate function returns a copy with the translation completed. 就像您说的那样,字符串是不可变的,因此translation函数返回翻译完成的副本。

The example below allows you to input a replacement string (not just a single character), and it will replace all occurrences of the character(s) to be translated with the provided input character(s). 下面的示例使您可以输入替换字符串(不仅是单个字符),而且还将所有出现的要翻译的字符替换为提供的输入字符。

from string import maketrans

original = 'ABCDEFGHIJK'
n = len(original)
user_in = "XYZ" #can also just be a single character X
m = min(len(user_in), n)
for i in range(n):
    trans_table = maketrans((original[i:] + original[:i])[:m], user_in[:m])
    new = original.translate(trans_table)
    print new

Output 产量

XYZDEFGHIJK
AXYZEFGHIJK
ABXYZFGHIJK
ABCXYZGHIJK
ABCDXYZHIJK
ABCDEXYZIJK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM