Re: [Tutor] Huge list comprehension

2017-06-12 Thread Danny Yoo
On Sun, Jun 11, 2017 at 9:54 PM, syed zaidi  wrote:
> Thanks
> One reason fornsharing the code was that I have to manually create over 100 
> variables


Don't do that.


Anytime you have to manually repeat things over and over is a sign
that you need a loop structure of some sort.


Let's look at a little code.

> with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f:
[code cut ]
> if dictionary.get(sample_name):
> dictionary[sample_name].append(operon)
> else:
> dictionary[sample_name] = []
> dictionary[sample_name].append(operon)
> locals().update(dictionary) ## converts dictionary keys to variables


Ah.  That last statement there is really big warning sign.   Do *not*
use locals().  locals() is almost never a good thing to use.  It's
dangerous and leads to suffering.


Instead, just stick with your dictionary, and use the dictionary.  If
you need a mapping from a sample name to an array of strings,
construct another dictionary to hold that information.  Then most of
the code here:

> for i in main_op_list_np:
> if i in DLF002: DLF002_1.append('1')
> else:DLF002_1.append('0')
> if i in DLF004: DLF004_1.append('1')
> else:DLF004_1.append('0')
> if i in DLF005: DLF005_1.append('1')
> else:DLF005_1.append('0')
> if i in DLF006: DLF006_1.append('1')
> else:DLF006_1.append('0')
> if i in DLF007: DLF007_1.append('1')
> else:DLF007_1.append('0')
> if i in DLF008: DLF008_1.append('1')
> else:DLF008_1.append('0')
...

will dissolve into a simple dictionary lookup, followed by an array append.




Just to compare to a similar situation, consider the following.  Let's
say that we want to compute the letter frequency of a sentence.
Here's one way we could do it:


def histogram(message):
a = 0
b = 0
c = 0
d = 0
#  cut

for letter in message:
if letter == 'a':
a = a + 1
else if letter == 'b':
b = b + 1
# ... cut

return {
'a': a,
'b': b,
'c': c,
# ... cut
}


This is only a sketch.  We can see how this would go, if we fill in
the '...' with the obvious code.  But it would also be a very bad
approach.  It's highly repetitive, and easy to mistpe: you might miss
a letr.



There's a much better approach.  We can use a dictionary that maps
from a letter to its given frequency.

#
def histogram(message):
frequencies = {}
for letter in message:
frequencies[letter] = frequencies.get(letter, 0) + 1
return frequencies
#


Unlike the initial sketch, the version that takes advantage of
dictionaries is short and simple, and we can run it:

##
>>> histogram('the quick brown fox')
{' ': 3, 'c': 1, 'b': 1, 'e': 1, 'f': 1, 'i': 1, 'h': 1, 'k': 1, 'o':
2, 'n': 1, 'q': 1, 'r': 1, 'u': 1, 't': 1, 'w': 1, 'x': 1}
>>> histogram('abacadabra')
{'a': 5, 'c': 1, 'b': 2, 'r': 1, 'd': 1}
##



If you have questions, please feel free to ask.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fahrenheit to Celsius Conversion with if else statements

2017-06-12 Thread William Gan
Good day Alan,

Very much thanks for your guidance.

I have added or 'c' to the if statement. That is resolved.

Through that correction I discovered my C to F code was wrong. The + 32 is 
supposed to be executed at the end.

Thanks again. Cheers



-Original Message-
From: Alan Gauld [mailto:alan.ga...@yahoo.co.uk] 
Sent: Tuesday, June 13, 2017 1:36 AM
To: tutor@python.org
Subject: Re: [Tutor] Fahrenheit to Celsius Conversion with if else statements

On 12/06/17 15:17, William Gan wrote:

> print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to 
> Celsius.') unit = input('Enter C or F:') temp = int(input('Enter 
> temperature:'))
> 
> if unit == 'C':

Note this only t5ests for 'C' - ie capital C.
You might want to force the input to be uppercase first?

if unit.upper() == 'C':


