分子和分母列表的组合

2024-04-29 00:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我设法在R中实现的东西。出于某些原因,现在它需要在python中实现。翻译不是直截了当的。你知道吗

如果我有两个列表,一个是分子,一个是分母,那么我们如何挑选两个比率的所有集合,使得每个集合中两个比率的分子不相同。同样,我不希望分母是一样的。你知道吗

upList = ("up1","up2","up3")
downList = ("down1","down2","down3")

我想制作:

up1down1, up2down2
up1down1, up2down3
up1down1, up3down2
up1down1, up3down3

up1down2, up2down1
up1down2, up2down3
up1down2, up3down1
up1down2, up3down3

等等。。。你知道吗

我最初的尝试itertools.产品你知道吗

upSets = itertools.combinations(upList,2)
downSets = itertools.combinations(downList,2)

allSets = itertools.product(upSets,downSets)

for aset in allSets:
    print (aset)
    print (list(itertools.product(geneSet[0],geneSet[1])))

这就得到了一组4个分子/分母对,我不知道如何组合这4对,这样分子就不一样了。上面的代码生成许多行,如:

[('up1', 'down1'), ('up1', 'down2'), ('up2', 'down1'), ('up2', 'down2')]

在这条线之外,我想生产

[('up1', 'down1'), ('up2', 'down2')]
[('up1', 'down2'), ('up2', 'down1')]

Tags: 分子比率itertools分母up2up1down1up3down3upsets
3条回答

您可以这样使用来自itertoolsproduct方法,以便获得所需的输出:

from itertools import product

a = ("up1","up2","up3")
# assuming your b variable is like this one
b = ("down1", "down2","down3")

c = ["".join(k) for k in list(product(a,b))]

subfinal = list(product(c,c))
# removing the duplicates
# maybe not the best way to do it...
# also removing those kind of data: up1down1,up1down2
# also removing those kind of data: up1down1,up3down1
final = [k for k in subfinal if k[0] != k[1] and k[0][:3] != k[1][:3] and k[0][3:] != k[1][3:]]
print('total: ', len(final))
for k in final:
    print(", ".join(k))

输出:

total:  36
up1down1, up2down2
up1down1, up2down3
up1down1, up3down2
up1down1, up3down3
up1down2, up2down1
up1down2, up2down3
up1down2, up3down1
up1down2, up3down3
...

编辑:更正,感谢@Blckknght的评论。

根据您的输入数据:

upList = ("up1","up2","up3")
downList = ("down1","down2","down3")

首先要创建分子x分母的所有排列。可从^{}获得。你知道吗

from itertools import product

ratios = product(upList, downList)

接下来,您需要从产品中找到两个不同项的所有组合。这是一个2-组合,可以通过^{}得到。你知道吗

from itertools import combinations

ratio_pairs = combinations(ratios, 2)

但是你想把组合限制在两个项目不共享同一分子,也不共享同一分母的情况下。这是一个过滤列表:

distinct_ratio_pairs = [ (p1,p2) for p1,p2 in ratio_pairs if p1[0] != p2[0] and p1[1] != p2[1] ]

for drp in distinct_ratio_pairs:
    print(drp)

输出:

(('up1', 'down1'), ('up2', 'down2'))
(('up1', 'down1'), ('up2', 'down3'))
(('up1', 'down1'), ('up3', 'down2'))
(('up1', 'down1'), ('up3', 'down3'))
(('up1', 'down2'), ('up2', 'down1'))
(('up1', 'down2'), ('up2', 'down3'))
(('up1', 'down2'), ('up3', 'down1'))
(('up1', 'down2'), ('up3', 'down3'))
(('up1', 'down3'), ('up2', 'down1'))
(('up1', 'down3'), ('up2', 'down2'))
(('up1', 'down3'), ('up3', 'down1'))
(('up1', 'down3'), ('up3', 'down2'))
(('up2', 'down1'), ('up3', 'down2'))
(('up2', 'down1'), ('up3', 'down3'))
(('up2', 'down2'), ('up3', 'down1'))
(('up2', 'down2'), ('up3', 'down3'))
(('up2', 'down3'), ('up3', 'down1'))
(('up2', 'down3'), ('up3', 'down2'))

我觉得你很接近。在创建要打印的最终列表之前,您的代码工作正常。我想您应该为从allSets生成的每个项目创建两个列表:

upSets = itertools.combinations(upList,2)
downSets = itertools.combinations(downList,2)

allSets = itertools.product(upSets,downSets)

for (a, b), (c, d) in allSets:
    print([(a, c), (b, d)])
    print([(a, d), (b, c)])

这并不完全符合您想要的顺序,但它应该产生所有所需的成对组合。如果您需要按Leicographic顺序显示结果,请将结果放入一个列表并sort它。你知道吗

假设您不关心列表中成对的顺序。如果您这样做了,您将需要另外两个结果,上面的对被交换([(b, d), (a, c)][(b, c), (a, d)])。你知道吗

相关问题 更多 >