Re: program python

2021-03-05 Thread alberto
Il giorno giovedì 4 marzo 2021 alle 23:51:39 UTC+1 Igor Korot ha scritto:
> Hi,
> On Thu, Mar 4, 2021 at 4:42 PM alberto  wrote: 
> > 
> > Il giorno giovedì 4 marzo 2021 alle 22:04:57 UTC+1 Paul Bryan ha scritto: 
> > > I don't see a Python program in that link. 
> > > 
> > > Are you asking how to extract data from a CSV? 
> > > A good start will be to look into the csv.reader function and 
> > > csv.DictReader class. 
> > > 
> > > Paul 
> > > On Thu, 2021-03-04 at 12:36 -0800, alberto wrote: 
> > > > Hi I'm tring to write a program with python to evaluate data of csv 
> > > > data 
> > > > In particular I would extract this information 
> > > > 
> > > > View data on the presence of men and women in Affori over time. 
> > > > 
> > > > * Carry out an analysis relating to the last available year. Of the 
> > > > 10 most populous neighborhoods show: 
> > > > * the proportion of births out of the total 
> > > > * the proportion of 80+ to the total 
> > > > * The ratio of minors / number of kindergartens 
> > > > 
> > > > this is the file 
> > > > https://drive.google.com/file/d/1zKflvSpB-oDAqYscLsEgUhSnqL1XPdql/view?usp=sharing
> > > >  
> > > > 
> > > > How could fix it? 
> > > > 
> > > > regards 
> > 
> > Hi, 
> > with this code 
> > import pandas as pd 
> > df = pd.read_csv('data.csv',usecols=['Uomini','Donne']) 
> > 
> > print(df) 
> > I extract two columns, but I would see 'Affori over time'
> And if you add this column to the list - what happens?
> Thank you. 
> 
> > 
> > regards 
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list

Hi, 
with pandas I obtain results!
Now I would calculate ratio beetween columns of my csv

