Re: [Tutor] Rearranging a list of numbers with corresponding index (RESOLVED)

2015-03-13 Thread Matthew Ruffalo
On 03/13/2015 11:55 AM, Ken G. wrote:
> I will be studying this also. I am still using Python 2.7.6 as that is
> the latest as provided by Ubuntu 14.04.1.

FYI, Python 3.4 is installed by default in Ubuntu 14.04(.1). You have to
invoke it as 'python3' though.

MMR...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rearranging a list of numbers with corresponding index (RESOLVED)

2015-03-13 Thread Ken G.



On 03/13/2015 10:38 AM, Dave Angel wrote:

On 03/13/2015 09:57 AM, Ken G. wrote:

I have been keeping track of numbers drawn in our local lotto drawings
into a list format as shown in a short example below. Using such list, I
am able to determine how often a number appears within the last 100 plus
drawings.

The length of my lists range from 5, 15, 35, 59and 75 long. I will give
an example of one of my short list.

PowerPlay = [0, 0, 61, 32, 11, 14]

Disregarding index 0 and 1, I can see and print the following:

IndexNumber
2 61
3 32
4 11
5 14

showing that number 2 appears 61 times, number 3 appears 32 times,
number 4 appears 11 times and number 5 appears 14 times within last 100
plus drawings.

How I best rearrange the numbers from high to low with its corresponding
index number such as below:

Number Index
612
323
14 5
11 4

showing the number 2 appears 61 times, number 3 appears 32 times, number
5 appears 14 times and number 4 appears 11 times. I know that using

MegaBall.reverse()

sort the number from high to low but the index numbers still remain in
the same positions.

Thanks for pointing out the way to do this.



Make a list of tuples by doing something like:
new_list = [ (cnt, index) for index, cnt in enumerate(power_play) ]

to produce:
  [ (0, 0), (0, 1), (61, 2), (32, 3), (11, 4), (14, 5) ]