> f = (temp + 32) * 9 / 5
> print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.')
> else:
> c = (temp - 32) * 5 / 9
> print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.')
> 

> However, when I entered C, the else block was executed instead. The if block 
> was skipped.
> 
> Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.
> 
> Enter C or F:c

Note you entered lowercase 'c' not 'C'.
Very different.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



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


[Tutor] decorators in a class

2017-06-12 Thread anish singh
Trying to use decorators in my class. I am calling
build_tree from the main function and i want to increment
the arguments by a constant factor by using decorators.
However as build_tree is a recursive function, I don't want
to call it recursively with increased constant factor always.

Probably decorators are not the ideal way for this but still
how to go about it using decorators.

Simple solution would be to just pass the parameters after
incrementing with constant values in the main function.
However, I want the caller of build_tree to not know that
internally we increment the indexes in the class and work
on that.
I can also call a intermediate function and then call build_tree
but then would that be the right way?

def pow_of_2(n):
n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n += 1
return n

def p_decorate(func):
def func_wrapper(self, left, right, root):
return func(self, left+self.n, right+self.n, root)
return func_wrapper

class segment_tree(object):
def __init__(self, data):
self.n = pow_of_2(len(data))
self.tree = [0]*self.n + data + [0]*(self.n - len(data))

@p_decorate
def build_tree(self, left, right, root):
if left == right:
return self.tree[left]
#below build_tree should not use decorated function,
#how to achieve that?
s = self.build_tree(left, (left+right)/2, 2*root) +
  self.build_tree(1+(left+right)/2, right, 2*root+1)
self.tree[root] = s

def __repr__(self):
return " ".join(str(i) for i in self.tree)

data = [1, 2, 3, 4]
sg = segment_tree(data)
sg.build_tree(0, 7, 1)
print(sg)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 15:57, Vikas YADAV wrote:

> for i in range(len(s)-1, 1, -2):
> print s[i]
> -
> 
> So my question is: how would you write "s[::-1]" in terms of a for loop for 
> illustration purpose?

Exactly as above but replace -2 with -1

for i in range(len(s)-1, 1, -1):
print s[i]

But that seems too obvious, am I missing something
subtle in your question?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Creating 2 Subarrays for large dataset

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 16:52, Peter Gibson wrote:

> I have a large, 4 column data set that is in the form of an array.  

Do you mean 4 separate arrays or a single 4xN array?
Or do you mean an N size array of 4 item tuples?
Or are the 4 colums part of a class?

There are lots of ways to interpret that statement.
Also are you using NumPy arrays or standard Python
arrays, or standard Python lists/tuples treated as arrays?

These all have a bearing.

It might help if you post a small sample of
your real data structure?

> last column, there is either a 1 or a 2, and they are not organized in any
> predictable manner (ex of an array of the last columns:
> 1,2,2,1,2,2,1,1,1,1,2,1,1, ect).
> 
> I would like to cut this large data set into two different arrays, one
> where the final column has a 1 in it, and the other where the final column
> of the data has a 2 in it.

A trivial way to do that (assuming 4 arrays called
col1,col2,col3,col4) is to create two lists/arrays
and iterate over the data filtering on col4:

ones = []
twos = []
for n in col4:
   if n == 1: ones.append((col1[n],col2[n],col3[n]))
   else: twos.append((col1[n],col2[n],col3[n]))

If you must have arrays rather than lists that's a relatively
minor tweak.

However, depending on your data structures there are
likely more efficient ways to do it (e.g. sorting on
column 4 then using slicing, or using a dict keyed
on col4, etc).

but it all depends on how large 'large' is, what the
actual time constraints are, what the real data structures
look like etc. Premature optimisation is the root of all
evil...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Fahrenheit to Celsius Conversion with if else statements

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 15:17, William Gan wrote:

> print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')
> unit = input('Enter C or F:')
> temp = int(input('Enter temperature:'))
> 
> if unit == 'C':

Note this only t5ests for 'C' - ie capital C.
You might want to force the input to be uppercase first?

if unit.upper() == 'C':