import pandas as pd
pd.set_option('display.max_rows', 500)
df = pd.read_csv ('data_quartiere_2018.csv', usecols= 
['Anno','Totale','Quartiere','Nati','80 e più soli','80 e 
più','Minori','Scuola_infanzia (numero)'])
test = df.loc[df['Totale'] >= 31990, 
print (test)

in particular I would calculate ratio beetween ('Nati'*100)/'Totale' when 
['Totale'] >= 31990

How could fix it
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with printing statement when condition is false

2021-03-05 Thread Terry Reedy

On 3/4/2021 4:28 PM, Terry Reedy wrote:

Quentin privately sent me 12 lines (which should have been posted here 
instead), which can be reduced to the following 4 that exhibit his bug.


if a == b:
     print('correct')
     if a != b:
     print('incorrect')

The bug is a != b will never be true when a == b and will not be tested 
when a != b.  The fix is to use else.


if a == b:
     print('correct')
else:
     print('incorrect')

This should not be reduced to a conditional expression since different 
code follows the print statements in the actual example.


Quentin wrote me privately that this, applied to his code, solved his 
problem.



--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list


Re: Why assert is not a function?

2021-03-05 Thread Cameron Simpson
On 03Mar2021 16:50, Grant Edwards  wrote:
>On 2021-03-03, Chris Angelico  wrote:
>> On Thu, Mar 4, 2021 at 1:40 AM Grant Edwards  
>> wrote:
>>
>>> I thought the entire point of asser being a keyword was so that if you
>>> disable asserts then they go away completely: the arguments aren't
>>> even evaluated.
>>
>> It depends on what the point of "removing the assertions" is, but
>> yes, that will indeed still evaluate the arguments. IMO the cost of
>> running assertions isn't that high compared to the value of keeping
>> them (which is why I never run -O), and the performance argument is
>> a weak one compared to the much stronger value of having the actual
>> failing expression available in the exception report.
>
>Good point. I had forgotten about having the expression available in
>the exception output. That's definitly valuable.

Did we all see the recently announced ycecream PyPI module? Very cool!

See: https://github.com/salabim/ycecream

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


trouble using pygame

2021-03-05 Thread Quentin Bock
So, I recently downloaded the pygame module, this code is from a youtube
tutorial by the way, I am following along the tutorial, the error involves
the pygame.init() and also says this:

import pygame

# Initialize Pygame
pygame.init()

#create the screen
screen = pygame.display.set_mode((800, 600))

# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False

The error says "partially initialized module 'pygame' has no attribute
'init' (most likely due to a circular import)

If someone could also explain what a circular import is and how to
avoid them within the future that would be great

Thanks :)

Here is a link to the tutorial  as well just in case someone wants to see it:

https://www.youtube.com/watch?v=FfWpgLFMI7w
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: trouble using pygame

2021-03-05 Thread MRAB

On 2021-03-06 02:54, Quentin Bock wrote:

So, I recently downloaded the pygame module, this code is from a youtube
tutorial by the way, I am following along the tutorial, the error involves
the pygame.init() and also says this:

import pygame

# Initialize Pygame
pygame.init()

#create the screen
screen = pygame.display.set_mode((800, 600))

# Game Loop
running = True
while running:
 for event in pygame.event.get():
 if event.type == pygame.quit():
 running = False

The error says "partially initialized module 'pygame' has no attribute
'init' (most likely due to a circular import)

If someone could also explain what a circular import is and how to
avoid them within the future that would be great

Thanks :)

Here is a link to the tutorial  as well just in case someone wants to see it:

https://www.youtube.com/watch?v=FfWpgLFMI7w

A circular import is when a module tries to import itself directly or 
indirectly. (A imports A; A imports B, which imports A; A imports B, 
which imports C, which imports A; you get the idea!)


What did you call your script? Did you perhaps call it "pygame"?
--
https://mail.python.org/mailman/listinfo/python-list


Question about generators

2021-03-05 Thread Frank Millman

Hi all

This is purely academic, but I would like to understand the following -

>>>
>>> a = [('x', 'y')]
>>>
>>> s = []
>>> for b, c in a:
...   s.append((b, c))
...
>>> s
[('x', 'y')]


This is what I expected.

>>>
>>> s = []
>>> s.append(((b, c) for b, c in a))
>>> s
[ at 0x019FC3F863C0>]
>>>

I expected the same as the first one.

I understand the concept that a generator does not return a value until 
you call next() on it, but I have not grasped the essential difference 
between the above two constructions.


TIA for any insights.

Frank Millman

--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about generators

2021-03-05 Thread Alan Bawden
   >>>
   >>> s = []
   >>> s.append(((b, c) for b, c in a))
   >>> s
   [ at 0x019FC3F863C0>]
   >>>

   TIA for any insights.

Replace "append" above with "extend" and observe the results.  Then
ponder the difference between append and extend.  I suspect that the
heart of your confusion actually has nothing to do with generators.

-- 
Alan Bawden
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about generators

2021-03-05 Thread Ming
On Sat, Mar 06, 2021 at 08:21:47AM +0200, Frank Millman wrote:
> [...]
> I understand the concept that a generator does not return a value until you
> call next() on it, but I have not grasped the essential difference between
> the above two constructions.
> 
> TIA for any insights.
> 
> Frank Millman

In your first example, a certain element is appended to s each time in
the loop. But in your second example, the generator first traverses all
the elements, and then generates an iterable object, you are trying to
append this object to s.

Let's look at a simpler but more certain example:

a = [1, 2, 3]
s = []
for n in a:
s.append(n)
# Result: 
# len(s) == 3
# s == [1, 2, 3]

a = [1, 2, 3]
s = []
g = (n for n in a)
s.append(g)
# Result:
# len(s) == 1
# list(s[0]) == [1, 2, 3]

If you want to get the same result in the above two cases, just replace
append with extend.
list.extend() can accept an iterable object as input, and then append all
the elements to the original list.

-- 
OpenPGP fingerprint: 3C47 5977 4819 267E DD64  C7E4 6332 5675 A739 C74E


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about generators

2021-03-05 Thread Frank Millman

On 2021-03-06 8:21 AM, Frank Millman wrote:

Hi all

This is purely academic, but I would like to understand the following -

 >>>
 >>> a = [('x', 'y')]
 >>>
 >>> s = []
 >>> for b, c in a:
...   s.append((b, c))
...
 >>> s
[('x', 'y')]


This is what I expected.

 >>>
 >>> s = []
 >>> s.append(((b, c) for b, c in a))
 >>> s
[ at 0x019FC3F863C0>]
 >>>

I expected the same as the first one.

I understand the concept that a generator does not return a value until 
you call next() on it, but I have not grasped the essential difference 
between the above two constructions.





Thanks, Alan and Ming.

I think I have got it now.

In my first example, a 'for' loop both creates an iterable object *and* 
iterates over it.


In my second example, a generator expression creates an iterable object, 
but does nothing with it until asked.


Changing 'append' to 'extend' has the effect of iterating over the 
generator expression and appending the results.


Frank

--
https://mail.python.org/mailman/listinfo/python-list