[Tutor] R: Tutor Digest, Vol 125, Issue 71

2014-07-23 Thread jarod...@libero.it
Thanks so much!!

>Messaggio originale
>Da: tutor-requ...@python.org
>Data: 23/07/2014 8.10
>A: 
>Ogg: Tutor Digest, Vol 125, Issue 71
>
>Send Tutor mailing list submissions to
>   tutor@python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>   https://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>   tutor-requ...@python.org
>
>You can reach the person managing the list at
>   tutor-ow...@python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. How to show dictionary item non present on file
>  (jarod...@libero.it)
>   2. Re: How to show dictionary item non present on file
>  (Steven D'Aprano)
>   3. Re: How to show dictionary item non present on file (Peter Otten)
>   4. Getting a directory listing with Python to MySQL (Eric Dannewitz)
>   5. Re: Getting a directory listing with Python to MySQL (Danny Yoo)
>   6. Re: Getting a directory listing with Python to MySQL
>  (Steven D'Aprano)
>   7. Re: Getting a directory listing with Python to MySQL
>  (Eric Dannewitz)
>
>
>--
>
>Message: 1
>Date: Tue, 22 Jul 2014 13:10:18 +0200 (CEST)
>From: "jarod...@libero.it" 
>To: tutor@python.org
>Subject: [Tutor] How to show dictionary item non present on file
>Message-ID:
>   <253805423.250161406027418296.JavaMail.defaultUser@defaultHost>
>Content-Type: text/plain;charset="UTF-8"
>
>Hin there!!!
>
>I have a niave question on dictionary analysis: 
>If you have a dictionary like this:
>diz
>Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1}
>
>and you have a list and you want to know which  keys are not present on my 
>dictionary the code are simple.
>for i in diz.keys():
>   ...: if i in mitico:
>   ...: print "NO"
>   ...: else:
>   ...: print i
>   ...: 
>NO
>
>But I havethis problem I have a file and I want to know which elements are 
not 
>present on my file from dictionary.
> more data.tmp 
>jack   1
>pippo  1
>luis   1
>frate  1
>livio  1
>frank  1
>
>
>with open("data.tmp") as p:
>for i in p:
>lines= i.strip("\n").split("\t")
>if not diz.has_key(lines[0]):
>   : print i
>   : 
>pippo  1
>
>luis   1
>
>frate  1
>
>livio  1
>
>The output I want is to have :
>ralph and 'elenour.. how can I do this?
>thanks in advance!
>
>
>
>--
>
>Message: 2
>Date: Tue, 22 Jul 2014 21:32:47 +1000
>From: Steven D'Aprano 
>To: tutor@python.org
>Subject: Re: [Tutor] How to show dictionary item non present on file
>Message-ID: <20140722113247.GO9112@ando>
>Content-Type: text/plain; charset=us-ascii
>
>On Tue, Jul 22, 2014 at 01:10:18PM +0200, jarod...@libero.it wrote:
>
>> But I havethis problem I have a file and I want to know which elements are 
not 
>> present on my file from dictionary.
>>  more data.tmp 
>> jack 1
>> pippo1
>> luis 1
>> frate1
>> livio1
>> frank1
>> 
>> 
>> with open("data.tmp") as p:
>> for i in p:
>> lines= i.strip("\n").split("\t")
>> if not diz.has_key(lines[0]):
>>: print i
>>: 
>> pippo1
>> luis 1
>> frate1
>> livio1
>> 
>> The output I want is to have :
>> ralph and 'elenour.. how can I do this?
>
>You are doing the comparison the wrong way: you are saying:
>
>for each line in the file:
>is the line in the dict?
>if no, print the line
>
>
>What you want is:
>
>for each key in the dict:
>is the key in the file?
>if no, print the key
>
>
>It is not easy to try searching the file directly, so we copy the lines 
>from the file into a set:
>
>lines = set()
>with open("data.tmp") as the_file:
>for line in the_file:
>line = line.strip().split("\t")[0]
>lines.add(line)
>
>
>Here is a shorter way to do the same thing:
>
>with open("data.tmp") as the_file:
>lines = set([line.strip().split("\t")[0] for line in the_file])
>
>
>Now you can walk through the dict:
>
>for name in diz:
>if name not in lines:
>print(name) 
>
>
>Or, if you prefer:
>
>names = set(diz)  # copy the keys from the dict into a set
>print(names.difference(lines))
>
>
>If you want to see the other way around:
>
>print(lines.difference(names))
>
>
>
>
>-- 
>Steven
>
>
>--
>
>Message: 3
>Date: Tue, 22 Jul 2014 13:48:09 +0200
>From: Peter Otten <__pete...@web.de>
>To: tutor@python.org
>Subject: Re: [Tutor] How to show dictionary item non present on file
>Message-ID: 
>Content-Type: text/plain; charset="ISO-8859-1"
>
>jarod...@libero.it wrote:
>
>> Hin there!!!
>> 
>> I have a niave question on dictionary analysis:
>> If you have a dictionary like this:
>> diz
>> Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1}
>> 
>> and you have a list and you want to know whi

