hi,
i have a list from a resultset like this
1,"Chicago Bulls",,,""
2,"Cleveland Caveliers",,,""
4,"Detroit Pistons",1,"23686386.35"
4,"Detroit Pistons",2,"21773898.07"
4,"Detroit Pistons",3,"12815215.57"
4,"Detroit Pistons",4,"48522347.76"
4,"Detroit Pistons",5,"28128425.99"
4,"Detroit Pistons",6,"15681603.08"
4,"Detroit Pistons",8,"12627725.03"
4,"Detroit Pistons",9,"11417.00"
4,"Detroit Pistons",10,"945689.22"
4,"Detroit Pistons",11,"818246.57"
4,"Detroit Pistons",12,"1154292.77"
5,"Phoenix Suns",1,"23211445.97"
5,"Phoenix Suns",3,"11268469.53"
5,"Phoenix Suns",4,"49913569.61"
5,"Phoenix Suns",5,"26035236.09"
5,"Phoenix Suns",7,"113953310.50"
5,"Phoenix Suns",8,"17091769.84"
5,"Phoenix Suns",10,"818569.99"
5,"Phoenix Suns",11,"824602.19"
5,"Phoenix Suns",12,"1142018.11"
and I wish to transform it into something like this
1, "Chicago Bulls", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2, "Cleveland Caveliers", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4, "Detroit Pistons", 23686386.35, 21773898.07, 12815215.57,
48522347.76, 28128425.99, 15681603.08, 0, 12627725.03, 11417.00,
945689.22, 818246.57, 1154292.77
5, "Phoenix Suns", 23211445.97, 0, 11268469.53, 499113569.61,
26035236.09, 0, 113953310.50, 17091769.84, 0, 818569.99, 824602.19,
1142018.11
currently my solution is something like this the rows is the original
list which is list of list ... the report is a list of list with the
new arrangement that I want.
report = []
report_item = None
temp_c_id = 0
for row in rows:
if not row[0] == temp_c_id:
temp_c_id = row[0]
if report_item: report.append(report_item)
report_item = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0]
if row[2]:
report_item[row[2]+1] = row[3]
I feel this solution is not that good, can I make this more pythonic?!
Thanks
james :) 5 1261
On Oct 23, 9:46?pm, james_027 <cai.hai...@gmail.comwrote:
hi,
i have a list from a resultset like this
1,"Chicago Bulls",,,""
2,"Cleveland Caveliers",,,""
^ ^^^ ^
| ||| |
1 234 5
How come these records have 5 fields...
4,"Detroit Pistons",1,"23686386.35"
^ ^ ^ ^
| | | |
1 2 3 4
....whereas these only have 4 fields?
4,"Detroit Pistons",2,"21773898.07"
4,"Detroit Pistons",3,"12815215.57"
4,"Detroit Pistons",4,"48522347.76"
4,"Detroit Pistons",5,"28128425.99"
4,"Detroit Pistons",6,"15681603.08"
4,"Detroit Pistons",8,"12627725.03"
4,"Detroit Pistons",9,"11417.00"
4,"Detroit Pistons",10,"945689.22"
4,"Detroit Pistons",11,"818246.57"
4,"Detroit Pistons",12,"1154292.77"
5,"Phoenix Suns",1,"23211445.97"
5,"Phoenix Suns",3,"11268469.53"
5,"Phoenix Suns",4,"49913569.61"
5,"Phoenix Suns",5,"26035236.09"
5,"Phoenix Suns",7,"113953310.50"
5,"Phoenix Suns",8,"17091769.84"
5,"Phoenix Suns",10,"818569.99"
5,"Phoenix Suns",11,"824602.19"
5,"Phoenix Suns",12,"1142018.11"
and I wish to transform it into something like this
1, "Chicago Bulls", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2, "Cleveland Caveliers", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4, "Detroit Pistons", 23686386.35, 21773898.07, 12815215.57,
48522347.76, 28128425.99, 15681603.08, 0, 12627725.03, 11417.00,
945689.22, 818246.57, 1154292.77
5, "Phoenix Suns", 23211445.97, 0, 11268469.53, 499113569.61,
26035236.09, 0, 113953310.50, 17091769.84, 0, 818569.99, 824602.19,
1142018.11
currently my solution is something like this the rows is the original
list which is list of list ... the report is a list of list with the
new arrangement that I want.
report = []
report_item = None
temp_c_id = 0
for row in rows:
if not row[0] == temp_c_id:
temp_c_id = row[0]
if report_item: report.append(report_item)
report_item = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0]
if row[2]:
report_item[row[2]+1] = row[3]
I feel this solution is not that good, can I make this more pythonic?!
Thanks
james :)
hi,
On Oct 24, 1:14 pm, "mensana...@aol.com" <mensana...@aol.comwrote:
On Oct 23, 9:46?pm, james_027 <cai.hai...@gmail.comwrote:hi,
i have a list from a resultset like this
1,"Chicago Bulls",,,""
2,"Cleveland Caveliers",,,""
^ ^^^ ^
| ||| |
1 234 5
How come these records have 5 fields...
4,"Detroit Pistons",1,"23686386.35"
^ ^ ^ ^
| | | |
1 2 3 4
...whereas these only have 4 fields?
sorry, it's a typo error... there are only 4 fields.
thanks
On Oct 23, 10:46 pm, james_027 <cai.hai...@gmail.comwrote:
hi,
i have a list from a resultset like this
1,"Chicago Bulls",,,""
2,"Cleveland Caveliers",,,""
4,"Detroit Pistons",1,"23686386.35"
4,"Detroit Pistons",2,"21773898.07"
4,"Detroit Pistons",3,"12815215.57"
4,"Detroit Pistons",4,"48522347.76"
4,"Detroit Pistons",5,"28128425.99"
4,"Detroit Pistons",6,"15681603.08"
4,"Detroit Pistons",8,"12627725.03"
4,"Detroit Pistons",9,"11417.00"
4,"Detroit Pistons",10,"945689.22"
4,"Detroit Pistons",11,"818246.57"
4,"Detroit Pistons",12,"1154292.77"
5,"Phoenix Suns",1,"23211445.97"
5,"Phoenix Suns",3,"11268469.53"
5,"Phoenix Suns",4,"49913569.61"
5,"Phoenix Suns",5,"26035236.09"
5,"Phoenix Suns",7,"113953310.50"
5,"Phoenix Suns",8,"17091769.84"
5,"Phoenix Suns",10,"818569.99"
5,"Phoenix Suns",11,"824602.19"
5,"Phoenix Suns",12,"1142018.11"
and I wish to transform it into something like this
1, "Chicago Bulls", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2, "Cleveland Caveliers", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4, "Detroit Pistons", 23686386.35, 21773898.07, 12815215.57,
48522347.76, 28128425.99, 15681603.08, 0, 12627725.03, 11417.00,
945689.22, 818246.57, 1154292.77
5, "Phoenix Suns", 23211445.97, 0, 11268469.53, 499113569.61,
26035236.09, 0, 113953310.50, 17091769.84, 0, 818569.99, 824602.19,
1142018.11
currently my solution is something like this the rows is the original
list which is list of list ... the report is a list of list with the
new arrangement that I want.
report = []
report_item = None
temp_c_id = 0
for row in rows:
if not row[0] == temp_c_id:
temp_c_id = row[0]
if report_item: report.append(report_item)
report_item = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0]
if row[2]:
report_item[row[2]+1] = row[3]
This doesn't print the last row (5, "Phoenix Suns", ...); you need one
more append outside the loop (if report_item:
report.append(report_item)).
I feel this solution is not that good, can I make this more pythonic?!
Sure; get familiar with a swiss-knife module, itertools, and in
particular for this task, groupby:
from operator import itemgetter
from itertools import groupby, chain
def group_roster(rows, num_stats=12):
# group the rows by their first two elements
# Caveat: the rows should already be sorted by
# the same key
for key,group in groupby(rows, itemgetter(0,1)):
stats = [0.0] * num_stats
for row in group:
if row[2]: stats[int(row[2])-1] = float(row[3])
yield list(chain(key,stats))
if __name__ == '__main__':
import csv
for row in group_roster(csv.reader(open('roster.txt'))):
print row
HTH,
George
hi james,
this is one implementation using python dictionaries.
report ={}
for row in data:
if not row[0] in report:
report[row[0]] = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0]
if row[2]:
report[row[0]][row[2]+1] = row[3]
reports = report.values()
regards,
Sandip More
james_027 wrote:
hi,
i have a list from a resultset like this
1,"Chicago Bulls",,,""
2,"Cleveland Caveliers",,,""
4,"Detroit Pistons",1,"23686386.35"
4,"Detroit Pistons",2,"21773898.07"
4,"Detroit Pistons",3,"12815215.57"
4,"Detroit Pistons",4,"48522347.76"
4,"Detroit Pistons",5,"28128425.99"
4,"Detroit Pistons",6,"15681603.08"
4,"Detroit Pistons",8,"12627725.03"
4,"Detroit Pistons",9,"11417.00"
4,"Detroit Pistons",10,"945689.22"
4,"Detroit Pistons",11,"818246.57"
4,"Detroit Pistons",12,"1154292.77"
5,"Phoenix Suns",1,"23211445.97"
5,"Phoenix Suns",3,"11268469.53"
5,"Phoenix Suns",4,"49913569.61"
5,"Phoenix Suns",5,"26035236.09"
5,"Phoenix Suns",7,"113953310.50"
5,"Phoenix Suns",8,"17091769.84"
5,"Phoenix Suns",10,"818569.99"
5,"Phoenix Suns",11,"824602.19"
5,"Phoenix Suns",12,"1142018.11"
and I wish to transform it into something like this
1, "Chicago Bulls", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2, "Cleveland Caveliers", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4, "Detroit Pistons", 23686386.35, 21773898.07, 12815215.57,
48522347.76, 28128425.99, 15681603.08, 0, 12627725.03, 11417.00,
945689.22, 818246.57, 1154292.77
5, "Phoenix Suns", 23211445.97, 0, 11268469.53, 499113569.61,
26035236.09, 0, 113953310.50, 17091769.84, 0, 818569.99, 824602.19,
1142018.11
currently my solution is something like this the rows is the original
list which is list of list ... the report is a list of list with the
new arrangement that I want.
report = []
report_item = None
temp_c_id = 0
for row in rows:
if not row[0] == temp_c_id:
temp_c_id = row[0]
if report_item: report.append(report_item)
report_item = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0]
if row[2]:
report_item[row[2]+1] = row[3]
I feel this solution is not that good, can I make this more pythonic?!
Thanks
james :)
sandipm wrote:
hi james,
this is one implementation using python dictionaries.
report ={}
for row in data:
if not row[0] in report:
I'd use:
if row[0] not in report:
report[row[0]] = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0]
if row[2]:
report[row[0]][row[2]+1] = row[3]
reports = report.values()
or even:
report ={}
for code, team, game, bet in data:
if code not in report:
report[code] = [code, team, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]
else:
assert report[code][1] == team
if game:
report[code][game + 1] = bet
reports = report.values()
-Scott David Daniels
Sc***********@Acm.Org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jblazi |
last post by:
Let us assume I have a list like
and would like to transoform it into the string
'{1,2},{7,8},{12,13}'
Which is the simplest way of achiebing this? (The list is in fact much
longer and...
|
by: David Furey |
last post by:
Hi,
I am using the XSLT document to filter records from an XML document
The XSL is shown below:
I have a parameter named "search-for" that is used to bring back a list of
Vendor Names that...
|
by: Daniel Crespo |
last post by:
Is there a built-in method for transforming (1,None,"Hello!") to
1,None,"Hello!"?
Thanks
|
by: Showjumper |
last post by:
I am using the NITF DTD for my xml files and i need to use 2 xsl files to do
the transform: one for the <body.head> and the second for the
<body.content>. I've got this so far for transforming...
|
by: Sergio Otoya |
last post by:
Hi all,
I need to transform an xml document, using xsl to a HTML output.
I can do this successfully using the XslTransform class as below:
Dim oTrans As New XslTransform
...
|
by: Cathie |
last post by:
Hi All,
I am trying to get my style sheet to work. It works fine in IE but I can't
get it to work in .net.
Below is the function I use for transforming, where advancedOptionsFile is
the path...
|
by: Adam Clauss |
last post by:
I have a list of points (their coming in as decimal types, I can convert to PointF) that I am trying to draw to a form.
Unfortunately, the coordinate system these points are coming from is not...
|
by: Puzzled |
last post by:
http://www.biglist.com/lists/xsl-list/archives/200303/msg01242.html purports
to show how xslt can be used to copy all of an xhtml file & selectively
transform certain nodes. The copy works fine on...
|
by: psbasha |
last post by:
Hi,
I have a Point and Transfoirmation matrix.How to translate the Point from one Coordinate system to other using list .
Point1 =
#Transformation Matrix
T =
,
,
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |