Re: [Tutor] please help

2014-03-22 Thread Cameron Simpson
On 22Mar2014 00:04, Mark Lawrence  wrote:
> On 21/03/2014 21:39, Cameron Simpson wrote:
> >On 21Mar2014 20:31, Mustafa Musameh  wrote:
> >
> >I would collect the statistics using a dictionary to keep count of
> >the characters. See the dict.setdefault method; it should be helpful.
> >
> 
> Delightfully old fashioned but I'd now prefer
> http://docs.python.org/3/library/collections.html#collections.Counter
> :)

Maybe, but using a dictionary is easy and Mustafa will learn more.

Cheers,
-- 
Cameron Simpson 

Do not underestimate your abilities.  That is your boss's job.
It is your job to find ways around your boss's roadblocks.
- Glen Appleby  http://www.armory.com/~glena/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2014-03-22 Thread spir

On 03/21/2014 09:57 PM, Jerry Hill wrote:

On Fri, Mar 21, 2014 at 3:25 PM, Gary Engstrom  wrote:

I am trying to understand function fibc code line a,b = b, a + b and would
like to see it written line by line
without combining multiply assignment. If possible. I sort of follow the
right to left evaluation of the other code.


Sure.  a,b = b, a+b is equivalent to:

new_a = b
new_b = a + b
a = new_a
b = new_b
del new_a
del new_b

That is, we first evaluate everything on the right hand side of the
equals sign, then assign those values to a and b.  Then we get rid of
the temporary variables, since the original statement didn't leave any
temporary variable floating around.


In other words, it is like
a = b ; b = a+b
performed *in parallel*. Another way to write it is:
old_a = a
a = b
b = old_a + b

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


Re: [Tutor] Understanding code line

2014-03-22 Thread spir

On 03/21/2014 06:14 PM, Gary wrote:


Pythonists

I am trying to understand the difference between

a = b
b = a + b
  and

a,b = b, a+ b
When used in my Fibonacci code the former generates 0,1,2,4,8,16,32 and the 
later
Generates 0,1,1,2,3,5,8,13,21,34,55,89.  The second is the sequence I want, but 
I would
Like to understand the second code sequence better so I can write the code in R 
and Scilab as well as python.


To understand
a, b = b, a+ b
correctly, think of it operating in parallel, each assignment independantly. So, 
it does what you think (and need to compute a fibonacci sequence).


A better explanation, maybe, is that python has *tuples*, which are groups of 
values held together; and are better written inside parens (), like (1,2,3) or 
(x,y,z). But () are not compulsary, a comma is enough to make a tuple. Here we 
have two 2-tuples, also called pairs, meaning tuples of 2 values. The assignment 
thus actually means:

(a, b) = (b, a+b)
So, python *first* constructs the right side tuple, *then* assigns it to the 
left side; however, since we don't have on the left side a (named) tuple 
variable, it actually assigns to the local variables a & b, in //. Hope it's 
clear. You can consider this last step as nice, little magic. Rarely needed, but 
very handy when needed.


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


Re: [Tutor] Fib sequence code assignment

2014-03-22 Thread spir

On 03/21/2014 10:21 PM, Gary wrote:

Dear Jerry,

Thank you so much, once you see it it seems so clear, but to see it I might as 
well  be in the Indian Ocean. Got kinda close using temporary variable,but 
didn't know enough to use del.
A lesson learn.


You don't need del (here). Every variable is automagically recycled at the end 
of the current scope, meaning usually the end of the function (fib). In 
practice, we so-to-say never need del in python (the common exception being to 
delete an item in a collection).


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


Re: [Tutor] please help

2014-03-22 Thread spir

On 03/21/2014 10:39 PM, Cameron Simpson wrote:

On 21Mar2014 20:31, Mustafa Musameh  wrote:

Please help. I have been search the internet to understand how to write a 
simple program/script with python, and I did not do anything.
I have a file that look like this

ID 1

agtcgtacgt…

ID 2

acccttcc
.
.
.
in other words, it contains several IDs each one has a sequence of 'acgt' 
letters
I need to write a script in python where the output will be, for example, like 
this

ID 1

a = 10%, c = 40%,  g=40%, t = 10%

ID 2

a = 15%, c = 35%,  g=35%, t = 15%
.
.
.
(i mean the first line is the ID and the second line is the frequency of each 
letter )
How I can tell python to print the first line as it is and count characters 
starting from the second line till the beginning of the next '>' and so on


You want a loop that reads lines in pairs. Example:

   while True:
 line1 = fp.readline()
 print line1,
 line2 = fp.readline()
 ... process the line and report ...

Then to process the line, iterate over the line. Because a line is
string, and a string is a sequence of characters, you can write:

   for c in line2:
 ... collect statistics about c ...
   ... print report ...

I would collect the statistics using a dictionary to keep count of
the characters. See the dict.setdefault method; it should be helpful.


I think it would be easier to do that in 2 loops:
* first read the file line by line, building a list of pairs (id, base-sequence)
  (and on the fly check the input is correct, if needed)
* then traverse the sequences of bases to get numbers & percentages, and write 
out

d

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