创建一个函数来删除或替换每个子列表包含 2 个值的列表中的最后一个数字
我有一个很长的列表,其中包含很多子列表,其中存在 2 个“值”, 例如
test=[["AAAGG1","AAAAA22"],["GGGGA1","AAGGA"],["GGGGG23","GGAGA6"]]
我想要的是替换或删除最后一位数字。 因此,我尝试使用一个相当长的函数:
def remove_numbers(index,newlist):
for com in index:
for dup in com:
if "1" in dup:
newlist.append(dup.replace("1",""))
elif "2" in dup:
newlist.append(dup.replace("2",""))
elif "3" in dup:
newlist.append(dup.replace("3",""))
elif "4" in dup:
newlist.append(dup.replace("4",""))
elif "5" in dup:
newlist.append(dup.replace("5",""))
elif "6" in dup:
newlist.append(dup.replace("6",""))
elif "7" in dup:
newlist.append(dup.replace("7",""))
elif "8" in dup:
newlist.append(dup.replace("8",""))
elif "9" in dup:
newlist.append(dup.replace("9",""))
else:
newlist.append(dup)
调用了该函数,
emptytest=[]
testfunction=remove_numbers(test,emptytest)
我创建了一个空列表,并在调用空测试时
['AAAGG', 'AAAAA', 'GGGGA', 'AAGGA', 'GGGGG3', 'GGAGA']
我的输出如下问题是它现在是一个列表,并且最后有两个数字不一样,它们不会全部被删除/替换。我需要子列表保持完整。
有人知道这个问题的解决方案吗?
抱歉,如果这是一个简单的问题,因为我对 python 还没有那么丰富的经验,但我无法在网络或现有论坛上找到合适的解决方案。
I have a long list containing a lot of sublists which exist of 2 "values",
for instance
test=[["AAAGG1","AAAAA22"],["GGGGA1","AAGGA"],["GGGGG23","GGAGA6"]]
What i want, is to replace or remove the last digits.
Therfore i have tried using a pretty long function:
def remove_numbers(index,newlist):
for com in index:
for dup in com:
if "1" in dup:
newlist.append(dup.replace("1",""))
elif "2" in dup:
newlist.append(dup.replace("2",""))
elif "3" in dup:
newlist.append(dup.replace("3",""))
elif "4" in dup:
newlist.append(dup.replace("4",""))
elif "5" in dup:
newlist.append(dup.replace("5",""))
elif "6" in dup:
newlist.append(dup.replace("6",""))
elif "7" in dup:
newlist.append(dup.replace("7",""))
elif "8" in dup:
newlist.append(dup.replace("8",""))
elif "9" in dup:
newlist.append(dup.replace("9",""))
else:
newlist.append(dup)
i created an empty list and called out the function
emptytest=[]
testfunction=remove_numbers(test,emptytest)
when i call out the emptytest my output is the following
['AAAGG', 'AAAAA', 'GGGGA', 'AAGGA', 'GGGGG3', 'GGAGA']
The problem is that it is now a single list and when there are two numbers in the end that are not the same, they are not all removed/replaced. I need the sublists to remain intact.
does anybody know of a solution for this?
Sorry if it is a simple question since i am not that experienced with python yet, but i couldn't find a suitable solution on the web or an existing forum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是使用正则表达式来替换数字,而不是手动识别所有内容。整个事情可以通过下面两行来实现。
给出结果
这里我们使用了一个正则表达式
"\d+$"
它基本上匹配字符串末尾的数字模式。如果识别出这样的模式,那么我们将其替换为空。What you need is to use a regex for replacing the numbers and not manually identifying everything. The whole thing can be achieved by 2 lines below.
Gives a result
Here we used a regex
"\d+$"
which basically matches a numerical pattern at end of the string. If such a pattern is identified, then we replace it with empty.