Re: [Tutor] (no subject)

2014-07-23 Thread Wolfgang Maier

On 07/23/2014 03:36 AM, LN A-go-go wrote:

Hi again and thank-you Wolfgang and company.  I wish I could give you
snickers bars or something!  I was able to get through the road_blocks
with your help.  I have been working the last few days, I am sorry to
say, unsuccessfully, to calculate the mean (that's easy), split the data
into sub-groups or secondary means - which are the break values between
4 classes.  Create data-sets with incursive values.  I can do it with
brute force (copy and paste) but need to rise to the pythonic way and
use a while loop and a nested if-else structure.  My attempts have been
lame enough that I don't even want to put them here.


Before addressing your question, I guess it's time that you start 
following the etiquette on this list:

1) start a new thread when you ask a new question
2) give the thread a telling subject line
3) show your attempts (even if you think they are lame)
4) show only the relevant part of your code (makes it easier for 
evereybody to read your post, but often sitting down and writing a 
streamlined version of what you have done before posting also helps you 
to understand what the actual problem is)


Finally and as suggested by others, it is really time that you start 
writing programs instead of just typing individual commands in the 
interactive interpreter. Use IDLE or any text editor for that.


I'll be following point 1 and 2 from the list above and answer your 
question in a separate thread.


Best,
Wolfgang

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


[Tutor] binning data and calculating means of classes

2014-07-23 Thread Wolfgang Maier

