question about a program

L

Logan Butler

question about an assignment:
[4, 10, 15, 18]

this is my code:

def places(x, y):
return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the next
three items it needs to
 
A

Andreas Waldenburger

question about an assignment:
[4, 10, 15, 18]

this is my code:

def places(x, y):
return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the next
three items it needs to

<http://docs.python.org/tutorial/datastructures.html>

list.index(x)
Return the index in the list of the first item whose value is x. It
is an error if there is no such item.

/W
 
R

Richard Thomas

question about an assignment:

[4, 10, 15, 18]

this is my code:

def places(x, y):
    return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the next
three items it needs to

Try this instead:

def places(x, y):
return [i for i, v in enumerate(x) if v == y]
 
C

Chris Rebert

question about an assignment:
[4, 10, 15, 18]

this is my code:

def places(x, y):
   return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the next
three items it needs to

Your loop variable is v, but your expression `x.index(y)` does not use
v at all, and hence its value is invariant over the course of the
loop.

Cheers,
Chris
 
A

Andreas Waldenburger

question about an assignment:
places("home sweet home is here",' ')
[4, 10, 15, 18]

this is my code:

def places(x, y):
   return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the
next three items it needs to

Your loop variable is v, but your expression `x.index(y)` does not use
v at all, and hence its value is invariant over the course of the
loop.
Nice catch, but the "if (v==y)" clause nullifies that in any case.

The real reason is that list.index(x) returns the index of the *first*
occurrence of x, so its invariant by default.

/W
 
S

Steven D'Aprano

question about an assignment:
[4, 10, 15, 18]

this is my code:

def places(x, y):
return [x.index(y) for v in x if (v == y)]

so far I'm only getting
[4, 4, 4, 4]

so the first value is correct, it is just not iterating on to the next
three items it needs to


Not every tool is a hammer, not every list builder needs to be a list
comp, and not every function needs to be a one-liner. The simplest way to
deal with this is to do an explicit loop.

Also, your names "x" and "y" are misleading. It is conventional to expect
x and y to be numeric values, so it is best to pick more descriptive
names.

def places(s, sub):
"""Return indexes into string s where non-overlapping copies
of substring sub is found, or [-1] if not found at all.
"""
n = len(sub)
start = 0
indexes = []
try:
while True:
i = s.index(sub, start)
indexes.append(i)
start = i + n
except ValueError:
if not indexes: indexes = [-1]
return indexes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top