1

Can I assign a dictionary key to a list OR list of lists. Then take averages, cov, etc it:

input={'03439', '03440'}
list of list= [[PersonA, 7, 8, 9, 10, 11],[PersonB, 12, 13, 14, 15, 16]]

Desired Output

{'03439': [PersonA, 7, 8, 9, 10, 11],'03440':[PersonB, 12, 13, 14, 15, 16]}

If I wanted to take the average values of the above, can I do so even with part of this being string and the other values?

3
  • 3
    What is dct? The input? The output? BTW, dct is a set. Applying the average to elements of different types has nothing to do with python, but with the concept of comparing apples to starfish Commented Dec 23, 2015 at 23:28
  • 2
    It feels kind of weird to have "keys" in a set and "corresponding" (?) values in a separated list. Mind explaining where your data comes from ? Commented Dec 23, 2015 at 23:42
  • Sets are unordered so unless you sort or do some custom ordering then there is no way to know what will be paired Commented Dec 25, 2015 at 14:44

3 Answers 3

2

In case you wanted to calculate the average there and then you could do

>>> PersonA = 'Pino'
>>> PersonB = 'Gino'
>>> i=('03439', '03440') # tuple
>>> l=[[PersonA, 7, 8, 9, 10, 11],[PersonB, 12, 13, 14, 15, 16]]
>>> av={k:[v[0], sum(v[1:])/(len(v)-1)] for k,v in zip(i,l)}
>>> av
{'03439': ['Pino', 9.0], '03440': ['Gino', 14.0], }

Nota Bene: for the zipping to work input must be an iterable which can keep the order of its elements, e.g. a tuple or a list. Sets and plain dictionaries cannot do it.

And the answer is no, integers cannot be summed with strings

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

Comments

0

Using izip and list comprehension

from itertools import izip
     [dict([(x,y)])for x,y in izip({'03439', '03440'},[['PersonA', 7, 8, 9, 10, 11],['PersonB', 12, 13, 14, 15, 16]])]

output

[{'03439': ['PersonA', 7, 8, 9, 10, 11]}, {'03440': ['PersonB', 12, 13, 14, 15, 16]}]

1 Comment

Nota Bene: for the zipping to work input must be an iterable which can keep the order of its elements, e.g. a tuple or a list. Sets and plain dictionaries cannot do it.
0

Use zip and just construct a dict:

>>> dict(zip(input, list_of_lists))
{'03439': ['PersonA', 7, 8, 9, 10, 10], '03440': ['PersonB', 12, 13, 14, 15, 16]}

1 Comment

Nota Bene: for the zipping to work input must be an iterable which can keep the order of its elements, e.g. a tuple or a list. A set cannot do it.

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.