Re: [Tutor] indexing a list

2012-10-18 Thread eryksun
On Thu, Oct 18, 2012 at 8:01 AM, Spyros Charonis  wrote:
>
> x = 21   # WINDOW LENGTH
>
> In [70]: SEQ[0:x]
> Out[70]: 'MKAAVLTLAVLFLTGSQARHF'
>
> In [71]: SEQ[x:2*x]
> Out[71]: 'WQQDEPPQSPWDRVKDLATVY'
>
> In [72]: SEQ[2*x:3*x]
> Out[72]: 'VDVLKDSGRDYVSQFEGSALG'
>
> How could I write a function to automate this so that it does this from
> SEQ[0] throughout the entire sequence, i.e. until len(SEQ)?

In your examples, the lower slice limit is 0x, 1x, 2x, and so on. The
upper limit is 1x, 2x, 3x, and so on. That should scream that you need
a counter, or range/xrange. The lower limit of the last slice should
be less than len(SEQ), such that there's at least 1 item in the last
slice. So, in terms of range, the start value is 0, and the stop value
is len(SEQ). To make things even simpler, range takes an optional step
size. This gives you the 0x, 1x, 2x, etc for the start index of each
slice. The upper bound is then i+x (corresponding to 1x, 2x, 3x, etc).
For example:

>>> seq = 'MKAAVLTLAVLFLTGSQARHFWQQDEPPQSPWDRVKDLATVYVDVLK'
>>> x = 21
>>> for i in range(0, len(seq), x):
... print(seq[i:i+x])
...
MKAAVLTLAVLFLTGSQARHF
WQQDEPPQSPWDRVKDLATVY
VDVLK

If you're using Python 2.x, use xrange instead of range, and "print"
is a statement instead of a function.

You can also use a generator expression to create a one-time iterable
object that can be used in another generator, a for loop, a
comprehension, or as the argument of a function that expects an
iterable, such as the list() constructor:

>>> chunks = (seq[i:i+x] for i in range(0, len(seq), x))
>>> list(chunks)
['MKAAVLTLAVLFLTGSQARHF', 'WQQDEPPQSPWDRVKDLATVY', 'VDVLK']
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] indexing a list

2012-10-18 Thread Spyros Charonis
Hello pythoners,

I have a string that I want to read in fixed-length windows.

In [68]: SEQ
Out[68]:
'MKAAVLTLAVLFLTGSQARHFWQQDEPPQSPWDRVKDLATVYVDVLKDSGRDYVSQFEGSALGKQLNLKLLDNWDSVTSTFSKLREQLGPVTQEFWDNLEKETEGLRQEMSKDLEEVKAKVQPYLDDFQKKWQEEMELYRQKVEPLRAELQEGARQKLHELQEKLSPLGEEMRDRARAHVDALRTHLAPYSDELRQRLAARLEALKENGGARLAEYHAKATEHLSTLSEKAKPALEDLRQ'

I would like a function that reads the above string, 21 characters at a
time, and checks for certain conditions, i.e. whether characters co-occur
in other lists I have made. For example:

x = 21   # WINDOW LENGTH

In [70]: SEQ[0:x]
Out[70]: 'MKAAVLTLAVLFLTGSQARHF'

In [71]: SEQ[x:2*x]
Out[71]: 'WQQDEPPQSPWDRVKDLATVY'

In [72]: SEQ[2*x:3*x]
Out[72]: 'VDVLKDSGRDYVSQFEGSALG'

How could I write a function to automate this so that it does this from
SEQ[0] throughout the entire sequence, i.e. until len(SEQ)?

Many thanks for your time,
Spyros
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a list with nested tuples

2011-08-05 Thread Alexander Quest
My bad- meant to say [1]. Thanks.

-Alexander

On Fri, Aug 5, 2011 at 12:36 PM, Christopher King wrote:

>
>
> On Tue, Aug 2, 2011 at 10:44 PM, Alexander Quest wrote:
>>
>> have [0] to indicate that I want to go to the second value within that
>> first item, which is the
>> point value
>>
> Actually [0] is the first element. I would go with [1].
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a list with nested tuples