> f = (temp + 32) * 9 / 5
> print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.')
> else:
> c = (temp - 32) * 5 / 9
> print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.')
> 

> However, when I entered C, the else block was executed instead. The if block 
> was skipped.
> 
> Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.
> 
> Enter C or F:c

Note you entered lowercase 'c' not 'C'.
Very different.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Huge list comprehension

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 05:54, syed zaidi wrote:

> One reason fornsharing the code was that I have to manually create over 100 
> variables
> Is there a way i can automate thst process?
I doubt very much that you *have* to create 100 varables,
that's an implementation choice as part of your design.

If you tell us what the problem is you are trying to solve
we might be able to suggest a more manageable solution
 - such as a better data structure perhaps?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Vikas YADAV
Thanks, Abdur for forwarding Steve's message. It is a very good explanation 
with examples.

The following example  does help:
-
py> s = "Nobody expects the Spanish Inquisition!"
py> s[-1:1:-2]
'!otsun snp h tex db'

is somewhat like:

for i in range(len(s)-1, 1, -2):
print s[i]
-

So my question is: how would you write "s[::-1]" in terms of a for loop for 
illustration purpose?



Vikas

-Original Message-
From: Abdur-Rahmaan Janhangeer [mailto:arj.pyt...@gmail.com] 
Sent: Monday, June 12, 2017 1:26 AM
To: Vikas YADAV 
Cc: tutor 
Subject: Re: [Tutor] string reversal using [::-1]

[QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING 
QUESTION]

The way to think about string indexing and slicing is that the index positions 
mark *between* the characters. Take your string:

Machine learning is awesome!

For brevity, I'll just use the first word:

Machine

Imagine slicing it between the characters. I'll mark the cuts with a vertical 
bar:

|M|a|c|h|i|n|e|

and add indexes. The indexes will only line correctly if you use a monspaced or 
fixed width font like Courier, otherwise things may not line up correctly.

|M|a|c|h|i|n|e|
0 1 2 3 4 5 6 7

Here they are again starting from the right, I've spread things out a bit to 
fit in the minus signs:

   |M  |a  |c  |h  |i  |n  |e  |
   -7  -6  -5  -4  -3  -2  -1  0

Notice that 0 gets used twice. Of course, that's impossible, because it would 
be ambiguous. If you give 0 as an index, how does Python know whether you mean 
0 at the start or 0 or the end? So the simple rule Python uses is that 0 
*always* means the start.

When you give a single index, Python always uses the character immediately to 
the right of the cut:

s = "Machine"
s[0]
=> returns "M"

s[-1]
=> returns "e"

s[7]
=> raises an exception, because there is no character to the right

When you give two indexes, using slice notation, Python returns the characters 
BETWEEN those cuts:

s[0:7]
=> returns "Machine"

s[1:-1]
=> returns "achin"

Because 0 always means the start of the string, how do you slice to the end? 
You can use the length of the string (in this case, 7) or you can leave the 
ending position blank, and it defaults to the length of the
string:

s[1:]  # means the same as [1:len(s)]

You can leave the starting position blank too, it defaults to 0:

s[:]  # means the same as [0:len(s)]

So remember that slices always cut *between* the index positions.


Things get complicated when you include a step (or stride), especially when the 
step is negative. For step sizes other than 1, it is probably best to think of 
looping over the string:

py> s = "Nobody expects the Spanish Inquisition!"
py> s[-1:1:-2]
'!otsun snp h tex db'

is somewhat like:

for i in range(len(s)-1, 1, -2):
print s[i]



--
Steve
[QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING 
QUESTION]


Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 10 Jun 2017 21:31, "Vikas YADAV"  wrote:

> Question: Why does "123"[::-1] result in "321"?
>
>
>
> MY thinking is [::-1] is same as [0:3:-1], that the empty places 
> defaults to start and end index of the string object.
>
> So, if we start from 0 index and decrement index by 1 till we reach 3, 
> how many index we should get? I think we should get infinite infinite 
> number of indices (0,-1,-2,-3.).
>
>
>
> This is my confusion.
>
> I hope my question is clear.
>
>
>
> Thanks,
>
> Vikas
>
> ___
> 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


