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

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


[Tutor] Rearranging a list of numbers with corresponding index

2015-03-13 Thread Ken G.
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.

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


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

2015-02-26 Thread Ken G.

I may have mis-stated my intention. I will rewrite
my request for assistance later and resubmit.

Thanks,

Ken

On 02/26/2015 08:04 AM, Peter Otten wrote:

Steven D'Aprano wrote:


Ah wait, the penny drops! Now I understand what you mean!

Glad I'm not the only one ;)

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



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


Re: [Tutor] Rearranging a list

2015-02-26 Thread Peter Otten
Steven D'Aprano wrote:

 Ah wait, the penny drops! Now I understand what you mean!

Glad I'm not the only one ;)

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


Re: [Tutor] Rearranging a list

2015-02-26 Thread Peter Otten
Ken G. wrote:

 Assuming I have the following list and code how do I best be able
 rearrange the list as stated below:
 
 list = [0, 0, 21, 35, 19, 42]
 
 Using print list[2:6] resulted in the following:
 
 221
 335
 419
 542
 
 I would like to rearrange the list as follow:
 
 542
 335
 221
 419
 
 I tried using list.reverse() and print list[0,6] and it resulted in:
 
 [42, 19, 35, 21, 0, 0] or as printed:
 
 042
 119
 235
 321
 40
 50
 
 In another words, I would desire to show that:
 
 5 appears 42 times
 3 appears 35 times
 2 appears 21 times
 4 appears 19 times
 
 but then I lose my original index of the numbers by reversing. How do I
 best keep the original index number to the rearrange numbers within a
 list?

Don't rearrange the original list, make a new one instead:

 frequencies = [0, 0, 21, 35, 19, 42]
 wanted_indices = [5, 3, 2, 4]
 [frequencies[i] for i in wanted_indices]
[42, 35, 21, 19]

Or look up the values as you print them:

 for i in wanted_indices:
... print(i, appears, frequencies[i], times)
... 
5 appears 42 times
3 appears 35 times
2 appears 21 times
4 appears 19 times

The expression [frequencies[i] for i in wanted_indices] is called list 
comprehension and is a shortcut for a loop:

 result = []
 for i in wanted_indices:
... result.append(frequencies[i])
... 
 result
[42, 35, 21, 19]


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


Re: [Tutor] Rearranging a list

2015-02-26 Thread Steven D'Aprano
Ah wait, the penny drops! Now I understand what you mean!


On Thu, Feb 26, 2015 at 07:13:57AM -0500, Ken G. wrote:

 In another words, I would desire to show that:
 
 5 appears 42 times
 3 appears 35 times
 2 appears 21 times
 4 appears 19 times
 
 but then I lose my original index of the numbers by reversing. How do I 
 best keep the original index number to the rearrange numbers within a list?

Here's your list again:

[0, 0, 21, 35, 19, 42]

I think using a list is the wrong answer. I think you should use a 
dictionary:

d = {0: 0, 1: 0, 2: 21, 3: 35, 4: 19, 5: 42}
for key, value in sorted(d.items(), reverse=True):
print(key, value)


which gives this output:

5 42
4 19
3 35
2 21
1 0
0 0


How should you build the dict in the first place? There's an easy way, 
and an even easier way. Here's the easy way.

# count the numbers
numbers = [2, 3, 1, 4, 0, 4, 2, 5, 2, 3, 4, 1, 1, 1, 3, 5]

counts = {}
for n in numbers:
i = counts.get(n, 0)
counts[n] = i + 1

print(counts)




which gives output:

{0: 1, 1: 4, 2: 3, 3: 3, 4: 3, 5: 2}




Here's the even easier way:

from collections import Counter
counts = Counter(numbers)
print(counts)


which gives very similar output:

Counter({1: 4, 2: 3, 3: 3, 4: 3, 5: 2, 0: 1})


In both cases, you then run the earlier code to print the items in 
order:

for key, value in sorted(counts.items(), reverse=True):
print(key, value)



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


Re: [Tutor] Rearranging a list

2015-02-26 Thread Peter Otten
Peter Otten wrote:

 Ken G. wrote:
 
 Assuming I have the following list and code how do I best be able
 rearrange the list as stated below:
 
 list = [0, 0, 21, 35, 19, 42]
 
 Using print list[2:6] resulted in the following:
 
 221
 335
 419
 542
 
 I would like to rearrange the list as follow:
 
 542
 335
 221
 419
 
 I tried using list.reverse() and print list[0,6] and it resulted in:
 
 [42, 19, 35, 21, 0, 0] or as printed:
 
 042
 119
 235
 321
 40
 50
 
 In another words, I would desire to show that:
 
 5 appears 42 times
 3 appears 35 times
 2 appears 21 times
 4 appears 19 times
 
 but then I lose my original index of the numbers by reversing. How do I
 best keep the original index number to the rearrange numbers within a
 list?
 
 Don't rearrange the original list, make a new one instead:
 
 frequencies = [0, 0, 21, 35, 19, 42]
 wanted_indices = [5, 3, 2, 4]
 [frequencies[i] for i in wanted_indices]
 [42, 35, 21, 19]
 
 Or look up the values as you print them:
 
 for i in wanted_indices:
 ... print(i, appears, frequencies[i], times)
 ...
 5 appears 42 times
 3 appears 35 times
 2 appears 21 times
 4 appears 19 times
 
 The expression [frequencies[i] for i in wanted_indices] is called list
 comprehension and is a shortcut for a loop:
 
 result = []
 for i in wanted_indices:
 ... result.append(frequencies[i])
 ...
 result
 [42, 35, 21, 19]

Oops, it occurs to me that you may want the most frequent indices rather 
than specific indices. You can achieve that with

 def lookup_frequency(index):
... return frequencies[index]
... 
 wanted_indices = sorted(range(len(frequencies)), key=lookup_frequency, 
reverse=True)
 wanted_indices
[5, 3, 2, 4, 0, 1]
 wanted_indices[:4]
[5, 3, 2, 4]

You may also want to look at the collections.Counter class:

 import collections
 c = collections.Counter()
 for i, freq in enumerate(frequencies):
... c[i] = freq
... 
 c.most_common(4)
[(5, 42), (3, 35), (2, 21), (4, 19)]
 for key, freq in c.most_common(4):
... print(key, appears, freq, times)
... 
5 appears 42 times
3 appears 35 times
2 appears 21 times
4 appears 19 times


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


Re: [Tutor] Rearranging a list

2015-02-26 Thread Mark Lawrence

On 26/02/2015 16:33, Ken G. wrote:

I may have mis-stated my intention. I will rewrite
my request for assistance later and resubmit.

Thanks,

Ken



When you come back please don't top post, it makes following long 
threads difficult if not impossible, thanks.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


[Tutor] Rearranging a list

2015-02-26 Thread Ken G.
Assuming I have the following list and code how do I best be able 
rearrange the list as stated below:


list = [0, 0, 21, 35, 19, 42]

Using print list[2:6] resulted in the following:

221
335
419
542

I would like to rearrange the list as follow:

542
335
221
419

I tried using list.reverse() and print list[0,6] and it resulted in:

[42, 19, 35, 21, 0, 0] or as printed:

042
119
235
321
40
50

In another words, I would desire to show that:

5 appears 42 times
3 appears 35 times
2 appears 21 times
4 appears 19 times

but then I lose my original index of the numbers by reversing. How do I 
best keep the original index number to the rearrange numbers within a list?


Thanking in advance,

Ken




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