2011-08-05 Thread Christopher King
On Tue, Aug 2, 2011 at 10:44 PM, Alexander Quest wrote:
>
> have [0] to indicate that I want to go to the second value within that
> first item, which is the
> point value
>
Actually [0] is the first element. I would go with [1].
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a list with nested tuples

2011-08-03 Thread Alexander Quest
Hi Bob- thanks for the reply again. I apologize about not "replying all"
last time- still getting in the habit of doing this.

I am using Python version 3.1. As far as tuples are concerned, I don't NEED
to use them, but I am trying to get some practice with them. This is because
I am following an instructional book that is discussing nested tuples within
lists.
The way I get the "selection" variable from the user is just by typing the
following: selection = input("Selection: ")

I'm not sure why it reads it initially as a string, but I later included the
line selection = int(selection), which solved the int/str problem.

Also, I was about to switch to dictionaries or just lists without tuples,
but another poster above stated that I could just replace the entire tuple
item within the list, which technically would not be changing the tuple, so
it worked out. The only problem I have now is trying to sort the 4
attributes based on their numerical value, not their alphabetical value. But
when I type in  attributes.sort(reverse=True), it sorts them alphabetically
because the name of the attribute is 1st in the list, and its value is 2nd.
Here it is again for reference: attributes = [("strength", 0), ("health  ",
0), ("wisdom  ", 0), ("dexterity", 0)]

Sorry if this is a bit confusing. Thanks for your help and tips so far Bob.

-Alex

On Wed, Aug 3, 2011 at 5:52 AM, bob gailer  wrote:

