Solved

Python iterate over two arrays at the same time

Posted on 2015-02-11
1
124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

717 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