[Tutor] Creating 2 Subarrays for large dataset

2017-06-12 Thread Peter Gibson
Hello,

I have a large, 4 column data set that is in the form of an array.  In the
last column, there is either a 1 or a 2, and they are not organized in any
predictable manner (ex of an array of the last columns:
1,2,2,1,2,2,1,1,1,1,2,1,1, ect).

I would like to cut this large data set into two different arrays, one
where the final column has a 1 in it, and the other where the final column
of the data has a 2 in it.

Please let me know if there is a way to create two separate arrays from my
large data set and also how I can name these so that I may further
manipulate the subsets of data.

Thank you,

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


[Tutor] Fahrenheit to Celsius Conversion with if else statements

2017-06-12 Thread William Gan
Good day Everybody,

I am practicing coding when I encountered a problem with the if and else 
statements in my code. Hope someone can help me understand my mistake.

The following is my code to convert Fahrenheit to Celsius and vice-versa:

print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')
unit = input('Enter C or F:')
temp = int(input('Enter temperature:'))

if unit == 'C':
f = (temp + 32) * 9 / 5
print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.')
else:
c = (temp - 32) * 5 / 9
print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.')

OUT:
Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.
Enter C or F:f
Enter temperature:212
212 F is equivalent to 100.00 C.

However, when I entered C, the else block was executed instead. The if block 
was skipped.



Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.

Enter C or F:c

Enter temperature:100

100 F is equivalent to 37.78 C.

I could not figure out my mistake.

For advice, please. Thank you.

Best regards
william

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


Re: [Tutor] Huge list comprehension

2017-06-12 Thread syed zaidi
Thanks
One reason fornsharing the code was that I have to manually create over 100 
variables
Is there a way i can automate thst process?

Get Outlook for Android


From: Abdur-Rahmaan Janhangeer
Sent: Saturday, June 10, 3:35 PM
Subject: Re: [Tutor] Huge list comprehension
To: syed zaidi, tutor

take a look at numpy

and don't necessarily give us the whole code. it becomes too long without 
purpose

Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 6 Jun 2017 03:26, "syed zaidi" 
> wrote:

hi,

I would appreciate if you can help me suggesting a quick and efficient strategy 
for comparing multiple lists with one principal list

I have about 125 lists containing about 100,000 numerical entries in each

my principal list contains about 6 million entries.

I want to compare each small list with main list and append yes/no or 0/1 in 
each new list corresponding to each of 125 lists

The program is working but it takes ages to process huge files,
Can someone pleases tell me how can I make this process fast. Right now it 
takes arounf 2 weeks to complete this task

the code I have written and is working is as under:

sample_name = []

main_op_list,principal_list = [],[]
dictionary = {}

with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f:
reader = csv.reader(f, dialect = 'excel', delimiter='\t')
list2 = filter(None, reader)
for i in range(len(list2)):
col1 = list2[i][0]
operon = list2[i][1]
main_op_list.append(operon)
col1 = col1.strip().split("_")
sample_name = col1[0]
if dictionary.get(sample_name):
dictionary[sample_name].append(operon)
else:
dictionary[sample_name] = []
dictionary[sample_name].append(operon)
locals().update(dictionary) ## converts dictionary keys to variables
##print DLF004
dict_values = dictionary.values()
dict_keys = dictionary.keys()
print dict_keys
print len(dict_keys)
main_op_list_np = np.array(main_op_list)

DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1
 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1
 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 = 
[],[],[],[],[],[],[]
NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1
 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1
 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1
 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[]

for i in main_op_list_np:
if i in DLF002: DLF002_1.append('1')
else:DLF002_1.append('0')
if i in DLF004: DLF004_1.append('1')
else:DLF004_1.append('0')
if i in DLF005: DLF005_1.append('1')
else:DLF005_1.append('0')
if i in DLF006: DLF006_1.append('1')
else:DLF006_1.append('0')
if i in DLF007: DLF007_1.append('1')
else:DLF007_1.append('0')
if i in DLF008: DLF008_1.append('1')
else:DLF008_1.append('0')
##   if main_op_list[i] in DLF009: DLF009_1.append('1')
 ##   else:DLF009_1.append('0')