>  On 8/2/2011 11:39 PM, Alexander Quest wrote:
>
> Hey Bob- thanks for the reply. Here is a more complete part of that code
> section (the ellipses are parts where I've deleted code because I don't
> think it's important for this question):
>
>
> Please always reply-all so a copy goes to the list.
>
> Thanks for posting more code & traceback
>
> I forgot to mention earlier - tell us which version of Python you are using
> (this looks like version 3)
>
> You did not answer all my questions! How come? Please do so now.
>
>
>
> _
> attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
> ("dexterity", 0)]
> .
> .
> .
> print(
> """
> 1 - Strength
> 2 - Health
> 3 - Wisdom
> 4 - Dexterity
>
> Any other key - Quit
> """
> )
> selection = input("Selection: ")
> if selection == "1" or selection == "2" or selection == "3" or selection ==
> "4":
> print("You have ", points, "points available.")
> how_many = input("How many would you like to add to this
> attribute?: ")
> while how_many < 0 or how_many > 30 or how_many ==
> "":   # Because max points available is 30, and
> entering less than 0 does not make sense.
> print("Invalid entry. You have ", points, "points
> available.")   # If the user enters a number
> less than 0, greater than 30, or just presses enter, it loops.
> how_many = input("How many would you like to add to
> this attribute?: ")
> print("Added ", points, "to ", attributes[selection-1][0],
> "attribute.") # Here is where I try to add the
> number of points to the value, based on what the user entered.
> points = points -
> how_many
> # I subtract the number of points added from the total points available.
> attributes[selection-1][1] +=
> how_many  # I
> add the number of points the user selected to the variable selected.
>
>
> __
>
>
> Here's the traceback I get:
>
> Traceback (most recent call last):
>   File "C:\Users\Alexander\Desktop\Python Practice\Ch05-2.py", line 54, in
> 
> print("Added ", points, "to ", attributes[selection-1][0],
> "attribute.")
> TypeError: unsupported operand type(s) for -: 'str' and 'int'
> _
>
> Thanks for any help. I understand that I can't change tuples directly, but
> is there a way to change them indirectly (like saying attribute.remove[x]
> and then saying attribute.append[x] with the new variable? But this seems to
> take out both the string and the value, when I only want to increase or
> decrease the value for one of the 4 strings, strength, health, wisdom, or
> dexterity).
>
>
> DON'T USE TUPLES. WHY DO YOU INSIST ON THEM?
>
>
>
>> What does the error message( unsupported operand type(s) for -: 'str' and
>> 'int')  tell you?
>
>
>  Why would selection be a string rather than an integer?
>
>
>  This has to do with how you obtain selection from the user.
>>
>> Why did you expect to be able to alter the value of a tuple element?
>> Tuples are immutable! Use a list instead.
>>
>>
> --
> Bob Gailer919-636-4239
> Chapel Hill NC
>
>
___
Tutor

Re: [Tutor] Indexing a list with nested tuples

2011-08-03 Thread Alexander Quest
Thanks Peter- I tried the replacement method where the entire tuple is
replaced with a new one and that worked. Changing the "attribute_index" (or
"selection" variable, as I called it) to an integer removed the int/str
errors.

-Alex

On Wed, Aug 3, 2011 at 12:12 AM, Peter Otten <__pete...@web.de> wrote:

> Alexander Quest wrote:
>
> > Hi guys- I'm having a problem with a list that has nested tuples:
> >
> > attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
> > ("dexterity", 0)]
> >
> > I've defined the list above with 4 items, each starting with a value of
> 0.
> > The player
> > enters how many points he or she wants to add to a given item. The
> > selection menu
> > is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
> > variable is actually
> > 1 more than the index location of the intended item. So I have the
> > following code:
> >
> > print("Added ", points, "to ", attributes[selection-1][0], "attribute.")
> >
> > My intent with this is to say that I've added this many points (however
> > many) to the
> > corresponding item in the list. So if the player selects "1", then
> > selection = 1, but I subtract
> > 1 from that (selection -1) to get the index value of that item in the
> list
> > (in this case 0). Then I
> > have [0] to indicate that I want to go to the second value within that
> > first item, which is the
> > point value. I get an error saying that list indices must be integers,
> not
> > strings. I get a similar
> > error even if I just put attributes[selection][0] without the minus 1.
> >
> > Also, it seems that the tuple within the list cannot be modified
> directly,
> > so I can't add points to the original value of "0" that all 4 items start
> > with. Is there a way to keep this nested list with
> > tuples but be able to modify the point count for each item, or will it be
> > better to create a dictionary or 2 separate lists (1 for the names
> > "Strength, Health, Wisdom, Dexterity" and one
> > for their starting values "0,0,0,0")? Any suggestions/help will be
> greatly
> > appreciated!!!
>
> [I'm assuming you are using Python 3. If not replace input() with
> raw_input()]
>
> Let's investigate what happens when you enter an attribute index:
>
> >>> attribute_index = input("Choose attribute ")
> Choose attribute 2
> >>> attribute_index
> '2'
>
> Do you note the '...' around the number?
>
> >>> attribute_index -= 1
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unsupported operand type(s) for -=: 'str' and 'int'
>
> It's actually a string, not an integer; therefore you have to convert it to
> an integer before you can do any math with it:
>
> >>> attribute_index = int(attribute_index)
> >>> attribute_index
> 2
> >>> attribute_index -= 1
> >>> attribute_index
> 1
>
> Now let's try to change the second tuple:
>
> >>> attributes = [
> ... ("strength", 0), ("health", 0), ("wisdom", 0), ("dexterity", 0)]
> >>> attributes[attribute_index]
> ('health', 0)
> >>> attributes[attribute_index][1] += 42
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: 'tuple' object does not support item assignment
>
> The error message is pretty clear, you cannot replace items of a tuple.
> You can either to switch to nested lists
>
> [["strength", 0], ["health", 0], ...]
>
> or replace the entire tuple with a new one:
>
> >>> name, value = attributes[attribute_index]
> >>> attributes[attribute_index] = name, value + 42
> >>> attributes
> [('strength', 0), ('health', 42), ('wisdom', 0), ('dexterity', 0)]
>
> However, I think the pythonic way is to use a dictionary. If you want the
> user to input numbers you need a second dictionary to translate the numbers
> into attribute names:
>
> >>> attributes = dict(attributes)
> >>> lookup = {1: "strength", 2: "health", 3: "wisdom", 4: "dexterity"}
> >>> while True:
> ... index = input("index ")
> ... if not index: break
> ... amount = int(input("amount "))
> ... name = lookup[int(index)]
> ... attributes[name] += amount
> ...
> index 1
> amount 10
> index 2
> amount 20
> index 3
> amount 10
> index 2
> amount -100
> index
> >>> attributes
> {'dexterity': 0, 'strength': 10, 'health': -38, 'wisdom': 10}
>
> Personally I would ask for attribute names directly.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a list with nested tuples

2011-08-03 Thread bob gailer

On 8/2/2011 11:39 PM, Alexander Quest wrote:
Hey Bob- thanks for the reply. Here is a more complete part of that 
code section (the ellipses are parts where I've deleted code because I 
don't think it's important for this question):


Please always reply-all so a copy goes to the list.

Thanks for posting more code & traceback

I forgot to mention earlier - tell us which version of Python you are 
using (this looks like version 3)


You did not answer all my questions! How come? Please do so now.



_
attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0), 
("dexterity", 0)]

