Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Lowest Value in List

Reply
Thread Tools

Lowest Value in List

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      10-02-2013
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.

Thanking in Advance,
Subhabrata.


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-02-2013
On Wed, 02 Oct 2013 03:04:16 -0700, subhabangalore wrote:

> 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.


If they have to be the lowest *unique* values, the easiest way is to
build a set from all three lists, then sort, and take a slice of only the
first 10:

sorted(set(alist + blist + clist))[:10]

If you don't want unique values, but want to keep duplicates, then drop
the call to set:

sorted(alist + blist + clist)[:10]



> The Solution:
>
> I tried to address the issue in the following ways:


Thank you for posting your attempts to solve this problem! You had the
right idea, you just did a little bit too much work.



--
Steven
 
Reply With Quote
 
 
 
 
Antoon Pardon
Guest
Posts: n/a
 
      10-02-2013
Op 02-10-13 12:04, http://www.velocityreviews.com/forums/(E-Mail Removed) 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()
 
Reply With Quote
 
dvghana@gmail.com
Guest
Posts: n/a
 
      10-02-2013
On Wednesday, October 2, 2013 10:04:16 AM UTC, (E-Mail Removed) wrote:
> 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.
>
>
>
> Thanking in Advance,
>
> Subhabrata.


PS: I'm learning python (or any programming language) for the first time so I'm pretty sure you don't have to take my word for it but this is what I've got:

list1 = [1,2,3,4,5,6,7,8,9,10]
list2 = [1,2,5,8,9,10,12,15,16,17]
list3 = [-1,-2,-3,8,20,30,40,50,60,17]


def smallestTen(a,b,c):
ultimatelist = a + b + c
for i in ultimatelist:
return sorted(set(ultimatelist))[:10]

print (smallestTen(list1, list2, list3))

 
Reply With Quote
 
Ravi Sahni
Guest
Posts: n/a
 
      10-02-2013
On Wed, Oct 2, 2013 at 3:34 PM, <(E-Mail Removed)> wrote:
> 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.
>
> Thanking in Advance,
> Subhabrata.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list


[Disclaimer: Beginner myself]

The heapq module has merge
Since the lists are already sorted what's wrong with just this?

list(merge(list1, list2, list3))[:10]



--
- Ravi
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-03-2013
(E-Mail Removed) wrote:

> 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.


A bit late to the show here's my take. You could separate your problem into
three simpler ones:

(1) combine multiple sequences into one big sequence
(2) filter out duplicate items
(3) find the largest items

(1) is covered by the stdlib:

items = itertools.chain.from_iterable([list1, list2, list3])

(2) is easy assuming the items are hashable:

def unique(items):
seen = set()
for item in items:
if item not in seen:
seen.add(item)
yield item

items = unique(items)

(3) is also covered by the stdlib:

largest = heapq.nlargest(3, items)

This approach has one disadvantage: the `seen` set in unique() may grow
indefinitely if the sequence passed to it is "long" and has an unlimited
number of distinct duplicates.

So here's an alternative using a heap and a set both limited by the length
of the result:

import heapq

def unique_nlargest(n, items):
items = iter(items)
heap = []
seen = set()
for item in items:
if item not in seen:
seen.add(item)
heapq.heappush(heap, item)
if len(heap) > n:
max_discard = heapq.heappop(heap)
seen.remove(max_discard)
break
for item in items:
if item > max_discard and item not in seen:
max_discard = heapq.heappushpop(heap, item)
seen.remove(max_discard)
return heap


if __name__ == "__main__":
print(unique_nlargest(3, [1,2,3,4,5,4,3,2,1,6,2,7]))

I did not test it, so there may be bugs, but the idea behind the code is
simple: you can remove from the set all items that are below the minimum
item in the heap. Thus both lengths can never grow beyond n (or n+1 in my
actual implementation).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesser value from list of values? Lowest, Least? wildman@noclient.net ASP .Net 3 02-21-2008 03:35 PM
Finding lowest value in dictionary of objects, how? davenet Python 7 11-19-2007 05:03 PM
ListControl SelectedItem gets the lowest index with same value pro Jay ASP .Net Web Controls 0 09-26-2005 02:14 PM
Lowest array value given index Jim Perl Misc 17 10-21-2004 02:03 PM
New Zealand, on the other hand, has one of the lowest growth rates and one of the lowest levels of broadband penetration in the world. punlic NZ Computing 6 05-02-2004 10:28 AM



Advertisments