1

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..

5
  • So you need it to have specific order, if the first entry was the key 2, you need to access it in the matrix using the first/second/whatever position it had, not the value of the key, right? I mean it because dictionary's don't have specific order, so the keys are not in any given order. Commented Mar 29, 2017 at 11:54
  • Welcome to Stack Overflow! Please edit your question to show what you have tried so far. You should include at least an outline (but preferably a minimal reproducible example) of the code that you are having problems with, then we can try to help with the specific problem. You should also read How to Ask. Commented Mar 29, 2017 at 11:58
  • @valeas No. If key=2, then from the data of 2: [9, ['b', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']], we simply count the occurrence of a,b and c's; and so on for other keys. Commented Mar 29, 2017 at 12:11
  • Then if you can access the new matrix using the original key, I would go with @reut-sharabani 's answer Commented Mar 29, 2017 at 12:15
  • @valeas Thanks for that information. Is there a way to skip items in it. Like in this, I want to skip 5 as it has value less than 9 (as others have) Commented Mar 29, 2017 at 13:07

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

good answer - possibly worth highlighting that the use of get will report the zeroes required, e.g.matrix[0].get('c', 0) is 0.
@valeas No. Order is specific. I have edited the question.
1

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.

1 Comment

Thanks for that info. Is there a way to skip the key 5 containing value <9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.