.
.
.
print(
"""
1 - Strength
2 - Health
3 - Wisdom
4 - Dexterity

Any other key - Quit
"""
)
selection = input("Selection: ")
if selection == "1" or selection == "2" or selection == "3" or 
selection == "4":

print("You have ", points, "points available.")
how_many = input("How many would you like to add to 
this attribute?: ")
while how_many < 0 or how_many > 30 or how_many == 
"":   # Because max points available is 
30, and entering less than 0 does not make sense.
print("Invalid entry. You have ", points, "points 
available.")   # If the user enters a 
number less than 0, greater than 30, or just presses enter, it loops.
how_many = input("How many would you like to add 
to this attribute?: ")
print("Added ", points, "to ", 
attributes[selection-1][0], "attribute.") # 
Here is where I try to add the number of points to the value, based on 
what the user entered.
points = points - 
how_many   
# I subtract the number of points added from the total points available.
attributes[selection-1][1] += 
how_many  
# I add the number of points the user selected to the variable selected.



__


Here's the traceback I get:

Traceback (most recent call last):
  File "C:\Users\Alexander\Desktop\Python Practice\Ch05-2.py", line 
54, in 
print("Added ", points, "to ", attributes[selection-1][0], 
"attribute.")

TypeError: unsupported operand type(s) for -: 'str' and 'int'
_

Thanks for any help. I understand that I can't change tuples directly, 
but is there a way to change them indirectly (like saying 
attribute.remove[x] and then saying attribute.append[x] with the new 
variable? But this seems to take out both the string and the value, 
when I only want to increase or decrease the value for one of the 4 
strings, strength, health, wisdom, or dexterity).


DON'T USE TUPLES. WHY DO YOU INSIST ON THEM?



What does the error message( unsupported operand type(s) for -:
'str' and 'int')  tell you? 



Why would selection be a string rather than an integer? 




This has to do with how you obtain selection from the user.

Why did you expect to be able to alter the value of a tuple
element? Tuples are immutable! Use a list instead.



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Indexing a list with nested tuples

2011-08-03 Thread Peter Otten
Alexander Quest wrote:

> Hi guys- I'm having a problem with a list that has nested tuples:
> 
> attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
> ("dexterity", 0)]
> 
> I've defined the list above with 4 items, each starting with a value of 0.
> The player
> enters how many points he or she wants to add to a given item. The
> selection menu
> is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
> variable is actually
> 1 more than the index location of the intended item. So I have the
> following code:
> 
> print("Added ", points, "to ", attributes[selection-1][0], "attribute.")
> 
> My intent with this is to say that I've added this many points (however
> many) to the
> corresponding item in the list. So if the player selects "1", then
> selection = 1, but I subtract
> 1 from that (selection -1) to get the index value of that item in the list
> (in this case 0). Then I
> have [0] to indicate that I want to go to the second value within that
> first item, which is the
> point value. I get an error saying that list indices must be integers, not
> strings. I get a similar
> error even if I just put attributes[selection][0] without the minus 1.
> 
> Also, it seems that the tuple within the list cannot be modified directly,
> so I can't add points to the original value of "0" that all 4 items start
> with. Is there a way to keep this nested list with
> tuples but be able to modify the point count for each item, or will it be
> better to create a dictionary or 2 separate lists (1 for the names
> "Strength, Health, Wisdom, Dexterity" and one
> for their starting values "0,0,0,0")? Any suggestions/help will be greatly
> appreciated!!!

[I'm assuming you are using Python 3. If not replace input() with 
raw_input()]

Let's investigate what happens when you enter an attribute index:

>>> attribute_index = input("Choose attribute ")
Choose attribute 2
>>> attribute_index
'2'

Do you note the '...' around the number?

>>> attribute_index -= 1
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for -=: 'str' and 'int'

It's actually a string, not an integer; therefore you have to convert it to 
an integer before you can do any math with it:

>>> attribute_index = int(attribute_index)
>>> attribute_index
2
>>> attribute_index -= 1
>>> attribute_index
1

Now let's try to change the second tuple:

>>> attributes = [
... ("strength", 0), ("health", 0), ("wisdom", 0), ("dexterity", 0)]
>>> attributes[attribute_index]
('health', 0)
>>> attributes[attribute_index][1] += 42
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

The error message is pretty clear, you cannot replace items of a tuple.
You can either to switch to nested lists

[["strength", 0], ["health", 0], ...]

or replace the entire tuple with a new one:

>>> name, value = attributes[attribute_index]
>>> attributes[attribute_index] = name, value + 42
>>> attributes
[('strength', 0), ('health', 42), ('wisdom', 0), ('dexterity', 0)]

However, I think the pythonic way is to use a dictionary. If you want the 
user to input numbers you need a second dictionary to translate the numbers 
into attribute names:

>>> attributes = dict(attributes)
>>> lookup = {1: "strength", 2: "health", 3: "wisdom", 4: "dexterity"}
>>> while True:
... index = input("index ")
... if not index: break
... amount = int(input("amount "))
... name = lookup[int(index)]
... attributes[name] += amount
...
index 1
amount 10
index 2
amount 20
index 3
amount 10
index 2
amount -100
index
>>> attributes
{'dexterity': 0, 'strength': 10, 'health': -38, 'wisdom': 10}

Personally I would ask for attribute names directly.

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


Re: [Tutor] Indexing a list with nested tuples

2011-08-02 Thread bob gailer

On 8/2/2011 10:44 PM, Alexander Quest wrote:

Hi guys- I'm having a problem with a list that has nested tuples:

attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0), 
("dexterity", 0)]


