1

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]]
Angel Lira
  • 293
  • 3
  • 12

2 Answers2

1

Use list.extend

Ex:

positive = []
negative = []
for item in mainDictlist:
    for k, v in item.items():
        findFloat = findfloat(k)
        if findFloat > 0:               
            positive.extend(item[k])            
        if findFloat < 0:
            negative.extend(item[k])
print('List Pos',positive)
print('List Neg',negative)

Output:

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]]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

Your example of desired return is not a 2D list, but two separate lists. In order to get a 2D list like [[4, 4, 4], [4, 4, 4], [2, 2, 2], [2, 2, 2]] you can replace positive.append(item[k]) with positive += v and do the same for negative.

Also, judging from the example you provided, filenames with positive floats doesn't contain - while those with negative do. So, separation of values can be done this way:

positive = []
negative = []
for item in mainDictlist:
    for k, v in item.items():
        if "-" in k:
            negative += v
        else:
            positive += v

Hope this helps.

Ivan T.
  • 31
  • 3
  • Hi Ivan, not sure if you can append to a list using += operation. – Angel Lira Jun 10 '20 at 12:29
  • In fact, you [can](https://stackoverflow.com/questions/3653298/concatenating-two-lists-difference-between-and-extend/37762884). – Ivan T. Jun 10 '20 at 12:32
  • Well, that is the errror I got using your idea.
    ValueError Traceback (most recent call last) in 8 negative1 += v 9 else: ---> 10 positive1 += v 11 ValueError: operands could not be broadcast together with shapes (0,) (2,3)
    – Angel Lira Jun 10 '20 at 12:40
  • Hm, this is strange. Are you using pure python? Can you give a full piece of code that doesn't work? – Ivan T. Jun 10 '20 at 15:50
  • I am using Jupyter Notebook with Python 3, does it has any difference than using Python?? – Angel Lira Jun 11 '20 at 08:13
  • I don't think so, however, something is not right though. The variable with dictionaries, `mainDictlist`, is it a list or a numpy array? – Ivan T. Jun 11 '20 at 18:19