这是我设法在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')]
答案 0 :(得分:1)
修改:已更正,感谢@Blckknght的评论。
根据您的输入数据:
upList = ("up1","up2","up3")
downList = ("down1","down2","down3")
您想首先创建分子x分母的所有排列。这可以从itertools.product
获得。
from itertools import product
ratios = product(upList, downList)
接下来,您需要查找产品中2个不同商品的所有组合。这是一个2组合,可以通过itertools.combinations
获得。
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'))
答案 1 :(得分:1)
我认为你非常接近。在您创建要打印的最终列表之前,您的代码工作正常。我想你想为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)]
)中再获得两个结果。
答案 2 :(得分:0)
您可以像product
这样使用itertools
方法,以获得所需的输出:
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
...