I've defined the list above with 4 items, each starting with a value 
of 0. The player
enters how many points he or she wants to add to a given item. The 
selection menu
is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the 
"selection" variable is actually
1 more than the index location of the intended item. So I have the 
following code:


print("Added ", points, "to ", attributes[selection-1][0], "attribute.")

My intent with this is to say that I've added this many points 
(however many) to the
corresponding item in the list. So if the player selects "1", then 
selection = 1, but I subtract
1 from that (selection -1) to get the index value of that item in the 
list (in this case 0). Then I
have [0] to indicate that I want to go to the second value within that 
first item, which is the
point value. I get an error saying that list indices must be integers, 
not strings. I get a similar

error even if I just put attributes[selection][0] without the minus 1.

Also, it seems that the tuple within the list cannot be modified 
directly, so I can't add points to the original value of "0" that all 
4 items start with. Is there a way to keep this nested list with
tuples but be able to modify the point count for each item, or will it 
be better to create a dictionary or 2 separate lists (1 for the names 
"Strength, Health, Wisdom, Dexterity" and one
for their starting values "0,0,0,0")? Any suggestions/help will be 
greatly appreciated!!!

Thanks for inquiring. Some guidelines about questions:

1 - show us more code. in this case specifically how you obtain user input.
2 - show the complete traceback

What does the error message tell you? Why would selection be a string 
rather than an integer? This has to do with how you obtain selection 
from the user. What


Why did you expect to be able to alter the value of a tuple element? 
Tuples are immutable! Use a list instead.


HTH

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] Indexing a list with nested tuples

