我想要生成一个字典,其中六种不同组合的三组不同的图片被组织起来。这就是我计算字典的原因:
import glob, os, random, sys, time
import numpy.random as rnd
im_a = glob.glob('./a*') # upload pictures of the a-type, gives out a List of .jpg-files
im_n = glob.glob('./n*') # n-type
im_e = glob.glob('./e*') # e-type
# combining Lists of Pictures
A_n = im_a + im_n
N_a = im_n + im_a
A_e = im_a + im_e
E_a = im_e + im_a
E_n = im_e + im_n
N_e = im_n + im_e
# making a Dictionary of Pictures and Conditions
PicList = [A_n, N_a, A_e, E_a, E_n, N_e] # just the six Combinations
CondList = [im_a,im_n,im_a,im_e,im_e,im_n] # images that are in the GO-Condition
ImageList = []
ImageList.append({'PicList':PicList, 'CondList':CondList})
在这一点上有两个问题:
CondList
与PicList
不匹配。最好直接将PicList
与CondList
联系起来。PicList
A_n
与CondList
im_a
和N_a-im_n, A_e-im_a
.发布于 2015-04-15 11:10:54
要回答第一点,可以使用itertools.permutations
import itertools as it
elements = [im_a, im_n, im_e]
perms = it.permutations(elements, 2) # 2 -> take pairs
pic_list = [sum(perm, []) for perm in perms]
返回的顺序是字典,即
im_a + im_n, im_a + im_e, im_n + im_a, im_n + im_e, im_e + im_a, im_e + im_n
只需执行以下操作,就可以构建相应的cond_list
:
cond_list = [im_a] * 2 + [im_n] * 2 + [im_e] * 2
或者,更广泛地说:
d = len(elements) - 1
cond_list = list(chain.from_iterable([el]*d for el in elements))
# or
cond_list = sum(([el]*d for el in elements), [])
https://stackoverflow.com/questions/23570299
复制