everyone!
I have this dictionary which contains name of files as keys and some data as values.
In the name of the file there is some float number, which indicates if the test that was done on this data was on positive or negative of some field.
I want to separate this dictionary into two lists, eg: positive and negative. These two list corresponds to the float values of the name of the files containing positive and negative
The following code separated the values according to the float number. But return 3D list.
Question How can I use list comprehension to have a 2D list as a result in this case?
mainDictlist = [{'A1_0.5.txt':[[4,4,4],[4,4,4]],'B1_-0.5.txt':[[1,2,3],[1,2,3]],'A2_0.5.txt'[[2,2,2],[2,2,2]],'B2_-0.5.txt':[[1,1,1],[1,1,1]]}]
def findfloat(keys):
lst_numberStr = re.findall(r'[*+-]?\d+\.\d+', keys)
v = float(lst_numberStr[0])
return v
positive = []
negative = []
for item in mainDictlist:
for k, v in item.items():
findFloat = findfloat(k)
if findFloat > 0:
positive.append(item[k])
if findFloat < 0:
negative.append(item[k])
print('List Pos',positive)
print('List Neg',negative)
Returns
>>>List Pos [[[4, 4, 4], [4, 4, 4]], [[2, 2, 2], [2, 2, 2]]]
>>>List Neg [[[1, 2, 3], [1, 2, 3]], [[1, 1, 1], [1, 1, 1]]]
Desired Return
List Pos [[4, 4, 4], [4, 4, 4]], [[2, 2, 2], [2, 2, 2]]
List Neg [[1, 2, 3], [1, 2, 3]], [[1, 1, 1], [1, 1, 1]]
ValueError Traceback (most recent call last)