473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

working out the average within a nested list

Hi all,

another question....

I have a data structure which consists of a game players name and their scores, I have use lists within lists (2D):

results = [["Ben", 15, 18,25], ["George", 25, 35, 85], ["Evie", 2, 54, 84]]

What I want to do is work out the average for each player and then sort by the highest average score.

any idea?

Thanks in advance
May 20 '15 #1
4 4879
dwblas
626 Expert 512MB
Calculate it for each item in the list and create a 2nd list with the average as the first sub-item.
Expand|Select|Wrap|Line Numbers
  1. test_list=[[20, "Ben", 15, 18,25], [50, "George", 25, 35, 85], [19, "Evie", 2, 54, 84]]
  2. test_list.sort
  3. print test_list 
May 20 '15 #2
computerfox
276 100+
Expand|Select|Wrap|Line Numbers
  1. #!/bin/python
  2. results = [["Ben", 15, 18,25], ["George", 25, 35, 85], ["Evie", 2, 54, 84]];
  3. i=0;
  4. while i < len(results):
  5.  j=1;
  6.  scores=[];
  7.  while j < 4:
  8.   scores.append(results[i][j]);
  9.   j+=1;
  10.  scores.sort();
  11.  print results[i][0]+": "+str(scores[2])+","+str(scores[1])+","+str(scores[0]);
  12.  i+=1;
  13.  
http://safe.abelgancsos.com/codepost...ect.php?id=383
May 21 '15 #3
dwblas, you said calculate it for each item, how would you actually do this in python?

computerfox, thanks for the solution is this the most effective way to get an average in Python?
May 21 '15 #4
computerfox
276 100+
The scores that you gave, are those the scores and you want to get the average for each player?
For example, for Ben, 15+18+25/3=19.33 .

In that case, it should be:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/python
  2. results = [["Ben", 15, 18,25], ["George", 25, 35, 85], ["Evie", 2, 54, 84]];
  3. i=0;
  4. while i < len(results):
  5.  j=1;
  6.  scores=[];
  7.  while j < 4:
  8.   scores.append(results[i][j]);
  9.   j+=1;
  10.  avg=float(sum(scores))/float(len(scores));
  11.  print results[i][0]+": "+str(avg);
  12.  i+=1;
  13.  
http://safe.abelgancsos.com/codepost...ect.php?id=384

I'm not aware of any built in function in Python that calculates the average so, yes, I believe this is the way it should be done. Best case, this has a Big O of n^2, which is definitely not nice, but I've seen much worse algorithms.
May 21 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Brett | last post by:
I am trying to pass the value of a nested list into a function (to my "if" statement) as follows: textfilelist = "]] def idfer(listlength, comparelistlength, list): while x < (listlength - 1):...
4
by: Lee K. Seitz | last post by:
I'm still relatively new to stylesheets. I'm trying to do something that seemed fairly simple on the surface, but is proving to be a challenge. I have a set of nested lists: <ul> <li>Side...
11
by: mlf | last post by:
Is there a clever way, or any way for that matter, to have a nested list show 1.1, 1.2, 1.3...2.1, 2.2, etc?
2
by: neildunn | last post by:
Hey guys: >>> >>> def a(): .... print .... >>> a() Traceback (most recent call last): File "<stdin>", line 1, in ?
6
by: deko | last post by:
How do I construct an XHTML-compliant nested unordered list? This displays correctly (both FF and IE): <ul> <li>list item</li> <li>list item</li> <li>list item</li> <ul> <li>nested list...
7
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML...
2
by: torbs | last post by:
Hi I have expanded an suckerfish-type menu ( http://testwww.sogn.vgs.no?p=first ), but I have a problem with Internet Explorer. I believe it is because of the z-index-problem. Am I correct? Can...
2
by: Gentr1 | last post by:
Hi everybody! I am presently working on a Genetic Programming API in python. I have a bit of a problem at the moment... For some specific reasons, I am using nested lists data structure to...
3
by: suneelkn | last post by:
Unable to identify the same level for nested lists in all scenarios, when the nested-list inside an ordered list the conversion process executes with out proper list order for nested list items. The...
3
by: Ahmed Na | last post by:
I have a dynamic link generated from a database i'm trying to wrap every 9 links ( or any N number) inside a container using javaScript, the problem is it counts the children too. My code locks...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.