On 07/23/2014 03:36 AM, LN A-go-go wrote:
>
> with your help.  I have been working the last few days, I am sorry to
> say, unsuccessfully, to calculate the mean (that's easy), split the data
> into sub-groups or secondary means - which are the break values between
> 4 classes.  Create data-sets with incursive values.  I can do it with
> brute force (copy and paste) but need to rise to the pythonic way and
> use a while loop and a nested if-else structure.  My attempts have been
> lame enough that I don't even want to put them here.

A while loop with an if inside is indeed a very plausible solution, so 
it would be interesting to see your attempts.


> int_list
> [36, 39, 39, 45, 61, 54, 61, 93, 62, 51, 47, 72, 54, 36, 62, 50, 41, 41,
> 40, 62, 62, 58, 57, 54, 49, 43, 47, 50, 45, 41, 54, 57, 57, 55, 62, 51,
> 34, 57, 55, 63, 45, 45, 42, 44, 34, 53, 67, 58, 56, 43, 33]
 int_list.sort()
 int_list
> [33, 34, 34, 36, 36, 39, 39, 40, 41, 41, 41, 42, 43, 43, 44, 45, 45, 45,
> 45, 47, 47, 49, 50, 50, 51, 51, 53, 54, 54, 54, 54, 55, 55, 56, 57, 57,
> 57, 57, 58, 58, 61, 61, 62, 62, 62, 62, 62, 63, 67, 72, 93]
 flo_list = [float(integral) for integral in int_list]

While this last line shows that you've started using list 
comprehensions, which is a good thing, converting your data to floating 
point is not a good idea. It is completely unnecessary and (though 
probably not relevant here) can compromise the accuracy of calculations 
due to inherent rounding errors.
I guess you are doing this to prevent subsequent rounding of the result 
of sum(int_list)/len(int_list).
This is a Python2-specific issue and, personally, I think that as a 
beginner you should use Python3, where (among other things) this is not 
a problem.

If you want to stick to Python2 for whatever reason then do:

from __future__ import division

after which integer divisions return a float if required just as in Python3.

>>> sum(int_list)/len(int_list)
51.31372549019608

 flo_list
> [33.0, 34.0, 34.0, 36.0, 36.0, 39.0, 39.0, 40.0, 41.0, 41.0, 41.0, 42.0,
> 43.0, 43.0, 44.0, 45.0, 45.0, 45.0, 45.0, 47.0, 47.0, 49.0, 50.0, 50.0,
> 51.0, 51.0, 53.0, 54.0, 54.0, 54.0, 54.0, 55.0, 55.0, 56.0, 57.0, 57.0,
> 57.0, 57.0, 58.0, 58.0, 61.0, 61.0, 62.0, 62.0, 62.0, 62.0, 62.0, 63.0,
> 67.0, 72.0, 93.0]
 sum(flo_list)
> 2617.0
  totalnum = sum(flo_list)

stop generating references if you're not going to use them later!
Confuses you and others.

 len(flo_list)
> 51
 mean = sum(flo_list)/len(flo_list)
 mean
> 51.31372549019608

So, you know how to calculate the total mean. For the means of 
subsamples what you have to do is to apply that same logic to subsamples 
of the data, which you have to generate.
Without going through the lists of values several times, however, I 
cannot think of any simple implementation of this, which does not 
involve plenty of novel concepts.
One fairly simple approach would be through a while loop as you 
suggested, but as said before, for loops are often more elegant in 
Python. I guess the following code is roughly what you had in mind ?


breakpoints = [your_list_of breakpoints]
large_value_buffer = []
int_list_iter = iter(int_list) # see comment below
for breakpoint in breakpoints:
sublist = large_value_buffer
for value in int_list_iter:
if value < breakpoint:
sublist.append(value)
if large_value_buffer:
large_value_buffer = []
else:
if sublist:
print(sum(sublist)/len(sublist))
large_value_buffer.append(value)
break

Essentially, you should know all elements of this small program except 
the iter(int_list). Essentially, this gives you a one-time iterator, 
which cannot be reused or reset, to use in the inner for loop. This 
prevents starting from the beginning of the list every time.


Since this is probably too complicated for you to work it out by 
yourself at this stage, I decided to give you the complete code, but 
make sure you understand what it does, especially think about what the 
large_value_buffer is doing.


One problem with this code is that it silently skips empty bins. Maybe 
that's something for you to work on ?


Best,
Wolfgang

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


Re: [Tutor] binning data and calculating means of classes

2014-07-23 Thread Wolfgang Maier

On 07/23/2014 11:28 AM, Wolfgang Maier wrote:


breakpoints = [your_list_of breakpoints]
large_value_buffer = []
int_list_iter = iter(int_list) # see comment below
for breakpoint in breakpoints:
 sublist = large_value_buffer
 for value in int_list_iter:
 if value < breakpoint:
 sublist.append(value)
 if large_value_buffer:
 large_value_buffer = []
 else:
 if sublist:
 print(sum(sublist)/len(sublist))
 large_value_buffer.append(value)


oops, the last line above needs outdenting by one level.


 break




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


Re: [Tutor] Getting a directory listing with Python to MySQL

2014-07-23 Thread Danny Yoo
On Jul 22, 2014 11:10 PM, "Eric Dannewitz"  wrote:
>
> That's close. I have been playing from glob and os.walk but I'm at a loss
how to get the size, creation and modified date while running it.

Steven mentioned a few functions in his reply; check those ones out.  The
'os' module is a bit large, but I'm pretty sure what you're looking for is
there.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using module Facebook

2014-07-23 Thread Chris
On 07/20/2014 09:48 PM, Albert-Jan Roskam wrote:
> Hi, just wondering: why are you cloning the repo? You pip install
> from Pypi.  You can also pip install from the repo.

I didn't know that this is possible directly.

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