Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94409

Re: Find Minimum for element in multiple dimensional array

From Emile van Sebille <emile@fenx.com>
Subject Re: Find Minimum for element in multiple dimensional array
Date 2015-07-22 16:26 -0700
References <a7fe6fd3-2a51-40a9-b995-33fc88cb27ee@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.891.1437607638.3674.python-list@python.org> (permalink)

Show all headers | View raw


On 7/22/2015 3:54 PM, Robert Davis wrote:
> I have an array of arrays that have a origin zip code, origin latitude, origin longitude, destination zip code, destination latitude, destination longitude, and miles between the two points.
>
> I need to keep only those combinations that represent the minimum mileage between to the destination zip code. For example a point in New Jersey may have a distance from the Philadelphia Office that is 45 miles, from the Newark Office that is 78 miles and one from the Delaware Office that is 58 miles.
>
> I need to keep the mileage from the Philadelphia Office that is 45 miles and produce a .csv file that has origin zip code, origin latitude, origin longitude, destination zip code, destination latitude, destination longitude, and miles between the two points.
>
> The array looks like this:
>
> [['37015', 'TN31', 36.2777, -87.0046, 'NY', 'White Plains', '10629', 41.119008, -73.732996, 77.338920003],
> ['72202', 'ARB1', 34.739224, -92.27765, 'NY', 'White Plains', '10629', 41.119008, -73.732996, 1099.7837975322097]]

Assume the array in A:

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---
A= [['37015', 'TN31', 36.2777, -87.0046, 'NY', 'White Plains', '10629', 
41.119008, -73.732996, 77.338920003],
  ['72202', 'ARB1', 34.739224, -92.27765, 'NY', 'White Plains', '10629', 
41.119008, -73.732996, 1099.7837975322097]]

# transform to a dict ignoring dups
D = dict( [ ( ((r[6],r[0]),r[-1]), r) for r in A ] )

# convert to a sorted list
L = sorted(D.items())

# then print and filter out any duplicated entries
lastzippair = (None,None)

for ky,rec in L:
     if ky[:2] == lastzippair:
         continue
     print ky,":",rec
     lastzippair=ky[:2]

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

The results are what you'd write to the csv file.

Tested only with the data you provided.

HTH,

Emile

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Find Minimum for element in multiple dimensional array Robert Davis <rdavis7408@gmail.com> - 2015-07-22 15:54 -0700
  Re: Find Minimum for element in multiple dimensional array Emile van Sebille <emile@fenx.com> - 2015-07-22 16:26 -0700
  Re: Find Minimum for element in multiple dimensional array Robert Davis <rdavis7408@gmail.com> - 2015-07-23 05:31 -0700
  Re: Find Minimum for element in multiple dimensional array Robert Davis <rdavis7408@gmail.com> - 2015-07-23 14:50 -0700
  Re: Find Minimum for element in multiple dimensional array Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-24 00:18 +0000
  Re: Find Minimum for element in multiple dimensional array Robert Davis <rdavis7408@gmail.com> - 2015-07-24 06:17 -0700

csiph-web