if i in DLF010: DLF010_1.append('1')
else:DLF010_1.append('0')
if i in DLF012: DLF012_1.append('1')
else:DLF012_1.append('0')
if i in DLF013: DLF013_1.append('1')
else:DLF013_1.append('0')
if i in DLF014: DLF014_1.append('1')
else:DLF014_1.append('0')
if i in DLM001: DLM001_1.append('1')
else:DLM001_1.append('0')
if i in DLM002: DLM002_1.append('1')
else:DLM002_1.append('0')
if i in DLM003: DLM003_1.append('1')
else:DLM003_1.append('0')
if i in DLM004: DLM004_1.append('1')
else:DLM004_1.append('0')
if i in DLM005: DLM005_1.append('1')
else:DLM005_1.append('0')
if i in DLM006: DLM006_1.append('1')
else:DLM006_1.append('0')
if i in DLM009: DLM009_1.append('1')
else:DLM009_1.append('0')

Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Abdur-Rahmaan Janhangeer
[QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING
QUESTION]

The way to think about string indexing and slicing is that the index
positions mark *between* the characters. Take your string:

Machine learning is awesome!

For brevity, I'll just use the first word:

Machine

Imagine slicing it between the characters. I'll mark the cuts with a
vertical bar:

|M|a|c|h|i|n|e|

and add indexes. The indexes will only line correctly if you use a
monspaced or fixed width font like Courier, otherwise things may not
line up correctly.

|M|a|c|h|i|n|e|
0 1 2 3 4 5 6 7

Here they are again starting from the right, I've spread things out a
bit to fit in the minus signs:

   |M  |a  |c  |h  |i  |n  |e  |
   -7  -6  -5  -4  -3  -2  -1  0

Notice that 0 gets used twice. Of course, that's impossible, because it
would be ambiguous. If you give 0 as an index, how does Python know
whether you mean 0 at the start or 0 or the end? So the simple rule
Python uses is that 0 *always* means the start.

When you give a single index, Python always uses the character
immediately to the right of the cut:

s = "Machine"
s[0]
=> returns "M"

s[-1]
=> returns "e"

s[7]
=> raises an exception, because there is no character to the right

When you give two indexes, using slice notation, Python returns the
characters BETWEEN those cuts:

s[0:7]
=> returns "Machine"

s[1:-1]
=> returns "achin"

Because 0 always means the start of the string, how do you slice to the
end? You can use the length of the string (in this case, 7) or you can
leave the ending position blank, and it defaults to the length of the
string:

s[1:]  # means the same as [1:len(s)]

You can leave the starting position blank too, it defaults to 0:

s[:]  # means the same as [0:len(s)]

So remember that slices always cut *between* the index positions.


Things get complicated when you include a step (or stride), especially
when the step is negative. For step sizes other than 1, it is
probably best to think of looping over the string:

py> s = "Nobody expects the Spanish Inquisition!"
py> s[-1:1:-2]
'!otsun snp h tex db'

is somewhat like:

for i in range(len(s)-1, 1, -2):
print s[i]



--
Steve
[QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING
QUESTION]


Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 10 Jun 2017 21:31, "Vikas YADAV"  wrote:

> Question: Why does "123"[::-1] result in "321"?
>
>
>
> MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults
> to
> start and end index of the string object.
>
> So, if we start from 0 index and decrement index by 1 till we reach 3, how
> many index we should get? I think we should get infinite infinite number of
> indices (0,-1,-2,-3.).
>
>
>
> This is my confusion.
>
> I hope my question is clear.
>
>
>
> Thanks,
>
> Vikas
>
> ___
> 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] Python - help with something most essential

