当前位置: 代码迷 >> python >> 列表分子和分母的组合
  详细解决方案

列表分子和分母的组合

热度:94   发布时间:2023-06-13 16:58:38.0

这是我设法在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.product

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')]

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

给定您的输入数据:

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

您要首先创建分子x分母的所有排列。 可从 。

from itertools import product

ratios = product(upList, downList)

接下来,您要查找产品中2个不同项目的所有组合。 这是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)])

这不会完全符合您想要的顺序,但是它会产生所有所需的对组合。 如果您需要按地志顺序排列结果,请将结果放在列表中并进行sort

假定您不关心列表中的对的顺序。 如果这样做,您将需要另外两个结果,并且上面的对将交换为[(b, d), (a, c)][(b, c), (a, d)] )。

您可以像这样通过itertools使用product方法,以得到所需的输出:

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
...
  相关解决方案