生成多个列表的所有可能排列并在python中访问它们

时间:2018-01-25 15:45:21

标签: python

我有两个置换列表,其中一个包含字符串键和其他相应的值:

list1的

('inputData1', 'targetData1')
('inputData1', 'targetData2')
('inputData1', 'targetData3')
('inputData2', 'targetData1')
('inputData2', 'targetData2')
('inputData2', 'targetData3')
...

list2代表相应的inputData1targetData1

list2

(array([[  30.1678, -173.569 ,  725.724 ],
   [  29.9895, -173.34  ,  725.76  ],
   [  29.9411, -173.111 ,  725.768 ],
   [  29.9306, -173.016 ,  725.98  ],
   [  29.6754, -172.621 ,  725.795 ],
   [  29.5277, -172.274 ,  725.903 ],
   [  29.585 , -171.978 ,  726.111 ],
   [  29.4114, -171.507 ,  726.188 ],
   [  29.3951, -170.947 ,  726.173 ],
   [  29.3577, -170.196 ,  726.384 ],
   [  29.3594, -169.771 ,  726.52  ]]), 
array([[  14.8016, -175.911 ,  779.752 ],
   [  14.7319, -175.483 ,  779.504 ],
   [  14.5022, -175.087 ,  779.388 ],
   [  14.4904, -174.576 ,  779.416 ],
   [  14.4881, -174.058 ,  779.452 ],
   [  14.1612, -173.558 ,  779.459 ],
   [  14.0831, -172.938 ,  779.675 ],
   [  13.9405, -172.117 ,  780.146 ],
   [  13.8627, -171.233 ,  780.568 ],
   [  14.056 , -170.34  ,  780.876 ],
   [  14.1102, -169.477 ,  781.265 ]]))

要生成键和值的排列,我使用itertools.product()函数:

strIdx= []
for strIdx in itertools.product(inputDataNameStrings, targetDataNameStrings):
    print('dataNameIndex_Pairs:', strIdx)

valIdx= []
for valIdxin itertools.product(inputDataValue, targetDataValue):
    print('dataValueIndex_Pairs:\n', self.valIdx)

我的目标是从上面的排列中再次获得置换输出并通过键访问每个项目:

list3=
(('inputData1' [inputData1ValueArray]), ('targetData1' [targetData1ValueArray]))
(('inputData1' [inputData1ValueArray]), ('targetData2' [targetData2ValueArray]))
(('inputData1' [inputData1ValueArray]), ('targetData3' [targetData3ValueArray]))
(('inputData2' [inputData2ValueArray]), ('targetData1' [targetData1ValueArray]))

我该怎么做? 感谢您的任何建议

2 个答案:

答案 0 :(得分:0)

我真的不确定你想要什么,但这就是你的追求:

list1= [('inputData1', 'targetData1'),
        ('inputData1', 'targetData2'),
        ('inputData1', 'targetData3'),
        ('inputData2', 'targetData1'),
        ('inputData2', 'targetData2'),
        ('inputData2', 'targetData3')]

list2 = [[i] for i in range(len(list1) * 2)]
# [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]]

list2 = [tuple(list2[i:i+2]) for i in range(0, len(list2), 2)]
# [([0], [1]), ([2], [3]), ([4], [5]), ([6], [7]), ([8], [9]), ([10], [11])]

print([((x1, y1), (x2, y2)) for (x1, x2), (y1, y2) in zip(list1, list2)])

哪个输出:

[(('inputData1', [0]), ('targetData1', [1])), 
 (('inputData1', [2]), ('targetData2', [3])), 
 (('inputData1', [4]), ('targetData3', [5])), 
 (('inputData2', [6]), ('targetData1', [7])), 
 (('inputData2', [8]), ('targetData2', [9])), 
 (('inputData2', [10]), ('targetData3', [11]))]

答案 1 :(得分:0)

由于我没有那些变量值,所以我稍微修改了一下,如果你已经在某处定义了这些值,则不必修改list2:

Price   Units  Product  CumSum  CumSumWithThreshold
  5       100     500     500          500
  6       200    1200    1700         1500
  7       250    1750    3450            0
  8      1000    8000   11450            0

输出:

list1=[('inputData1', 'targetData1'),
('inputData1', 'targetData2'),
('inputData1', 'targetData3'),
('inputData2', 'targetData1'),
('inputData2', 'targetData2'),
('inputData2', 'targetData3')]


list2=[['inputData1ValueArray'], ['targetData1ValueArray'],
['inputData1ValueArray'], ['targetData2ValueArray'],
['inputData1ValueArray'], ['targetData3ValueArray'],
['inputData2ValueArray'], ['targetData1ValueArray'],
['inputData2ValueArray'], ['targetData2ValueArray'],
['inputData2ValueArray'], ['targetData3ValueArray']]



final_=[]
for i in list1:
    for j in list2:
        sub_={}
        if i[0] in j[0]:
            sub_[i[0]]=j[0]
        elif i[1] in j[0]:
            sub_[i[1]]=j[0]
        if sub_:
            final_.append(sub_)

print(final_)