如何使用已定义功能的地图

时间:2019-05-04 10:25:04

标签: python python-3.x

我们要使用使用已定义函数(map)的mutation_detector函数,该函数有两个参数:DNA(字符串)和mutated_DNA(列表字符串)。 由于某些原因,它不起作用。

DNA = "ATGCTGATGCTCGCCTCGATGATAGCTCGCTCGATAGACTCGCTAAAGCTCGTAGCTGATCGCCTAGATGCCTAGATCCGTAGC"

dna_letters=("CATG")
def random_mutation(dna):
    """this function takes a dna seq and chooses a random location and random letter and changes it"""
    if len(dna)>1 and set(dna).issubset(dna_letters):
        mutation=random.choice(dna_letters)  
        location=random.randint(0,(len(dna)))
        mut=list(dna)
        mut[location]=mutation
        dna_with_random_mutation="".join(mut)
        return dna_with_random_mutation 
    else:
        return ("Wrong input")

mutated_DNA=list(map(random_mutation,[DNA]*100)) 


def mutation_detector(dna1,dna2):
    """this function recive 2 dna sequencesin the same length and returns the first index that have a diffence between them"""
    if type(dna1)==str and type(dna2)==str and len(dna1)==len(dna2) and set(dna1).issubset(dna_letters) and set(dna2).issubset(dna_letters):
        list_dna1=list(dna1)
        list_dna2=list(dna2)
        unique=list(zip(list_dna1,list_dna2))
        for i in unique:
            if i[0]!=i[1]:
                return unique.index(i)
            elif list_dna1==list_dna2:
                return (-1)
    else:
        return "Wrong input"

mutation_indices=(list(map(mutation_detector,DNA,[mutated_DNA])))        

2 个答案:

答案 0 :(得分:1)

首先,列表索引从0len(lst)-1,所以要避免超出范围的错误,请更改

location=random.randint(0,(len(dna)))

location=random.randint(0,len(dna)-1)

第二,这是将map与一个或多个参数一起使用的方法:

mutation_indices = list(map(lambda x: mutation_detector(x,DNA), mutated_DNA))

这会将mutation_detector函数应用于mutated_DNA中的每个元素,其中DNAmutation_detector的第二个参数。

这些更改之后,print(mutation_indices)输出:

[-1, -1, 52, -1, 24, 64, 3, 52, 56, 33, 72, 16, 52, 66, -1, 68, 67, -1, 51, 29, 31, 64, -1, 28, 74, 49, 23, 28, 35, 21, 40, -1, 54, -1, -1, 11, 65, 15, 77, 56, 26,62, -1, -1, 53, 21, 56, 40, 67, 36, 49, 13, 67, 69, 24, 41, -1, 41, 32, 44, -1, 72, 49, 5, 56, 2, 40, 2, 34, 46, 47, 16, 30, 11, -1, 78, -1, 32, 50, 13, 37, -1, 60,30, 7, 12, 81, 71, 64, 62, 16, 7, 45, 35, 79, 29, 43, 6, 51, 37]

答案 1 :(得分:0)

您最初使用map的试用完全无效。如果要使用map,则需要将其与lambda一起使用,以将常量DNA参数传递给mutation_detector函数。

您应该使用列表推导,而不是使用列表推导,它将使代码更清晰,更快速:

所以替换掉它:

mutation_indices=(list(map(mutation_detector,DNA,[mutated_DNA]))) 

与此:

mutation_indices = [mutation_detector(DNA, mutated) for mutated in mutated_DNA]


您还需要在random_mutation函数中更改此行,以避免出现IndexError

location=random.randint(0,(len(dna)))

收件人:

location = random.randint(0, len(dna) - 1)


这是应用了以下修复程序的代码的清理版本:

import random


def random_mutation(dna):
    """this function takes a dna seq and chooses a random location and random letter and changes it"""
    if len(dna) > 1 and set(dna).issubset(dna_letters):
        mutation = random.choice(dna_letters)
        location = random.randint(0, len(dna) - 1)
        mut = list(dna)
        mut[location] = mutation
        dna_with_random_mutation = "".join(mut)
        return dna_with_random_mutation
    else:
        return ("Wrong input")


def mutation_detector(dna1, dna2):
    """this function recive 2 dna sequencesin the same length and returns the first index that have a diffence between them"""
    if type(dna1) == str and type(dna2) == str and len(dna1) == len(
            dna2) and set(dna1).issubset(dna_letters) and set(dna2).issubset(
                dna_letters):
        list_dna1 = list(dna1)
        list_dna2 = list(dna2)
        unique = list(zip(list_dna1, list_dna2))
        for i in unique:
            if i[0] != i[1]:
                return unique.index(i)
            elif list_dna1 == list_dna2:
                return (-1)
    else:
        return "Wrong input"


DNA = "ATGCTGATGCTCGCCTCGATGATAGCTCGCTCGATAGACTCGCTAAAGCTCGTAGCTGATCGCCTAGATGCCTAGATCCGTAGC"
dna_letters = ("CATG")

mutated_DNA = list(map(random_mutation, [DNA] * 100))
mutation_indices = [mutation_detector(DNA, mutated) for mutated in mutated_DNA]

print(mutation_indices)
  

示例输出:

[54, 78, 22, 47, 42, 65, 56, 4, 53, 79, -1, 2, 5, 17, -1, 69, -1, 43, -1, 66, 71, -1, 23, 79, 79, -1, 73, 64, -1, 8, 52, 58, -1, 8, 78, -1, 11, 12, 32, 4, 61, -1, -1, 12, -1, 80, -1, 35, 47, 17, 45, 1, -1, 43, 0, 41, 32, 66, 63, -1, 26, 43, 76, 65, 33, 19, 46, -1, -1, 10, 56, 45, 76, 66, 43, 19, -1, 14, -1, 77, 30, 79, 45, 81, 49, 42, -1, 27, 7, -1, 68, 81, -1, 44, 13,37, -1, 59, -1, 38]