473,385 Members | 1,359 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,385 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 4878
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.