0

这是我的代码:

if condition == '1':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '2':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '3':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")
elif condition == '4':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '5':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '6':
    a = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist1/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")
elif condition == '7':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '8':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")
elif condition == '9':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")
elif condition == '10':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '11':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '12':
    d = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '13':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '14':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '15':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '16':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    e = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '17':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    e = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '18':
    c = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    e = glob.glob("DDtest/targetimagelist4/*")
elif condition == '19':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '20':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist2/*")
    a = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")
elif condition == '21':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist4/*")
elif condition == '22':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    c = glob.glob("DDtest/targetimagelist2/*")
    d = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '23':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist4/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    c = glob.glob("DDtest/distractorimagelist3/*")
    a = glob.glob("DDtest/targetimagelist4/*")
elif condition == '24':
    e = glob.glob("DDtest/targetimagelist1/*")
    b = glob.glob("DDtest/distractorimagelist3/*")
    d = glob.glob("DDtest/targetimagelist2/*")
    a = glob.glob("DDtest/distractorimagelist3/*")
    c = glob.glob("DDtest/targetimagelist4/*")

基本上,它的作用是根据 a、c、d、e 的所有组合从不同的列表中提取,并使 a 和 b 对应。这段代码工作正常,但不是很漂亮。我想知道是否有人对我用更少的代码行做同样的事情有任何建议。最重要的是,我只是想扩展我编写代码的知识,所以如果你知道有一种更雄辩的方式来写这个,请告诉我。:)

4

3 回答 3

2

不确定“使 a 和 b 对应”是什么意思,但也许这会起作用:

from itertools import permutations
from random import choice

globs = [glob.glob("DDtest/targetimagelist1/*"),
         glob.glob("DDtest/distractorimagelist1/*"),
         glob.glob("DDtest/targetimagelist2/*"),
         glob.glob("DDtest/distractorimagelist3/*"),
         glob.glob("DDtest/targetimagelist4/*")]

glob_permutations = list(permutations(globs))

a, b, c, d, e = choice(glob_permutations)
于 2015-03-04T20:23:40.837 回答
1

内置itertools库具有不重复排列的功能:

#!/usr/bin/env python2
import itertools
import glob

p = [
    glob.glob("DDtest/targetimagelist1/*"),
    glob.glob("DDtest/distractorimagelist4/*"),
    glob.glob("DDtest/targetimagelist2/*"),
    glob.glob("DDtest/distractorimagelist3/*"),
    glob.glob("DDtest/targetimagelist4/*"),
]

perm = itertools.permutations(p)

print(len(perm))

=> 120 个排列

于 2015-03-04T20:24:40.120 回答
0

也许是这样?

setup = {'1': ('1','1','2','3','4'),
         '2': ('1','1','2','3','4'),
        #...
        }
a = glob.glob("DDtest/targetimagelist" + setup[condition][0] + "/*")
b = glob.glob("DDtest/distractorimagelist" + setup[condition][1] + "/*")
c = glob.glob("DDtest/targetimagelist" + setup[condition][2] + "/*")
d = glob.glob("DDtest/distractorimagelist" + setup[condition][3] + "/*")
e = glob.glob("DDtest/targetimagelist" + setup[condition][4] + "/*")
于 2015-03-04T19:58:55.757 回答