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


Groups > comp.lang.python > #55301

Re: Lowest Value in List

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
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; 'python.': 0.02; 'value,': 0.04; 'received:134': 0.05; 'element': 0.07; 'problem:': 0.07; 'suppose': 0.07; '[0,': 0.09; '[1,': 0.09; 'imported': 0.09; 'thus,': 0.09; '-1,': 0.16; 'ascending': 0.16; 'enough.': 0.16; 'finds': 0.16; 'fine.': 0.16; 'heapq': 0.16; 'tested)': 0.16; 'ways:': 0.16; 'elements': 0.16; 'index': 0.16; 'trying': 0.19; 'import': 0.22; 'portion': 0.22; 'header:User-Agent:1': 0.23; 'merge': 0.24; 'skip:l 30': 0.24; 'helpful': 0.24; 'sort': 0.25; 'order.': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'gives': 0.31; '>>>>': 0.31; 'index,': 0.31; 'lists': 0.32; 'another': 0.32; 'becomes': 0.33; 'problem': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'convert': 0.35; 'add': 0.35; 'like,': 0.36; 'subject:List': 0.36; 'next': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'step': 0.37; 'lists.': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'solve': 0.60; 'took': 0.61; 'kindly': 0.61; 'first': 0.61; 'back': 0.62; 'making': 0.63; 'address': 0.63; 'email addr:gmail.com': 0.63; 'group,': 0.63; 'sum': 0.64; 'more': 0.64; 'taking': 0.65; 'dear': 0.65; 'approaches': 0.68; 'skip:r 30': 0.69; 'lowest': 0.74; 'increase': 0.74; '10:': 0.84; 'subject:Value': 0.84; 'approach.': 0.91
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ap4EABD4S1KGuA9G/2dsb2JhbABYxSaBLoMaAQV4EQshFg8JAwIBAgEPNhMGAgIXh1kDD7MODVeJE4xmgnIWhA0DlCKBdIFphh+GJ4U1gyY
Date Wed, 02 Oct 2013 12:42:06 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: Lowest Value in List
References <dc6edb07-4f5a-4b1a-aa40-b287325196a2@googlegroups.com>
In-Reply-To <dc6edb07-4f5a-4b1a-aa40-b287325196a2@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.608.1380710528.18130.python-list@python.org> (permalink)
Lines 93
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380710528 news.xs4all.nl 15913 [2001:888:2000:d::a6]:50756
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55301

Show key headers only | View raw


Op 02-10-13 12:04, subhabangalore@gmail.com schreef:
> Dear Group,
> 
> I am trying to work out a solution to the following problem in Python. 
> 
> The Problem:
> Suppose I have three lists.
> Each list is having 10 elements in ascending order.
> I have to construct one list having 10 elements which are of the lowest value among these 30 elements present in the three given lists.
> 
> The Solution:
> 
> I tried to address the issue in the following ways:
> 
> a) I took three lists, like,
> list1=[1,2,3,4,5,6,7,8,9,10]
> list2=[0,1,2,3,4,5,6,7,8,9]
> list3=[-5,-4,-3,-2,-1,0,1,2,3,4]
> 
> I tried to make sum and convert them as set to drop the repeating elements:
> set_sum=set(list1+list2+list3)
> set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, -5, -4, -3, -2])
> 
> In the next step I tried to convert it back to list as,
> list_set=list(set_sum)
> gave the value as,
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, -5, -4, -3, -2]
> 
> Now, I imported heapq as, 
> import heapq
> 
> and took the result as,
> result=heapq.nsmallest(10,list_set)
> it gave as,
> [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
> 
> b) I am thinking to work out another approach.
> I am taking the lists again as,
> 
> list1=[1,2,3,4,5,6,7,8,9,10]
> list2=[0,1,2,3,4,5,6,7,8,9]
> list3=[-5,-4,-3,-2,-1,0,1,2,3,4]
> 
> as they are in ascending order, I am trying to take first four/five elements of each list,like,
> 
> list1_4=list1[:4]
>>>> list2_4=list2[:4]
>>>> list3_4=list3[:4]
> 
> Now, I am trying to add them as,
> 
> list11=list1_4+list2_4+list3_4
> 
> thus, giving us the result
> 
> [1, 2, 3, 4, 0, 1, 2, 3, -5, -4, -3, -2]
> 
> Now, we are trying to sort the list of the set of the sum as,
> 
> sort_sum=sorted(list(set(list11)))
> 
> giving us the required result as,
> 
> [-5, -4, -3, -2, 0, 1, 2, 3, 4]
> 
> If by taking the value of each list portion as 4 gives as less number of
> elements in final value, as we are making set to avoid repeating numbers,
> we increase element count by one or two and if final result becomes more
> than 10 we take first ten.
> 
> Are these approaches fine. Or should we think some other way.
> 
> If any learned member of the group can kindly let me know how to solve I would be helpful enough.

You may consider a merge phase from the merge sort. Something like the
following: (Pseudo code; not tested)

iters = [iter(list1), iter(list2), iter(list3)]
heads = [(itr.next() for itr in Iters]

index, value = find_smallest_from(heads) # This function finds the
smalles value and returns it with its index

last = value
result = [value]
heads[index] = iters[index].next()
while len(results) < 10:
    index, value = find_smallest_from(heads)
    if value == last:
        continue
    last = value
    result.append(value)
    heads[index] = iters[index].next()

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


Thread

Lowest Value in List subhabangalore@gmail.com - 2013-10-02 03:04 -0700
  Re: Lowest Value in List Steven D'Aprano <steve@pearwood.info> - 2013-10-02 10:10 +0000
  Re: Lowest Value in List Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-10-02 12:42 +0200
  Re: Lowest Value in List dvghana@gmail.com - 2013-10-02 05:56 -0700
  Re: Lowest Value in List Ravi Sahni <ganeshsahni07@gmail.com> - 2013-10-02 19:35 +0530
  Re: Lowest Value in List Peter Otten <__peter__@web.de> - 2013-10-03 13:12 +0200

csiph-web