Python - swapping lists?

Home > Computers & Internet > Programming & Design > Python - swapping lists?
lindseylindsey


We have an assignment in my beginning Python class and I am completely lost on it. I have asked some students and most of them were lost and the teacher gave me a very vague answer that did not help me at all.

THE ASSIGNMENT

"Write a function that is passed in a list and 2 index numbers (your function can assume these index numbers are between 0 and len() - 1, inclusive). Your function should swap the values at those index locations.

For example:

stuff = [10, 20, 30, 40]

swap(stuff, 0, 2)

print stuff

Would display:

[30, 20, 10, 40]"

I have no idea what i'm doing wrong but I'm getting

[10,10,30,40]

MY CODE:

def swap(list, indexA, indexB):

result =[]

sub=len(list)-1

for item in list:

if item == list[indexA]:

item=list[indexB]

if item == list[indexB]:

item=list[indexA]

result.append(item)

return result

def main():

test=[10,20,30,40]

print swap(test,0,1)

main()

----

I'm not trying to cheat and get my homework done by someone else, so can someone just explain what I am doing wrong or give me a much needed hint to point me in the right direction?

Thank you.

Dave
Dave

The problem is:

Both of your "if item == list[index...]:" lines run with each trip through the for loop.

On the first trip through the loop you get this:

for item in list:     ## item = 10

  if item == list[indexA]:     ## item == 10, list[indexA] == list[0] == 10, True

    item=list[indexB]     ## item = list[indexB] = 20

  if item == list[indexB]:     ## item == 20, list[indexB] == list[1] == 20, True

    item=list[indexA]     ## item = list[indexA] = list[0] = 10

  result.append(item)     ## item == 10, result == [10]

The second trip looks like this...

for item in list:     ## item = 20

  if item == list[indexA]:     ## item == 20, list[indexA] == list[0] == 10, False

    item=list[indexB]     ## does not execute

  if item == list[indexB]:     ## item == 20, list[indexB] == list[1] == 20, True

    item=list[indexA]     ## item = list[indexA] = list[0] = 10

  result.append(item)     ## item == 10, result == [10, 10]

So you need to come up with a way to keep that 2nd "if item==..." line from running in the first loop.


Other Questions & Answers

Whats The Best Dedicated Notebook Graphics?
Just Had A Few Questions. What Is The Best Dedicated Graphics For My HP DV7 4053CL. I Want A High Performance Hard. And Where Can I Buy It? ... Read more (3 answers)
Can you manually install a game by doing this...?
Before you ask no what i am doing isnt intentionally illegal if it is, my CD drive has broken. but anyway...can i manually install my game b ... Read more (3 answers)
I just want to know if installing a number of Wordpress sites on a single domain can affect the load time?
Hello,I just want to know if installing a number of Wordpress sites on a single domain can affect the load time of the website and its pages ... Read more (2 answers)
How To Upgrade Graphics On My HP DV7?
I Have An HP DV7 4053CL With ATI Radeon 4250 Dedicated Graphics. (This Notebook: ... Read more (3 answers)
Is this cpu worth the graphic chard?
I'm going to build a new pc and i wanna know: is a intel core i5-760 is worth a asus gtx 560?and is 600 watt enough?? it's recomen ... Read more (3 answers)