Solved

Python iterate over two arrays at the same time

Posted on 2015-02-11
1
113 Views
Last Modified: 2015-02-19
Dear all,

I'm trying to write to CSV in the following format.

Latitude  Longitude  Date1 Date2 Date3 etc

I have written the header, then to write the subsequent rows I need to get the same index for lat and lon append to content array, then iterate through the indexes of the date array to find the right value for sfc and append to content array.

At the moment i'm iterating through the first lat, then all the lons.  I'm not sure how to keep track of both indexes of lat/lon.

Once one row of data has been stored correctly I will write to file i.e. outputwriter.writerow(content)

In order to build the content array below is an example of the indexes for a row of data
sfc[time_index, lat_index, lon_index]
sfc[1, 1, 1]
sfc[2, 1,1]
sfc[3, 1,1]

and repeat for the next lat/lon iterating through the dates

data = sfc[time_index, lat_index, lon_index]
sfc[1, 2, 2]
sfc[2, 2,2]
sfc[3, 2,2]


header = ['Latitude', 'Longitude']
with open('Output.csv', 'wb') as csvFile:
    outputwriter = csv.writer(csvFile, delimiter=',')
    for time_index, time in enumerate(times): # pull the dates out for the header
         t = num2date(time, units = units, calendar='365_day')
         header.append(t)
    outputwriter.writerow(header) 
    
    for lat_index, lat in enumerate(lats):
        content = [lat] # first value in row lat
        #print lat_index
        for lon_index, lon in enumerate(lons):
            content.append(lon) # second value in row lon
            for time_index, time in enumerate(times): 
                n = num2date(time, units = units, calendar='365_day') # debug purposes only print to console
                data = sfc[time_index,lat_index,lon_index]
                if data != "--":
                   content.append(data)
                   print "data at:", n, lat, lon
                   print time_index,lat_index,lon_index
                   print data

Open in new window


I'm unsure of a way to implement this in python, thanks
0
Comment
Question by:AndyC1000
1 Comment
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 40605094
It's tough to tell from the sample data that you posted what exactly you've got there.  It might make things a little more clear if you posted a small subset of your time, lats, lons, and sfc arrays-- not just indices-- and what you would like that subset translated to as far as CSV goes. But... here's a guess based on what you did post.  
from collections import defaultdict
lats = [10.5, 22.3, 94.6]
lons = [33.1, 44.6, 102.8]
times = ["1:00am", "1:30am", "2:00am", "2:30am"]
sfc = dict()
sfc[1,1,1] = ""
sfc[2,1,1] = ""
sfc[3,1,1] = ""

sfc[1,2,2] = ""
sfc[2,2,2] = ""
sfc[3,2,2] = ""

lat_lon_times = defaultdict(list)
for (time_index, lat_index, lon_index) in sfc.keys():
    lat_lon = (lats[lat_index], lons[lon_index])
    lat_lon_times[lat_lon].append(times[time_index])

for (k, v) in lat_lon_times.items():
    data = list(k)
    data.extend(sorted(v))
    print data

Open in new window

The above prints:
[22.3, 44.6, '1:30am', '2:00am', '2:30am']
[94.6, 102.8, '1:30am', '2:00am', '2:30am']

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question