then sort it with reverse=True.  When you print it, just omit the 
items that have zero as their count.  (they'll be at the end, anyway)


Lots of other variants, some more concise.  And other approaches might 
be cleaner if we change how we generate the list.


(I tried it briefly using Python 3.4)


I will be studying this also. I am still using Python 2.7.6 as that is 
the latest as provided by Ubuntu 14.04.1. With thanks to all, I was able 
to resolve my request for assistance here in this forum. Thanks Peter.


Ken

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rearranging a list of numbers with corresponding index

2015-03-13 Thread Ken G.



On 03/13/2015 10:21 AM, Peter Otten wrote:

Ken G. wrote:


I have been keeping track of numbers drawn in our local lotto drawings
into a list format as shown in a short example below. Using such list, I
am able to determine how often a number appears within the last 100 plus
drawings.

The length of my lists range from 5, 15, 35, 59and 75 long. I will give
an example of one of my short list.

PowerPlay = [0, 0, 61, 32, 11, 14]

Disregarding index 0 and 1, I can see and print the following:

IndexNumber
2 61
3 32
4 11
5 14

showing that number 2 appears 61 times, number 3 appears 32 times,
number 4 appears 11 times and number 5 appears 14 times within last 100
plus drawings.

How I best rearrange the numbers from high to low with its corresponding
index number such as below:

Number Index
612
323
14 5
11 4

showing the number 2 appears 61 times, number 3 appears 32 times, number
5 appears 14 times and number 4 appears 11 times. I know that using

MegaBall.reverse()

sort the number from high to low but the index numbers still remain in
the same positions.

Thanks for pointing out the way to do this.

The initial list:


power_play = [0, 0, 61, 32, 11, 14]

Use enumerate() to get index, frequency pairs:


list(enumerate(power_play))

[(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)]

Sort the pairs:


sorted(enumerate(power_play))

[(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)]

Provide a key to sort after the second value, i. e. the frequency in the
(index, frequency) pairs:


def second_value(item):

... return item[1]
...

sorted(enumerate(power_play), key=second_value)

[(0, 0), (1, 0), (4, 11), (5, 14), (3, 32), (2, 61)]

Note that instead of writing your own custom function or lambda you could
also use operator.itemgetter(1) from the operator module in the stdlib.

Sort in reverse order:


sorted(enumerate(power_play), key=second_value, reverse=True)

[(2, 61), (3, 32), (5, 14), (4, 11), (0, 0), (1, 0)]


Print the output for frequencies > 0:


pairs = sorted(enumerate(power_play), key=second_value, reverse=True)
for index, freq in pairs:

... if freq == 0:
... break
... print(freq, index, sep="\t")
...
61  2
32  3
14  5
11  4


Wow! Thanks! Printing this out and will be studying it completely. 
Again, thanks.


Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rearranging a list of numbers with corresponding index

2015-03-13 Thread Dave Angel

On 03/13/2015 09:57 AM, Ken G. wrote:

I have been keeping track of numbers drawn in our local lotto drawings
into a list format as shown in a short example below. Using such list, I
am able to determine how often a number appears within the last 100 plus
drawings.

The length of my lists range from 5, 15, 35, 59and 75 long. I will give
an example of one of my short list.

PowerPlay = [0, 0, 61, 32, 11, 14]

Disregarding index 0 and 1, I can see and print the following:

IndexNumber
2 61
3 32
4 11
5 14

showing that number 2 appears 61 times, number 3 appears 32 times,
number 4 appears 11 times and number 5 appears 14 times within last 100
plus drawings.

How I best rearrange the numbers from high to low with its corresponding
index number such as below:

Number Index
612
323
14 5
11 4

showing the number 2 appears 61 times, number 3 appears 32 times, number
5 appears 14 times and number 4 appears 11 times. I know that using

MegaBall.reverse()

sort the number from high to low but the index numbers still remain in
the same positions.

Thanks for pointing out the way to do this.



Make a list of tuples by doing something like:
new_list = [ (cnt, index) for index, cnt in enumerate(power_play) ]

to produce:
  [ (0, 0), (0, 1), (61, 2), (32, 3), (11, 4), (14, 5) ]

then sort it with reverse=True.  When you print it, just omit the items 
that have zero as their count.  (they'll be at the end, anyway)


Lots of other variants, some more concise.  And other approaches might 
be cleaner if we change how we generate the list.


(I tried it briefly using Python 3.4)


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rearranging a list of numbers with corresponding index

2015-03-13 Thread Peter Otten
Ken G. wrote:

> I have been keeping track of numbers drawn in our local lotto drawings
> into a list format as shown in a short example below. Using such list, I
> am able to determine how often a number appears within the last 100 plus
> drawings.
> 
> The length of my lists range from 5, 15, 35, 59and 75 long. I will give
> an example of one of my short list.
> 
> PowerPlay = [0, 0, 61, 32, 11, 14]
> 
> Disregarding index 0 and 1, I can see and print the following:
> 
> IndexNumber
> 2 61
> 3 32
> 4 11
> 5 14
> 
> showing that number 2 appears 61 times, number 3 appears 32 times,
> number 4 appears 11 times and number 5 appears 14 times within last 100
> plus drawings.
> 
> How I best rearrange the numbers from high to low with its corresponding
> index number such as below:
> 
> Number Index
> 612
> 323
> 14 5
> 11 4
> 
> showing the number 2 appears 61 times, number 3 appears 32 times, number
> 5 appears 14 times and number 4 appears 11 times. I know that using
> 
> MegaBall.reverse()
> 
> sort the number from high to low but the index numbers still remain in
> the same positions.
> 
> Thanks for pointing out the way to do this.

The initial list:

>>> power_play = [0, 0, 61, 32, 11, 14]

Use enumerate() to get index, frequency pairs:

>>> list(enumerate(power_play))
[(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)]

Sort the pairs:

>>> sorted(enumerate(power_play))
[(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)]

Provide a key to sort after the second value, i. e. the frequency in the 
(index, frequency) pairs:

>>> def second_value(item):
... return item[1]
... 
>>> sorted(enumerate(power_play), key=second_value)
[(0, 0), (1, 0), (4, 11), (5, 14), (3, 32), (2, 61)]

Note that instead of writing your own custom function or lambda you could 
also use operator.itemgetter(1) from the operator module in the stdlib.

Sort in reverse order:

>>> sorted(enumerate(power_play), key=second_value, reverse=True)
[(2, 61), (3, 32), (5, 14), (4, 11), (0, 0), (1, 0)]


Print the output for frequencies > 0:

>>> pairs = sorted(enumerate(power_play), key=second_value, reverse=True)
>>> for index, freq in pairs:
... if freq == 0:
... break
... print(freq, index, sep="\t")
... 
61  2
32  3
14  5
11  4


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor