Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting tuples
#1
Hi guys.

Hope you can help

I need to split my tuples into separate columns. This is what I got

import csv 

with open('geyserData.csv', 'r') as f:
    reader = csv.reader(f)
    your_list = tuple(reader)
    
    
eruption, waiting = zip(*your_list)
What I need is to have the split with eruptions and waiting, without the row number

(['eruptions', 'waiting'],
 ['1', '3.600', '79'],
 ['2', '1.800', '54'],
 ['3', '3.333', '74'],
 ['4', '2.283', '62'],
 ['5', '4.533', '85'],
 ['6', '2.883', '55'],
 ['7', '4.700', '88'],
 ['8', '3.600', '85'],
Instead, it returns this when using eruption and waiting

('eruptions',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 '10',
 '11',
 '12',
('waiting',
 '3.600',
 '1.800',
 '3.333',
 '2.283',
 '4.533',
 '2.883',
 '4.700',
 '3.600',
 '1.950',
 '4.350',
 '1.833',
 '3.917',
 '4.200',
 '1.750',
 '4.700',
 '2.167',
 '1.750',
 '4.800',
How can I make it return it so index is not used? In my mind, I thinking I should enter the tuple by index and use [1] and [2]. Hope it makes sense :( Can you help?

Have a good day :)
Reply
#2
The problem is that your file header does not have all of the columns in the data. The easy fix would be to change the file to correct the header. If you can't do that because it's homework, instead of doing tuple(reader), do list(reader). That will allow you to change the header (your_list[0]) in the program. Change it to ['index', 'eruptions', 'waiting']. Then you can zip and get three columns out, including the correct eruptions and waiting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It seems to me that if you unpack it into variables eruption and waiting then you probably don't need first values to be 'eruptions' or 'waiting'. In this case one can just consume first row which is not needed with header = next(reader)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Cool! Thank you for the answer guys! Ofcourse, now I see the problem in the file too. I think the teacher made the mistake on purpose, to mess with us a bit with ;)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020