标签: cartesian-product

两个字典python的笛卡尔积

好的,所以我有两本词典.

dictionary_1 = {'status': ['online', 'Away', 'Offline'],
                'Absent':['yes', 'no', 'half day']}
dictionary_2 = {'healthy': ['yes', 'no'],
                'insane': ['yes', 'no']
Run Code Online (Sandbox Code Playgroud)

现在我需要将它们组合起来,以便我得到一个新的字典:

{'status': ['online', 'online', 'away', 'away', 'Offline', 'Offline'],
 'Absent': ['yes', 'yes', 'no', 'no', 'half day', 'half day'],
 'healthy': ['yes', 'no', 'yes', 'no', 'yes', 'no'],
 'insane': ['yes', 'no', 'yes', 'no', 'yes', 'no']
}
Run Code Online (Sandbox Code Playgroud)

这是一个很晚的更新,但如果有人感兴趣,我找到了一种没有itertools的方法.

def cartesian_product(dict1, dict2):
    cartesian_dict = {}
    dict1_length = len(list(dict1.values())[0])
    dict2_length = len(list(dict2.values())[0])
    h = []
    for key in dict1:
        for value in dict1[key]:
            if not key in cartesian_dict: …
Run Code Online (Sandbox Code Playgroud)

python dictionary cartesian-product python-itertools python-3.x

0
推荐指数
1
解决办法
3164
查看次数

Java Guava CartesianProduct

我正在通过编写一小段代码来探索Java Guava库.这是我为寻找n-sets 的笛卡儿积而写的.文档在这里

//allLists populated above
...
List<Set> interimList = new ArrayList<Set>();
for(List<String> tmp : allLists) //converting List<List> to List<Set> 
{
    Set interimSet   = new HashSet(tmp);
    interimList.add(interimSet);
}
System.out.println(interimList);
Sets.cartesianProduct(interimList);
Run Code Online (Sandbox Code Playgroud)

但这不是编译.最后一行Sets.cartesianProduct不接受List<Set>.但根据文件不是它的签名?

public static <B> Set<List<B>> cartesianProduct(List<? extends Set<? extends B>> sets)

错误信息: Cannot Resolve method cartesianProduct(java.util.List<java.util.Set)

java generics cartesian-product guava

0
推荐指数
1
解决办法
2026
查看次数

连接两个不同列表中的字符串

我需要在 python 中连接两个不同的字符串列表。

例如:

list1 = ['A','B','C']
list2 = ['D', 'E']
Run Code Online (Sandbox Code Playgroud)

我想获得

list3 = ['AD', 'AE', 'BD', 'BE', 'CD', 'CE']
Run Code Online (Sandbox Code Playgroud)

我试过了:

list3 = zip(list1,list2)
Run Code Online (Sandbox Code Playgroud)

它返回

list3 = [('A','D'), ('B','E')]
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

list(itertools.product(list1, list2))

但它返回

[('A','D'),('A','E'),...,('C','E')] 而不是 ['AD', 'AE', 'BD', 'BE'、'CD'、'CE']

任何想法?谢谢

python string list concatenation cartesian-product

0
推荐指数
1
解决办法
3245
查看次数

Lots of variables, best approach without nested loops

I’m in need of some help and guidance on the design of my code. I want to run tests with multiple variables set to multiple values, without creating insane amounts of nested loops. I got a struct which holds various variables like this (only three integers as an example, but the real deal will hold a lot more, including booleans, doubles etc):

struct VarHolder
{
    int a;
    int b;
    int c;
    // etc..
    // etc..
};
Run Code Online (Sandbox Code Playgroud)

The struct get passed …

c++ cartesian-product nested-loops

0
推荐指数
1
解决办法
99
查看次数

两个列表的笛卡尔积可以返回除涉及两个相同元素的组合之外的所有组合吗?

例如

nested = [a,b,c]

上面列表的笛卡尔乘积本身将生成以下对

[(a,a),(a,b),(a,c),(b,a),(b,c),(b,b),(c,a),(c,b),(c,c)]

我想找到一种方法来产生以下内容

[(a,b),(a,c),(b,a),(b,c),(c,a),(c,b)]

我尝试了以下

[(x,y) for x in nested for y in nested if x != y]

上面的代码在以下测试用例中失败

nested_testcase1 = [[1,2],[2,3],[1,2]]
Run Code Online (Sandbox Code Playgroud)

现在,上面的代码行将给出以下结果

[([1,2],[2,3]),([2,3],[1,2]),([1,2],[2,3]),([2,3],[1,2])]

但是我下面是什么

[([1,2],[2,3]),([2,3],[1,2]),([1,2],[2,3]),([2,3],[1,2]),([1,2],[1,2]),([1,2],[1,2])]

最后两个组合是通过将第一个和最后一个元素配对来实现的。

有人有什么想法吗?

python cartesian-product python-3.x

0
推荐指数
1
解决办法
62
查看次数

jq:如何更改 JSON 的结构以避免笛卡尔积?

我有很薄的 JSON 输入:

[
  {
    "x": [
      "2020-02-24T00:00:00",
      "2020-02-25T00:00:00",
      "2020-02-26T00:00:00"
    ],
    "y": [
      3,
      2,
      6
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我想获得:

[
  {
    "a": "2020-02-24T00:00:00",
    "b": 3
  },
  {
    "a": "2020-02-25T00:00:00",
    "b": 2
  },
  {
    "a": "2020-02-26T00:00:00",
    "b": 6
  }
]
Run Code Online (Sandbox Code Playgroud)

如果我申请,.[]|{a:.x[],b:.y[]}我将获得笛卡尔积(9 项)。

如何更改此 JSON 的结构以避免笛卡尔积?

json cartesian-product jq

0
推荐指数
1
解决办法
40
查看次数

为什么类型的星号超载?

我仍然不明白动机.

为什么他们分别为整数和浮点数乘法制作了两个不同的运算符(**.),就好像它们害怕超载一样,但同时它们曾经*用来表示类型的笛卡尔积?

type a = int * int ;;
Run Code Online (Sandbox Code Playgroud)

为什么他们突然变得如此勇敢?为什么不写

type a = int *.. int ;;
Run Code Online (Sandbox Code Playgroud)

或者其他的东西?

是否存在一些关系,这使得笛卡尔积更接近整数乘积并且更远离浮动乘积?

ocaml operator-overloading cartesian-product

-1
推荐指数
1
解决办法
411
查看次数

当其中一个是无限的时如何获得两个迭代的笛卡尔积

假设我有两个可迭代对象,一个是有限的,一个是无限的:

import itertools

teams = ['A', 'B', 'C']
steps = itertools.count(0, 100)
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以避免嵌套的 for 循环并使用itertools模块中的无限迭代器之一,例如cyclerepeat来获取这些迭代器的笛卡尔积。

循环应该是无限的,因为 的停止值steps预先未知。

预期输出:

$ python3 test.py  
A 0
B 0
C 0
A 100
B 100
C 100
A 200
B 200
C 200
etc...
Run Code Online (Sandbox Code Playgroud)

带有嵌套循环的工作代码:

from itertools import count, cycle, repeat

STEP = 100 
LIMIT = 500
TEAMS = ['A', 'B', 'C']


def test01():
    for step in count(0, STEP):
        for team in TEAMS:
            print(team, step)
        if …
Run Code Online (Sandbox Code Playgroud)

python cartesian-product python-itertools

-1
推荐指数
1
解决办法
284
查看次数