如何找到重复的字母并将所有这些字母从列表中的字符串中删除
给定的名称列表:
name_list = ['Jasonn', 'pPeter', 'LiSsa', 'Joanna']
我想删除相同的字母(案例不敏感),例如name_list [0]
,它将是'jaso'和name_list [3 ],它将是
'jo'
,因为'n'
s被删除,'a'
也应删除。
这是我的代码:
i = 0
for name in name_list:
ind = name_list.index(name)
length = len(name)
for i in range(0,length-1):
if name[i].lower() == name[i+1].lower():
name = name_list[ind].replace(name[i], '', 1)
name = name.replace(name[i], '', 1)
length -= 2
if i >= 1 and name[i].lower() == name[i-1].lower():
name = name_list[ind].replace(name[i], '', 1)
name = name.replace(name[i-1], '', 1)
else:
i += 1
if ind != len(name_list):
print(sep,end='', sep='') #sep is my separator
print()
我的代码不编译。它在这一行上失败了:
if i >= 1 and name[i].lower() == name[i-1].lower():
与:
IndexError: string index out of range
我无法弄清楚为什么范围是错误的。我的第一个想法是检查索引是否大于0,以便i-1
不会是负面的。例如,给定字符串'ppeter'
,在我删除'pp'
之后,我只需检查新的字母'e'e'
for <代码> i = 0 和't'
对于i+1
,因为在索引0之前没有字母。
对于'j [0] o [1] a [2] n [3] n [4] a [5]'
- 当
i = 3
,'n'
s fori
和i+1
被删除。然后将字符串变为'j [0] o [1] a [2] a [3]'。 - 因为
i = 3&gt; 0
和i-1
和i
等于'a'
,我们删除'a'
s并生成'jo'
。
有人可以帮我弄清楚我出错的地方吗?
Given a list of names:
name_list = ['Jasonn', 'pPeter', 'LiSsa', 'Joanna']
I want to remove the same letters(case insensitive), say for name_list[0]
, it will be 'Jaso'
and for name_list[3], it will be 'Jo'
since after 'n'
s are removed, 'a'
s should also be removed.
Here's my code:
i = 0
for name in name_list:
ind = name_list.index(name)
length = len(name)
for i in range(0,length-1):
if name[i].lower() == name[i+1].lower():
name = name_list[ind].replace(name[i], '', 1)
name = name.replace(name[i], '', 1)
length -= 2
if i >= 1 and name[i].lower() == name[i-1].lower():
name = name_list[ind].replace(name[i], '', 1)
name = name.replace(name[i-1], '', 1)
else:
i += 1
if ind != len(name_list):
print(sep,end='', sep='') #sep is my separator
print()
My code does not compile. It fails on this line:
if i >= 1 and name[i].lower() == name[i-1].lower():
with:
IndexError: string index out of range
I can't figure out why the range is wrong. My first thought was to check if the index is bigger than 0 so that i-1
would not be negative. For example, given the string 'pPeter'
, after I removed 'pP'
, I then just check the new letter 'e'
for i = 0
and 't'
for i+1
since there's no letter before index 0.
and for 'J[0]o[1]a[2]n[3]n[4]a[5]'
- When
i = 3
, the'n'
s fori
andi+1
are removed. The string then becomes 'J[0]o[1]a[2]a[3]'. - Since
i = 3 > 0
and bothi-1
andi
equals'a'
, we remove the'a'
s and generate'Jo'
.
Could someone help me figure out where I went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种方法看起来不必要地复杂。
相反,您可以跟踪列表中每个字母的频率。然后,仅保留完全出现一次的字母:
这输出:
This approach looks unnecessarily complex.
Instead, you can keep track of the frequencies of every letter in the list. Then, retain only the letters that appear exactly once:
This outputs:
带有正则表达式:
With regular expressions: