Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'csv': 0.09; 'dict': 0.09; 'ignoring': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'assume': 0.11; 'combinations': 0.16; 'dict(': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:array': 0.16; 'wrote:': 0.16; 'transform': 0.18; 'arrays': 0.22; 'file.': 0.22; 'code,': 0.23; 'code.': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'entries': 0.27; 'skip:( 20': 0.28; 'looks': 0.29; 'origin': 0.29; 'array': 0.29; 'convert': 0.29; 'print': 0.30; 'point': 0.33; 'file': 0.34; 'list': 0.34; 'skip:- 50': 0.35; 'filter': 0.35; 'robert': 0.35; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'minimum': 0.38; 'represent': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'office': 0.62; 'distance': 0.63; 'between': 0.65; 'results': 0.66; 'received:12': 0.81; 'miles.': 0.84; 'jersey': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Emile van Sebille Subject: Re: Find Minimum for element in multiple dimensional array Date: Wed, 22 Jul 2015 16:26:58 -0700 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: www.westernstatesglass.com User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437607638 news.xs4all.nl 2834 [2001:888:2000:d::a6]:57388 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4811 X-Received-Body-CRC: 1349265336 Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:94409 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