2011-08-02 Thread Alexander Quest
Hi guys- I'm having a problem with a list that has nested tuples:

attributes = [("strength", 0), ("health  ", 0), ("wisdom  ", 0),
("dexterity", 0)]

I've defined the list above with 4 items, each starting with a value of 0.
The player
enters how many points he or she wants to add to a given item. The selection
menu
is 1 - strength; 2 - health; 3 - wisdom; 4- dexterity. So the "selection"
variable is actually
1 more than the index location of the intended item. So I have the following
code:

print("Added ", points, "to ", attributes[selection-1][0], "attribute.")

My intent with this is to say that I've added this many points (however
many) to the
corresponding item in the list. So if the player selects "1", then selection
= 1, but I subtract
1 from that (selection -1) to get the index value of that item in the list
(in this case 0). Then I
have [0] to indicate that I want to go to the second value within that first
item, which is the
point value. I get an error saying that list indices must be integers, not
strings. I get a similar
error even if I just put attributes[selection][0] without the minus 1.

Also, it seems that the tuple within the list cannot be modified directly,
so I can't add points to the original value of "0" that all 4 items start
with. Is there a way to keep this nested list with
tuples but be able to modify the point count for each item, or will it be
better to create a dictionary or 2 separate lists (1 for the names
"Strength, Health, Wisdom, Dexterity" and one
for their starting values "0,0,0,0")? Any suggestions/help will be greatly
appreciated!!!

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


Re: [Tutor] Indexing a List of Strings

2011-05-18 Thread spawgi
Agreed that your original sequences are 1000 char long. But it helps to
understand the problem better if you can give examples with smaller strings.
Please can you post smaller examples? This will also help you test your code
on your own inputs.

On Wed, May 18, 2011 at 5:40 AM, Spyros Charonis wrote:

> Greetings Python List,
>
> I have a motif sequence (a list of characters e.g. 'EAWLGHEYLHAMKGLLC')
> whose index I would like to return.
> The list contains 20 strings, each of which is close to 1000 characters
> long making it far too cumbersome to display an example.
> I would like to know if there is a way to return a pair of indices, one
> index where my sequence begins (at 'E' in the above case) and
> one index where my sequence ends (at 'C' in the above case). In short, if
> 'EAWLGHEYLHAMKGLLC' spans 17 characters is it possible
> to get something like 100 117, assuming it begins at 100th position and
> goes up until 117th character of my string. My loop goes as
> follows:
>
> for item in finalmotifs:
> for line in my_list:
> if item in line:
> print line.index(item)
>
> But this only returns a single number (e.g 119), which is the index at
> which my sequence begins.
>
> Is it possible to get a pair of indices that indicate beginning and end of
> substring?
>
> Many thanks
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a List of Strings

2011-05-17 Thread Alan Gauld


"Spyros Charonis"  wrote


for item in finalmotifs:
   for line in my_list:
   if item in line:
   print line.index(item)

But this only returns a single number (e.g 119), which is the index 
at which

my sequence begins.

Is it possible to get a pair of indices that indicate beginning and 
end of

substring?



print line.index(item)+len(item)

Presumably since its matching item the end index will be len(item)
characters later? Or am I missing something?

Alan G. 



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


[Tutor] Indexing a List of Strings

2011-05-17 Thread Spyros Charonis
Greetings Python List,

I have a motif sequence (a list of characters e.g. 'EAWLGHEYLHAMKGLLC')
whose index I would like to return.
The list contains 20 strings, each of which is close to 1000 characters long
making it far too cumbersome to display an example.
I would like to know if there is a way to return a pair of indices, one
index where my sequence begins (at 'E' in the above case) and
one index where my sequence ends (at 'C' in the above case). In short, if
'EAWLGHEYLHAMKGLLC' spans 17 characters is it possible
to get something like 100 117, assuming it begins at 100th position and goes
up until 117th character of my string. My loop goes as
follows:

for item in finalmotifs:
for line in my_list:
if item in line:
print line.index(item)

But this only returns a single number (e.g 119), which is the index at which
my sequence begins.

Is it possible to get a pair of indices that indicate beginning and end of
substring?

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