2

Can you please teach me to construct a dict from lists?

I have two lists:

A = [1, 2, 0, 0, 3]

and

B = ['HAM', 'SPAM', 'HAM', 'HAM', 'SPAM']

I want a dict like this:

Dict = [
...     {'Count': 1., 'Class': 'HAM'},
...     {'Count': 2., 'Class': 'SPAM'},
...     {'Count': 0., 'Class': 'HAM'},
...     {'Count': 0., 'Class': 'HAM'},
...     {'Count': 3., 'Class': 'SPAM'},
... ]

Which include two feature keys 'Count' and 'Class' as well...

Much appreciated! Thanks.

1
  • 1
    That's not a dictionary, that's a list of dictionaries. What have you tried so far? Commented Jul 10, 2014 at 20:09

4 Answers 4

2

That's not a dict, but a list (of dictionaries). Anyway, if what you want is this:

data = [
            {'Count': 1., 'Class': 'HAM'},
            {'Count': 2., 'Class': 'SPAM'},
            {'Count': 0., 'Class': 'HAM'},
            {'Count': 0., 'Class': 'HAM'},
            {'Count': 3., 'Class': 'SPAM'}
       ]

then:

data = [{'Count': float(x[0]), 'Class': x[1]} for x in zip(A, B)]

update

I've updated my answer because I've just noticed that you required a float as value for 'Count'.

Sign up to request clarification or add additional context in comments.

3 Comments

That's basically a copy-paste of my answer.
Sorry, I didn't notice your answer before mine. I can delete mine for honesty if you want, but it wasn't a copy-paste, just the same.
Meh it's fine to leave it up, it show's that there's a consensus :) great minds think alike
1
>>> A = [1, 2, 0, 0, 3]
>>> B = ['HAM', 'SPAM', 'HAM', 'HAM', 'SPAM']

>>> zip(A,B)
[(1, 'HAM'), (2, 'SPAM'), (0, 'HAM'), (0, 'HAM'), (3, 'SPAM')]

>>> [{'Count':i[0], 'Class':i[1]} for i in zip(A,B)]

Output

[{'Count': 1, 'Class': 'HAM'},
 {'Count': 2, 'Class': 'SPAM'},
 {'Count': 0, 'Class': 'HAM'},
 {'Count': 0, 'Class': 'HAM'},
 {'Count': 3, 'Class': 'SPAM'}]

1 Comment

Instead of using an index why not just unpack the values? for key,value in zip(A,B)
1

Instead of a list of dicts (i.e. records), you can use pandas.

pandas is perfect for representing this kind of data.

df = pd.DataFrame({ 'Count': A, 'Class': B})
df
=> 
  Class  Count
0   HAM      1
1  SPAM      2
2   HAM      0
3   HAM      0
4  SPAM      3

[5 rows x 2 columns]

df.Class
=>
0     HAM
1    SPAM
2     HAM
3     HAM
4    SPAM
Name: Class, dtype: object

df.Class[1]
=> 'SPAM'

df.ix[1]
=>
Class    SPAM
Count       2
Name: 1, dtype: object

df.ix[1].Class
=> 'SPAM'

Comments

1

The Dict = [{}, {}, ...] construct you have there is not a dict but a list ([]) of dicts ({}).

To get the required result using zip and list comprehension:

>>> A = [1, 2, 0, 0, 3]
>>> B = ['HAM', 'SPAM', 'HAM', 'HAM', 'SPAM']

>>> [{'Count': a, 'Class': b} for a, b in zip(A, B)]
[{'Count': 1, 'Class': 'HAM'},
 {'Count': 2, 'Class': 'SPAM'},
 {'Count': 0, 'Class': 'HAM'},
 {'Count': 0, 'Class': 'HAM'},
 {'Count': 3, 'Class': 'SPAM'}]

2 Comments

Values of the Count key should be numbers.
@f_ficarola You are right, got confused by the . and the syntax coloring. Fixed, almost the same as others now. Still laving it here for the zip expansion.

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.