-2

I have the following list:

x = [(27.3703703703704, 2.5679012345679, 5.67901234567901,
      6.97530864197531, 1.90123456790123, 0.740740740740741,
      0.440136054421769, 0.867718446601942),
     (25.2608695652174, 1.73913043478261, 6.07246376811594,
      7.3768115942029, 1.57971014492754, 0.710144927536232,
      0.4875, 0.710227272727273)]

I'm looking for a way to get the average of each of the lists nested within the main list, and create a new list of the averages. So in the case of the above list, the output would be something like:

[[26.315],[2.145],[5.87],etc...]

I would like to apply this formula regardless of the amount of lists nested within the main list.

Justin
  • 37
  • 9
  • 1
    You have a list of tuples and the average of them won't be what you expect! – Mazdak Oct 01 '15 at 05:55
  • You have a list of tuples that contains one-item lists. Is this actually what you want to have? – Hayley Guillou Oct 01 '15 at 05:58
  • Apply the average over nested lists regardless of how nested they are? In your example all of your nested lists contain one element, so the average is just the single value in the list. Are you trying to zip the lists and average them pair wise? – zstewart Oct 01 '15 at 06:01
  • 1
    Do you really want a list of one-element lists? – Adam Smith Oct 01 '15 at 06:07
  • Sorry all, just realised I had the wrong code in my program, I've adjusted it to the code above, a list of tuples. Still looking for a solution. – Justin Oct 01 '15 at 06:07
  • Possible duplicate of [Python - merge items of two lists into a list of tuples](http://stackoverflow.com/questions/2407398/python-merge-items-of-two-lists-into-a-list-of-tuples) – zstewart Oct 01 '15 at 06:28

4 Answers4

0

I assume your list of tuples of one-element lists is looking for the sum of each unpacked element inside the tuple, and a list of those options. If that's not what you're looking for, this won't work.

result = [sum([sublst[0] for sublst in tup])/len(tup) for tup in x]

EDIT to match changed question

result = [sum(tup)/len(tup) for tup in x]

EDIT to match your even-further changed question

result = [[sum(tup)/len(tup)] for tup in x]
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

An easy way to acheive this is:

means = []     # Defines a new empty list
for sublist in x:     # iterates over the tuples in your list
    means.append([sum(sublist)/len(sublist)])    # Put the mean of the sublist in the means list

This will work no matter how many sublists are in your list. I would advise you read a bit on list comprehensions: https://docs.python.org/2/tutorial/datastructures.html

GeorgeDean
  • 83
  • 1
  • 7
  • Ah ha, and OP changed the question. – Adam Smith Oct 01 '15 at 06:04
  • Well then the answer is now wrong of course, but you answered it. I would still advise to read the doc though... – GeorgeDean Oct 01 '15 at 06:09
  • Actually the answer was wrong before (when the question had the form `x = [ ( [1], [2], ... ), ... ]`. Now it's almost right, since apparently OP is looking for a list of one-element lists? I have no idea why he might want that though – Adam Smith Oct 01 '15 at 06:10
  • True, I corrected to give lists of one-element list. – GeorgeDean Oct 01 '15 at 06:13
0

It looks like you're looking for the zip function:

[sum(l)/len(l) for l in zip(*x)]

zip combines a collection of tuples or lists pairwise, which looks like what you want for your averages. then you just use sum()/len() to compute the average of each pair.

*x notation means pass the list as though it were individual arguments, i.e. as if you called: zip(x[0], x[1], ..., x[len(x)-1])

zstewart
  • 2,093
  • 12
  • 24
-2
r = [[sum(i)/len(i)] for i in x]
Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
  • Should throw a TypeError, since you can't `sum` a bunch of tuples. If you nest it once it should throw a TypeError because you can't divide a list by an integer. – Adam Smith Oct 01 '15 at 06:00
  • Your edit makes even less sense. Now you can sum the outer lists, but you still can't do anything useful with them. – Adam Smith Oct 01 '15 at 06:02
  • Now that OP changed his code, your new method should throw an AttributeError, type `int` has no attribute `__iter__` (or something like that) – Adam Smith Oct 01 '15 at 06:08
  • @ Adam Smith why `int` – Luka Rahne Oct 01 '15 at 06:16