在python中,有没有一种更快的方法来应用函数而不是在循环中使用循环

在python中,有没有一种更快的方法来应用函数而不是在循环中使用循环,python,loops,Python,Loops,我正在迭代一个文件并创建一组唯一的“in”字符串。接下来,我迭代一对文件,并提取每个fastq读取的序列字符串。接下来,我将遍历“in”集合,查看字符串的Levenshtein距离是否为。您可以使用dictionary而不是list,其中键是条形码 如果您不喜欢使用循环中的循环,可以使用列表理解和测试性能 inlist = open("umi_tools_inlist_2000.txt", "r") barcodes = dict() for line

我正在迭代一个文件并创建一组唯一的“in”字符串。接下来,我迭代一对文件,并提取每个fastq读取的序列字符串。接下来,我将遍历“in”集合,查看字符串的Levenshtein距离是否为。您可以使用dictionary而不是list,其中键是条形码

如果您不喜欢使用循环中的循环,可以使用列表理解和测试性能

inlist = open("umi_tools_inlist_2000.txt", "r")
barcodes = dict()

for line in inlist:
    barcodes[line.split("\t")[0]] = 0

# Next I iterate through two fastq files and extract the sequence of each read
with pysam.FastxFile("errors_fullbarcode_read_R1.fastq") as fh, pysam.FastxFile("errors_fullbarcode_read_R2.fastq") as fh2:

for record_fh, record_fh2  in zip(fh, fh2):
    barcode = record_fh.sequence[0:24]

    b_found = [b for b in barcodes.keys() if Levenshtein.distance(barcode, b) <= 2]
    # as per your logic b_found will have only zero or one ele
    if b_found:  
        new_b = barcodes[b_found[0]] + record_fh.sequence[24:]
        barcodes[new_b] = 0
        del barcodes[b_found[0]]
inlist=open(“umi\u tools\u inlist\u 2000.txt”、“r”)
条形码=dict()
对于inlist中的行:
条形码[行分割(“\t”)[0]]=0
#接下来,我遍历两个fastq文件并提取每次读取的序列
将pysam.FastxFile(“errors\u fullbarcode\u read\u R1.fastq”)作为fh,将pysam.FastxFile(“errors\u fullbarcode\u read\u R2.fastq”)作为fh2:
对于zip(fh,fh2)中的记录(fh,fh2):
条形码=记录顺序[0:24]

b_found=[b代表条形码中的b.keys(),如果Levenshtein.distance(条形码,b)如果你想提高ALGO的质量,这可能是相关的,或者如果你把LevsTein改为更简单的度量,你可能会直接使用KDDTURE来删除一个循环。或者你可以切换到一个更高效的语言C++ C++,谢谢。是否有许多使用相同的两个代码调用Levenshtein?如果是这样,您可能会从使用例如
lru.cache
缓存这些结果中获益,或者通过在字典中存储本地数据来避免重新计算距离。
inlist = open("umi_tools_inlist_2000.txt", "r")
barcodes = dict()

for line in inlist:
    barcodes[line.split("\t")[0]] = 0

# Next I iterate through two fastq files and extract the sequence of each read
with pysam.FastxFile("errors_fullbarcode_read_R1.fastq") as fh, pysam.FastxFile("errors_fullbarcode_read_R2.fastq") as fh2:

for record_fh, record_fh2  in zip(fh, fh2):
    barcode = record_fh.sequence[0:24]

    b_found = [b for b in barcodes.keys() if Levenshtein.distance(barcode, b) <= 2]
    # as per your logic b_found will have only zero or one ele
    if b_found:  
        new_b = barcodes[b_found[0]] + record_fh.sequence[24:]
        barcodes[new_b] = 0
        del barcodes[b_found[0]]