2017-06-12 Thread Abdur-Rahmaan Janhangeer
i might add that
with open( . . .

instead of

foo = open( . . .

also shows some maturity in py

Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 11 Jun 2017 12:33, "Peter Otten" <__pete...@web.de> wrote:

> Japhy Bartlett wrote:
>
> > I'm not sure that they cared about how you used file.readlines(), I think
> > the memory comment was a hint about instantiating Counter()s
>
> Then they would have been clueless ;)
>
> Both Schtvveer's original script and his subsequent "Verschlimmbesserung"
> --
> beautiful german word for making things worse when trying to improve them
> --
> use only two Counters at any given time. The second version is very
> inefficient because it builds the same Counter over and over again -- but
> this does not affect peak memory usage much.
>
> Here's the original version that triggered the comment:
>
> [Schtvveer Schvrveve]
>
> > import sys
> > from collections import Counter
> >
> > def main(args):
> > filename = args[1]
> > word = args[2]
> > print countAnagrams(word, filename)
> >
> > def countAnagrams(word, filename):
> >
> > fileContent = readFile(filename)
> >
> > counter = Counter(word)
> > num_of_anagrams = 0
> >
> > for i in range(0, len(fileContent)):
> > if counter == Counter(fileContent[i]):
> > num_of_anagrams += 1
> >
> > return num_of_anagrams
> >
> > def readFile(filename):
> >
> > with open(filename) as f:
> > content = f.readlines()
> >
> > content = [x.strip() for x in content]
> >
> > return content
> >
> > if __name__ == '__main__':
> > main(sys.argv)
> >
>
> referenced as before.py below, and here's a variant that removes
> readlines(), range(), and the [x.strip() for x in content] list
> comprehension, the goal being minimal changes, not code as I would write it
> from scratch.
>
> # after.py
> import sys
> from collections import Counter
>
> def main(args):
> filename = args[1]
> word = args[2]
> print countAnagrams(word, filename)
>
> def countAnagrams(word, filename):
>
> fileContent = readFile(filename)
> counter = Counter(word)
> num_of_anagrams = 0
>
> for line in fileContent:
> if counter == Counter(line):
> num_of_anagrams += 1
>
> return num_of_anagrams
>
> def readFile(filename):
> # this relies on garbage collection to close the file
> # which should normally be avoided
> for line in open(filename):
> yield line.strip()
>
> if __name__ == '__main__':
> main(sys.argv)
>
> How to measure memoryview? I found
>  usage-of-a-linux-unix-process> and as test data I use files containing
> 10**5 and 10**6
> integers. With that setup (snipping everything but memory usage from the
> time -v output):
>
> $ /usr/bin/time -v python before.py anagrams5.txt 123
> 6
> Maximum resident set size (kbytes): 17340
> $ /usr/bin/time -v python before.py anagrams6.txt 123
> 6
> Maximum resident set size (kbytes): 117328
>
>
> $ /usr/bin/time -v python after.py anagrams5.txt 123
> 6
> Maximum resident set size (kbytes): 6432
> $ /usr/bin/time -v python after.py anagrams6.txt 123
> 6
> Maximum resident set size (kbytes): 6432
>
> See the pattern? before.py uses O(N) memory, after.py O(1).
>
> Run your own tests if you need more datapoints or prefer a different method
> to measure memory consumption.
>
> ___
> 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] Huge list comprehension

2017-06-12 Thread Abdur-Rahmaan Janhangeer
i think that you created 1000 vars as you needed them with different names

replacing these with dictionary keys might be the answer

if you hava a specific pattern for the variable, it'll be fine as it'll
just be string manipulation e.g.

variables ={ }

then

for i in range(..):
 variables[*keyname here*] = [ ]

Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 12 Jun 2017 08:54, "syed zaidi"  wrote:

