假设“ab & ac”不是字母数字(因为 & 和空格)并且您输入了一个错字,这将解决问题。
def removeNonAlpha(i,j):
indexI = 0
indexJ = 0
while indexJ < len(j):
if not j[indexJ].isalnum():
j[indexJ] = i[indexI]
indexI += 1
indexJ += 1
return j
>>>i=["a","b", "c"]
>>>j=["abc","(3)","ab & ac", "(1,4)","xyz"]
>>>removeNonAlpha(i,j)
['abc', 'a', 'b', 'c', 'xyz']
此代码还假设您在 i 中有足够的元素来完全替换 j。
如果由于某些特殊原因您需要允许&符号(这意味着您还需要允许空格),这里是替代方法:
def removeNonAlpha(i,j):
indexI = 0
indexJ = 0
while indexJ < len(j):
if not j[indexJ].replace('&', '').replace(' ', '').isalnum():
j[indexJ] = i[indexI]
indexI += 1
indexJ += 1
return j
>>>i=["a","b"]
>>>j=["abc","(3)","ab & ac", "(1,4)","xyz"]
>>>removeNonAlpha(i,j)
['abc', 'a', 'ab & ac', 'b', 'xyz']