创建一个函数来删除或替换每个子列表包含 2 个值的列表中的最后一个数字

发布于 2025-01-11 05:06:42 字数 1336 浏览 0 评论 0原文

我有一个很长的列表,其中包含很多子列表,其中存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2025-01-18 05:06:42

您需要的是使用正则表达式来替换数字,而不是手动识别所有内容。整个事情可以通过下面两行来实现。

import re
processed = [[re.sub(r"\d+$","",n) for n in t] for t in test]
print(processed)

给出结果

[['AAAGG', 'AAAAA'], ['GGGGA', 'AAGGA'], ['GGGGG', 'GGAGA']]

这里我们使用了一个正则表达式 "\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.

import re
processed = [[re.sub(r"\d+
quot;,"",n) for n in t] for t in test]
print(processed)

Gives a result

[['AAAGG', 'AAAAA'], ['GGGGA', 'AAGGA'], ['GGGGG', 'GGAGA']]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文