如何从一组列表中获取笛卡尔积(每种可能的值组合)?
输入:
somelists = [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
Run Code Online (Sandbox Code Playgroud)
期望的输出:
[(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5) ...]
Run Code Online (Sandbox Code Playgroud) 我有一个这种类型的列表List>包含这个
List<int> A = new List<int> {1, 2, 3, 4, 5};
List<int> B = new List<int> {0, 1};
List<int> C = new List<int> {6};
List<int> X = new List<int> {....,....};
Run Code Online (Sandbox Code Playgroud)
我希望拥有这样的所有组合
1-0-6
1-1-6
2-0-6
2-1-6
3-0-6
Run Code Online (Sandbox Code Playgroud)
等等.
据你说这是使用Linq解决这个问题吗?
我有一个列表列表。我想找到所有保持每个子列表顺序的平面列表。举个例子,假设我有一个这样的列表:
ll = [['D', 'O', 'G'], ['C', 'A', 'T'], ['F', 'I', 'S', 'H']]
Run Code Online (Sandbox Code Playgroud)
获得一个解决方案是微不足道的。我设法编写了以下代码,它可以生成一个随机的平面列表,该列表保持每个子列表的顺序。
import random
# Flatten the list of lists
flat = [x for l in ll for x in l]
# Shuffle to gain randomness
random.shuffle(flat)
for l in ll:
# Find the idxs in the flat list that belongs to the sublist
idxs = [i for i, x in enumerate(flat) if x in l]
# Change the order to match the order in the sublist
for …Run Code Online (Sandbox Code Playgroud) 注意:这不是重复的问题,因为标题可能会说
如果我有一个list列表,则需要从中获取所有组合并进行替换。
import itertools
l = [[1,2,3] ,[1,2,3], [1,2,3]]
n = []
for i in itertools.product(*l):
if sorted(i) not in n:
n.append(sorted(i))
for i in n:
print(i)
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 2]
[1, 2, 3]
[1, 3, 3]
[2, 2, 2]
[2, 2, 3]
[2, 3, 3]
[3, 3, 3]
Run Code Online (Sandbox Code Playgroud)
感谢@RoadRunner和@Idlehands。
上面的代码完美,有两个问题:
对于较大的列表,itertools.product引发MemoryError。当l具有18个3个长度的子列表时,给出的合并数约为4亿。
订单很重要,因此sorted无法解决我的问题。对于某些人可能会造成混淆,因此请在下面的示例中进行说明。
l = [[1,2,3], [1], [1,2,3]]
在这里,我有2个独特的群组:
组1:元素0、2具有相同的值[1,2,3]
第2组:元素1,其值为[1]
因此,我需要的解决方案是:
[1,1,1]
[1,1,2]
[1,1,3]
[2,1,2]
[2,1,3]
[3,1,3]
Run Code Online (Sandbox Code Playgroud)
因此位置1 …
我之前提过这个问题但是关于另一种编程语言.
假设我有几个词根,前缀和后缀.
roots = ["car insurance", "auto insurance"]
prefix = ["cheap", "budget"]
suffix = ["quote", "quotes"]
Run Code Online (Sandbox Code Playgroud)
Python中是否有一个简单的函数,它允许我构造三个字符向量的所有可能组合.
所以我想要一个列表或其他数据结构,它返回每个字符串的所有可能组合的以下列表.
cheap car insurance quotes
cheap car insurance quotes
budget auto insurance quotes
budget insurance quotes
...
Run Code Online (Sandbox Code Playgroud) 我希望能够生成有条件的产品.与此答案类似: 列表列表的所有组合
我想用itertools.product(*listOfLists).但是,我的问题是从一个列表中包含一个元素意味着必须为产品查阅其他列表.
例:
colors = ['red', 'blue', 'green']
fruits = ['apple', 'orange', 'banana']
locations = ['indoors', 'outdoors']
indoor_choices = ['bathroom', 'bedroom', 'kitchen']
green_choices = ['forest', 'light', 'dark']
Run Code Online (Sandbox Code Playgroud)
在这里,我们要始终考虑每种可能的颜色,特性和位置选择.然而,在"室内"的情况下,我们也想考虑室内选择,并且在"绿色"可能的选择的情况下,我们还想选择更具体的绿色.它是一种可能性的树,其中一些分支保持分支而另一些则不分支.
所以在上面的这个愚蠢的例子中你可以这样做一个for循环:
for c in colors:
for f in fruits:
for l in locations:
# etc
Run Code Online (Sandbox Code Playgroud)
但是,当我们遇到两个不同类别根据这个选择进行分支时会发生什么的问题.
一个简单的(hacky)解决方案就是手动编写条件并在其中放置循环:
for c in colors:
for f in fruits:
for l in locations:
if c == 'green' and l == 'indoor':
for gc in green_choices:
for ic in indoor_choices:
# output
elif c …Run Code Online (Sandbox Code Playgroud) 我有一个 python 列表:
mylist = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我想遍历这些值的所有可能组合而不依赖于位置,大小为 3,这意味着我希望将这些作为迭代:
iteration: 111
iteration: 112
iteration: 113
iteration: 114
iteration: 221 # note, no 211, since the combination of these values already occured as 112
iteration: 222
iteration: 223
iteration: 224
.
.
.
Run Code Online (Sandbox Code Playgroud)
我考虑过遍历列表的唯一值,但我仍然没有找到解决这个问题的简单解决方案,我至少认为这种情况经常发生。也许有一个很好的 numpy 方法。我怎样才能做到这一点?
谢谢!
我有两个元组:
t1 = ('A', 'B')
t2 = ('C', 'D', 'E')
Run Code Online (Sandbox Code Playgroud)
我想知道如何在元组之间创建组合,因此结果应该是:
AC, AD, AE, BC, BD, BE
Run Code Online (Sandbox Code Playgroud)
编辑
运用
list(itertools.combinations('abcd',2))
Run Code Online (Sandbox Code Playgroud)
我可以为给定的字符串生成组合列表:
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
Run Code Online (Sandbox Code Playgroud)
如果我插入元组而不是字符串,则会发生以下错误:
TypeError: sequence item 0: expected string, tuple found
Run Code Online (Sandbox Code Playgroud)
有什么建议怎么办?
python ×7
list ×6
combinations ×2
algorithm ×1
c# ×1
generics ×1
linq ×1
numpy ×1
permutation ×1
python-3.x ×1
tree ×1
tuples ×1