我正在尝试生成一个随机条码列表,其中包含6个汉明距离为3的条形码。问题是该程序生成的条形码列表中有重复项而不是正确的汉明距离。以下是代码。
import random
nucl_list = ['A', 'C', 'G', 'T']
length = 6
number = 6
attempts = 1000
barcode_list = []
tested = []
def make_barcode():
"""Generates a random barcode from nucl_list"""
barcode = ''
for i in range(length):
barcode += random.choice(nucl_list)
return barcode
def distance(s1, s2):
"""Calculates the hamming distance between s1 and s2"""
length1 = len(s1)
length2 = len(s2)
# Initiate 2-D array
distances = [[0 for i in range(length2 + 1)] for j in range(length1 + 1)]
# Add in null values for the x rows and y columns
for i in range(0, length1 + 1):
distances[i][0] = i
for j in range(0, length2 + 1):
distances[0][j] = j
for i in range(1, length1 + 1):
for j in range(1,length2 + 1):
cost = 0
if s1[i - 1] != s2[j - 1]:
cost = 1
distances[i][j] = min(distances[i - 1][j - 1] + cost, distances[i][j - 1] + 1, distances[i - 1][j] + 1)
min_distance = distances[length1][length2]
for i in range(0, length1 + 1):
min_distance = min(min_distance, distances[i][length2])
for j in range(0, length2 + 1):
min_distance = min(min_distance, distances[length1][j])
return min_distance
def compare_barcodes():
"""Generates a new barcode and compares with barcodes in barcode_list"""
new_barcode = make_barcode()
# keep track of # of barcodes tested
tested.append(new_barcode)
if new_barcode not in barcode_list:
for barcode in barcode_list:
dist = distance(barcode, new_barcode)
if dist >= 3:
barcode_list.append(new_barcode)
else:
pass
else:
pass
# make first barcode
first_barc = ''
for i in xrange(length):
first_barc += random.choice(nucl_list)
barcode_list.append(first_barc)
while len(tested) < attempts:
if len(barcode_list) < number:
compare_barcodes()
else:
break
barcode_list.sort()
print barcode_list
我认为我的问题在于最后一个while循环:我希望compare_barcodes
能够连续生成符合条件的条形码(不是重复的,而不是已经生成的任何条形码的汉明距离)。
答案 0 :(得分:1)
在compare_barcodes()
中尝试这样的行为。
基本上我们会跟dist >= 3
跟too_far
进行跟踪。完成barcode_list
循环后,我们返回并检查too_far
。如果不是too_far
,那么我们可以附加到列表中。
每次发现barcode_list
时,旧的逻辑都附加到dist >= 3
,这当然不止一次,具体取决于已经将多少条形码添加到列表中。
def compare_barcodes():
too_far = False
"""Generates a new barcode and compares with barcodes in barcode_list"""
new_barcode = make_barcode()
# keep track of # of barcodes tested
tested.append(new_barcode)
if new_barcode not in barcode_list:
for barcode in barcode_list:
dist = distance(barcode, new_barcode)
if dist >= 3:
too_far = True
if not too_far:
barcode_list.append(new_barcode)
编辑:我刚刚意识到您希望汉明距离为3或更大......在这种情况下,只需将if not too far
更改为if too far
。
答案 1 :(得分:1)
@Jkdc的答案是对的,给他+1。在您的原始代码中,您几乎就在那里。以下是我的建议,将if new_barcode not in barcode_list:
条件移到for loop
内,将其设为if new_barcode not in barcode_list and distance(barcode, new_barcode)
,然后您不会在列表中添加任何重复条件,然后仅在{{1}时计算距离不在你的new_barcode
:
barcode_list
另一个建议是,如果您想避免重复,可以使用def compare_barcodes():
"""Generates a new barcode and compares with barcodes in barcode_list"""
new_barcode = make_barcode()
# keep track of # of barcodes tested
tested.append(new_barcode)
for barcode in barcode_list:
if new_barcode not in barcode_list and distance(barcode, new_barcode):
barcode_list.append(new_barcode)
存储条形码,set
操纵未分类的唯一元素。
答案 2 :(得分:0)
问题在于你的compare_barcodes()函数。在旧版本中,一旦看到条形码(距离任何比较的字符串3步),它就会将新字符串添加到列表中。代码可以修改为以下内容。
def compare_barcodes():
"""Generates a new barcode and compares with barcodes in barcode_list"""
minDist = length
new_barcode = make_barcode()
# keep track of # of barcodes tested
tested.append(new_barcode)
if new_barcode not in barcode_list:
for barcode in barcode_list:
dist = distance(barcode, new_barcode)
#if dist >= 3:
# barcode_list.append(new_barcode)
#else:
# pass
if dist < minDist:
minDist = dist
else:
pass
if minDist >= 3:
barcode_list.append(new_barcode)
答案 3 :(得分:0)
我最终制作了一个计算汉明距离的新函数......
def compare_distances(new_barcode):
"""Compares the hamming_dist between new barcode and old barcodes"""
# Count number of distances < 3
count = 0
global barcode_list
for barcode in barcode_list:
if distance(new_barcode, barcode) < 3:``
count +=1
return count
def compare_barcodes():
new_barcode = make_barcode()
if new_barcode not in barcode_list:
count = compare_distances(new_barcode)
if count > 0:
pass
else:
barcode_list.append(new_barcode)
else:
pass
# Initiate the functions to generate barcodes
while len(barcode_list) < number:
compare_barcodes()