> Thanks
> One reason fornsharing the code was that I have to manually create over
> 100 variables
> Is there a way i can automate thst process?
>
> Get Outlook for Android 
>
> From: Abdur-Rahmaan Janhangeer
> Sent: Saturday, June 10, 3:35 PM
> Subject: Re: [Tutor] Huge list comprehension
> To: syed zaidi, tutor
>
> take a look at numpy
>
> and don't necessarily give us the whole code. it becomes too long without
> purpose
>
> Abdur-Rahmaan Janhangeer,
> Mauritius
> abdurrahmaanjanhangeer.wordpress.com
>
> On 6 Jun 2017 03:26, "syed zaidi"  wrote:
>
>
> hi,
>
> I would appreciate if you can help me suggesting a quick and efficient
> strategy for comparing multiple lists with one principal list
>
> I have about 125 lists containing about 100,000 numerical entries in each
>
> my principal list contains about 6 million entries.
>
> I want to compare each small list with main list and append yes/no or 0/1
> in each new list corresponding to each of 125 lists
>
> The program is working but it takes ages to process huge files,
> Can someone pleases tell me how can I make this process fast. Right now it
> takes arounf 2 weeks to complete this task
>
> the code I have written and is working is as under:
>
> sample_name = []
>
> main_op_list,principal_list = [],[]
> dictionary = {}
>
> with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r')
> as f:
> reader = csv.reader(f, dialect = 'excel', delimiter='\t')
> list2 = filter(None, reader)
> for i in range(len(list2)):
> col1 = list2[i][0]
> operon = list2[i][1]
> main_op_list.append(operon)
> col1 = col1.strip().split("_")
> sample_name = col1[0]
> if dictionary.get(sample_name):
> dictionary[sample_name].append(operon)
> else:
> dictionary[sample_name] = []
> dictionary[sample_name].append(operon)
> locals().update(dictionary) ## converts dictionary keys to variables
> ##print DLF004
> dict_values = dictionary.values()
> dict_keys = dictionary.keys()
> print dict_keys
> print len(dict_keys)
> main_op_list_np = np.array(main_op_list)
>
> DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,
> DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,
> DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,
> DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1
> =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
> DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,
> DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,
> DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,
> DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1
> =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
> DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 =
> [],[],[],[],[],[],[]
> NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,
> NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,
> NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,
> NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1
> =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
> NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,
> NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,
> NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,
> NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1
> =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
> NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,
> NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1,
> NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,
> NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1
> =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
> NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[]
>
> for i in main_op_list_np:
> if i in DLF002: DLF002_1.append('1')
> else:DLF002_1.append('0')
> if i in DLF004: DLF004_1.append('1')
> else:DLF004_1.append('0')
> if i in DLF005: DLF005_1.append('1')
> else:DLF005_1.append('0')
> if i in DLF006: DLF006_1.append('1')
> else:DLF006_1.append('0')
> if i in DLF007: DLF007_1.append('1')
> else:DLF007_1.append('0')
> if i in DLF008: DLF008_1.append('1')
> else:DLF008_1.append('0')
> ##   if main_op_list[i] in DLF009: DLF009_1.append('1')
>  ##   else:DLF009_1.append('0')
> if i in DLF010: DLF010_1.append('1')
> else:DLF010_1.append('0')
> if i in DLF012: DLF012_1.append('1')
> else:DLF012_1.append('0')
> if i in DLF013: 

Re: [Tutor] tkinter actively maintained?

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 03:54, Michael C wrote:
> Hi all:
> 
> is tkinter still being actively maintained? I only had to ask about this
> because I have never looked stuff like this up before.

Yes, Tkinter tracks the Tk releases so that most of
the activity at present is in the ttk sub package.

There is a moderately active active mailing list
and you can take a look at the list archive
to see the kind of issues they cover.

https://mail.python.org/pipermail/tkinter-discuss/

That having been said, and despite the fact I use
tkinter a lot, it is not the best UI toolkit if
you want to build a comprehensive desktop app.
GTk, Qt and Wxwidgets all offer much richer widget
support and better integration with native look 'n feel.
But Tkinter is great for quick and dirty UIs on top
of CLI code and is easier to learn.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] tkinter actively maintained?

2017-06-12 Thread Michael C
Hi all:

is tkinter still being actively maintained? I only had to ask about this
because I have never looked stuff like this up before.

Is it ok to develop using it, is it going to be dropped in the near future?

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