Python 如何提取索引而不是字符串中的所有字符?

Python 如何提取索引而不是字符串中的所有字符?,python,for-loop,if-statement,indexing,Python,For Loop,If Statement,Indexing,我想指示计算机,如果字符串“a”中的一个字符也是字符串“b”中的一个字符,则删除该字符(仅此一个字符),但是下面的代码所做的是一次性删除字符串中的所有相同字符。有什么帮助吗 我尝试使用a[I]和b[I]来替换每个循环的索引,但是下面的代码需要完全更改,因为我不能再执行“a中的I”和“b中的if I”,因为我需要对“I”进行整数引用 e、 g.a=“askhfidshf” b=“fhshdhfojej” 采用一个参数,该参数指定应替换多少个引用。默认情况下,它将替换所有 我认为字符串操作有点复杂,

我想指示计算机,如果字符串“a”中的一个字符也是字符串“b”中的一个字符,则删除该字符(仅此一个字符),但是下面的代码所做的是一次性删除字符串中的所有相同字符。有什么帮助吗

我尝试使用a[I]和b[I]来替换每个循环的索引,但是下面的代码需要完全更改,因为我不能再执行“a中的I”和“b中的if I”,因为我需要对“I”进行整数引用

e、 g.a=“askhfidshf” b=“fhshdhfojej”

采用一个参数,该参数指定应替换多少个引用。默认情况下,它将替换所有


我认为字符串操作有点复杂,因此,我建议将它转换成数组,因为您可以使用索引方法,而您的挑战将获得整数引用。看看我想出的代码

def permutation_checker(a, b):
    array_a = [i for i in a] # converting the string to an array
    array_b = [i for i in b] # converting the string to an array

    for i in range(len(array_a )):
        if array_a[i] in array_b:
            char_index = array_b.index(array_a[i]) # getting the first index of the value array_a[i] within array_b
            array_b[char_index] = ''

    if array_b == [''] * len(b):
        print(f'{a} is a permutation of {b}')

谢谢分享。事实上,我对数组的处理更好,这是我的第一个想法,但我对整数也有同样的问题。但我现在明白你的解决方案是如何解决这个问题的了!谢谢
str.replace(old, new[, count])
def permutation_checker(a, b):
    array_a = [i for i in a] # converting the string to an array
    array_b = [i for i in b] # converting the string to an array

    for i in range(len(array_a )):
        if array_a[i] in array_b:
            char_index = array_b.index(array_a[i]) # getting the first index of the value array_a[i] within array_b
            array_b[char_index] = ''

    if array_b == [''] * len(b):
        print(f'{a} is a permutation of {b}')