473,503 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please Help, Appending into list problem!

2 New Member
I figured out the answer myself and no longer need this post.
Apr 20 '10 #1
1 1147
bvdet
2,851 Recognized Expert Moderator Specialist
Ok, so lets say I have a .csv file called flags.csv, with the following data

(Column 1)- (Column 2)
Country- Colour
Australia- Red
Australia- Red
Australia- Red
America - Red
UK - Red
Japan- Red
China - Red
Australia- White
America - White
UK - White
Japan - White
Australia -Blue
America - Blue
UK - Blue
China - Yellow


I would like to arrange this data so as to present the countries with flags of at least three colours, and the total number of colours they have. I want the output to end up in this format.
Column1-Column2-Column3-Column4-Column5
--------- Blue - Red - White - Yellow - Total
America-- 1 - 3 - 1 - 0 - 5
Australia- 1 - 1 - 1 - 0 - 3
UK------- 1 - 1 - 1 - 0 - 3



I have no idea what the formulas for manipulating the data so as it forms in this way are. Could you please give me some help?

Thank you.
Compile the data into a dictionary. From there you can extract the data you need and convert to another format if needed.
Expand|Select|Wrap|Line Numbers
  1. data = """Country- Colour
  2. Australia- Red
  3. Australia- Red
  4. Australia- Red
  5. America - Red
  6. UK - Red
  7. Japan- Red
  8. China - Red
  9. Australia- White
  10. America - White
  11. UK - White
  12. Japan - White
  13. Australia -Blue
  14. America - Blue
  15. UK - Blue
  16. China - Yellow"""
  17.  
  18. dataList = data.split("\n")[1:]
  19. dd = {}
  20. for item in dataList:
  21.     country, color = [s.strip() for s in item.split("-")]
  22.     dd.setdefault(country, []).append(color)
  23.  
  24. print
  25. print "   Country: Blue  Red  White Yellow Total"
  26.  
  27. for country in dd:
  28.     print "%10s:%5s%5s%5s%5s%5s" % (country,
  29.                                     dd[country].count("Blue"),
  30.                                     dd[country].count("Red"),
  31.                                     dd[country].count("White"),
  32.                                     dd[country].count("Yellow"),
  33.                                     len(dd[country]))
The output:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2.    Country: Blue  Red  White Yellow Total
  3.      Japan:    0    1    1    0    2
  4.  Australia:    1    3    1    0    5
  5.      China:    0    1    0    1    2
  6.    America:    1    1    1    0    3
  7.         UK:    1    1    1    0    3
  8. >>> 
Apr 21 '10 #2

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

Similar topics

7
1347
by: rh0dium | last post by:
Hi all, I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So...
1
2010
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
1
9584
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
1992
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
5
2706
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
4
2707
by: Bucco | last post by:
I am trying to compare a list of items to the list of files generated by os.listdir. I am having trouble getting this to work and think I may be going down the wrong path. Please let me know if...
5
1641
by: John Salerno | last post by:
Here's the code: class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY) panel = wx.Panel(self) dbconn = self.connect_db() dbconn.execute("select namefirst,...
0
1690
by: jon | last post by:
Hi there, I'm brand new to Access and may be trying to do too much too soon, but I wanted to get some expert advice on how the best way to go about what I am trying to accomplish would be. I...
16
3462
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
13
3206
by: yueying53 | last post by:
I am new to creating a list of classes. Hence I checked out this website: http://www.daniweb.com/code/snippet390.html. However, I modified the code to check if a class with a particular has been...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
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...
1
7012
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
7468
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...
1
5023
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...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.