我写下了程序。它只是以寄生虫和宿主的二进制数字的形式随机列出“遗传密码”。我想要的只是帮助组织输出。我希望输出在列中 - 例如:
Host Parasite
1 [1,0,0,1]
2 [1,0,0,1]
3 ......
4 .......
主机旁边另一栏中的寄生虫随机数列表
import random
host_genome_length = 10
assert host_genome_length >0
parasite_genome_length = 5
assert parasite_genome_length>0
host_initial_population=10
assert host_initial_population >=1
parasite_initial_population=2
assert parasite_initial_population >=0
parasite_initial_chance_1= 0.1
assert 0<= parasite_initial_chance_1 <=1
host_initial_chance_1=0.5
assert 0<= host_initial_chance_1 <=1
hosts=[]
for i in range(host_initial_population):
genome= []
for j in range (host_genome_length):
if random.random()< host_initial_chance_1:
genome.append(1)
else:
genome.append(0)
hosts.append(genome)
print "hosts:"
print hosts
parasites=[]
for i in range(parasite_initial_population):
genome= []
for j in range (parasite_genome_length):
if random.random()< parasite_initial_chance_1:
genome.append(1)
else:
genome.append(0)
parasites.append(genome)
for i in range(host_initial_population-parasite_initial_population):
parasites.append(None)
print "parasites:"
print parasites
答案 0 :(得分:1)
您可以使用Python的字符串格式化工具。每个括号的:40
部分指定该字段应为40个字符宽:
print "{:40} {:40}".format("Host", "Parasite")
for host, parasite in zip(hosts, parasites):
print "{:40} {:40}".format(host, parasite)
输出:
Host Parasite
[0, 1, 1, 1, 0, 1, 1, 0, 0, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1] [0, 0, 0, 0, 0]
[1, 0, 1, 0, 1, 0, 1, 0, 1, 1] None
[0, 1, 0, 0, 0, 1, 1, 1, 0, 1] None
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1] None
[1, 1, 1, 1, 0, 1, 1, 0, 0, 1] None
[1, 1, 1, 0, 1, 0, 1, 1, 1, 0] None
[1, 0, 0, 1, 1, 1, 0, 0, 0, 1] None
[0, 0, 1, 1, 0, 1, 0, 0, 1, 1] None
[1, 1, 1, 0, 1, 0, 0, 1, 0, 1] None
答案 1 :(得分:0)
您可以查看str.format功能。
以下是您可能想出的一些草图。
print '{:50} {:50}'.format('Host', 'Parasites')
for i in range(max(len(parasites), len(hosts))):
print '{:50} {:50}'.format(str(i+1) + str(hosts[i]), parasites[i])
以下是代码中该代码段生成的输出:
Host Parasites
1[1, 0, 0, 1, 0, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0]
2[1, 0, 0, 1, 1, 1, 0, 0, 1, 0] [0, 0, 0, 0, 0]
3[0, 1, 1, 1, 1, 0, 1, 0, 0, 0] None
4[1, 0, 0, 0, 1, 0, 0, 0, 1, 1] None
5[0, 1, 0, 1, 0, 1, 0, 0, 1, 0] None
6[1, 1, 1, 1, 0, 1, 1, 0, 1, 1] None
7[0, 1, 1, 0, 0, 0, 0, 1, 1, 1] None
8[0, 0, 1, 0, 0, 1, 0, 0, 1, 1] None
9[0, 0, 1, 0, 1, 0, 0, 0, 1, 0] None
10[1, 0, 0, 1, 1, 1, 1, 1, 0, 1] None
你可能想要再美化它。