Anand
Anand

Reputation: 13

Make matrix out of the dictionary in Python

I have this data (taken part of random data coming from some server):

 data={2: [9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']], 3: [9,
 ['c', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']], 5: [5, ['c', 'b', 'a',
 'b', 'b']], 7: [9, ['c', 'c', 'a', 'b', 'a', 'b', 'a', 'b', 'a']]}

In this data, first number is the key, second value is the number of entries in the bracket following it. eg:- For data

2: [9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']]

2 is the key. 9 is the total number of entries in the bracket which follows it.

Also, for keys having second value less than 9 are to be discarded. I got that data.values() can give me the value as

[[9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']], [9, ['c', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']], [5, ['c', 'b', 'a', 'b', 'b']], [9, ['c', 'c', 'a', 'b', 'a', 'b', 'a', 'b', 'a']]}

But I am not able to find any way to index within the matrix.

I need to make matrix out of this data as follows:

     a    b    c
2    4    5    0
3    4    4    1
7    4    3    2

The matrix value [1][1] is the sum of a's in key value 2, [1][2] is the sum of b's in key value 2 and [1][3] is the sum of c's in key value 2, [2][1] is the sum of a's in key value 3, and so on..

Upvotes: 1

Views: 136

Answers (2)

Reut Sharabani
Reut Sharabani

Reputation: 31339

You can use Collections.Counter:

from collections import Counter

data = {
    2: [9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']],
    3: [9, ['c', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']],
    5: [5, ['c', 'b', 'a', 'b', 'b']],
    7: [9, ['c', 'c', 'a', 'b', 'a', 'b', 'a', 'b', 'a']]}

matrix = {k: Counter(v[1]) for k, v in data.iteritems() if v[0] >= 9}

The value of matrix is now:

{
     2: Counter({'b': 5, 'a': 4}),
     3: Counter({'a': 4, 'b': 4, 'c': 1}),
     7: Counter({'a': 4, 'b': 3, 'c': 2})
}

Accessing a member of the matrix can be done as follows:

matrix[2].get('a', 0)

(Note: using dict.get is to make the default return 0, which indicates no occurences so the key is not created in the Counter object).

Which will give the value:

4

Upvotes: 2

Foryah
Foryah

Reputation: 193

Probably Reut Sharabani's answer is better and faster, but here's how I would do it :

data = {
    2: [9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']],
    3: [9, ['c', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']],
    5: [5, ['c', 'b', 'a', 'b', 'b']],
    7: [9, ['c', 'c', 'a', 'b', 'a', 'b', 'a', 'b', 'a']]
}

matrix = {key: {k: val[1].count(k) for k in set(val[1])} for key, val in data.iteritems() if val[0] >=9}

Which will give you :

{2: {'a': 4, 'b': 5},
 3: {'a': 4, 'b': 4, 'c': 1},
 5: {'a': 1, 'b': 3, 'c': 1},
 7: {'a': 4, 'b': 3, 'c': 2}}

And, of course, use the .get('c', 0) so you get 0's for values which are 0.

*Edited to adapt to original question, keys below 9 must be discarded.

Upvotes: 1

Related Questions