Python Forum

Full Version: Splitting tuples
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 :)
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.
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)
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 ;)