人类有22条染色体和2条性染色体X和Y.
我想从'chr1','chr2','chr3'......'chr22','chrX'和'chrY'生成这些染色体名称,只输入染色体数目和X和Y.
# Input the data here
number_of_chr = 22,'X','Y'
for x in range(number_of_chr[0]):
print('chr'+str(x+1))
从代码我可以生成'chr1'到'chr22',但是如何生成'chrX'和chrY'并将它们附加到列表中,以便最终结果如下:
chrID = ['chr1','chr2','chr3',......'chr19','chr20','chr21','chr22','chrX','chrY']
答案 0 :(得分:6)
chrID = [ 'chr{}'.format(x) for x in list(range(1,23)) + ['X', 'Y'] ]
答案 1 :(得分:2)
使用append
附加到列表
r = list(range(1,23))
r.append('X')
r.append('Y')
l = []
for x in r:
l.append('chr'+str(x))
range
返回范围内的数字列表(在python2.7中)。只需将x和y附加到该列表中。
我猜您使用的是python 3,因此您必须将范围转换为如上所示的列表。
答案 2 :(得分:1)
>>> from itertools import chain
>>> number_of_chr = 22,'X','Y'
>>> n, *rest = number_of_chr
>>> ['chr%s'%i for i in chain(range(1, n+1), rest)]
['chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'chr7', 'chr8', 'chr9', 'chr10', 'chr11', 'chr12', 'chr13', 'chr14', 'chr15', 'chr16', 'chr17', 'chr18', 'chr19', 'chr20', 'chr21', 'chr22', 'chrX', 'chrY']