1

Have a dictionary and the values of the dictionary are arrays.

d = {"one": a1, "two": a2, "three": a3}
a1 = np.array([1,2,3])
a2 = np.array([2,5,6,7])
a3 = np.array([1,2,8,3,4])

Need to do some statistics test later( haven't decide which test to use yet, use ttest as one example) for each combinations of the arrays without worry about orders. For example do t-test for following combinations,like:

ttest(a1,a2)
ttest(a2,a3)
ttest(a1,a2,a3)

How could I quickly generate these combinations.

There is

from itertools import combinations
list(map(dict, itertools.combinations(d.items(), 2)))

which could create fixed length combinations like 2 of each combinations, how about 3 pairs.

Expected results

{'test1': test_result1,'test2': test_result2,'test3': test_result3}

Currently need to figure how to quickly generate those combinations.

newleaf
  • 2,257
  • 8
  • 32
  • 52

2 Answers2

1

You will simply need to concatenate combinations of the various length. Your example result illustrate combinations of values but your code attempt to product a dictionary so I'm not sure which one you're looking for.

from itertools import combinations

d = {"one": "a1", "two": "a2", "three": "a3"}

# combining values:

r = [ combo for size in range(2,len(d)+1) 
            for combo in combinations(d.values(),size) ]
print(r)
# [('a1', 'a2'), ('a1', 'a3'), ('a2', 'a3'), ('a1', 'a2', 'a3')]

# combining items into a new dictionary:

r = dict( tuple(zip(*combo)) for size in range(2,len(d)+1) 
                             for combo in combinations(d.items(),size) )
print(r)
# {('one', 'two'): ('a1', 'a2'), ('one', 'three'): ('a1', 'a3'),
#  ('two', 'three'): ('a2', 'a3'), 
#  ('one', 'two', 'three'): ('a1', 'a2', 'a3')}
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

You can try:

>>> {'one': 1, 'two': 2, 'three': 3}
>>> from itertools import chain, combinations
>>> list(chain.from_iterable(combinations(d.values(),r) for r,_ in enumerate(d,start=1)))
[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

You can change the value of start in enumerate to filter out outputs >= start.

>>> list(chain.from_iterable(combinations(d.values(),r) for r,_ in enumerate(d,start=2)))
[(1, 2), (1, 3), (2, 3), (1, 2, 3)]

>>> list(chain.from_iterable(combinations(d.values(),r) for r,_ in enumerate(d,start=3)))
[(1, 2, 3)]
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52