473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

debugging a simple code.

31
I have a function that won't work is it possible for someone to debug it for me?...i've been trying to play with everything...
Expand|Select|Wrap|Line Numbers
  1. def reverse(L):
  2.     """Given a list, return a new list that is the same as the original list except that its members are in reverse order, without changing the original.""" 
  3.     M=list()
  4.     for i in L:
  5.         M.append(L[i])
  6.         M.reverse()
  7.     return M
Feb 9 '09 #1
2 1306
boxfish
469 Expert 256MB
These lines are the problem:
Expand|Select|Wrap|Line Numbers
  1. for i in L:
  2.     M.append(L[i])
i contains items from the list, not list indices. The code should be
Expand|Select|Wrap|Line Numbers
  1. for i in L:
  2.     M.append(i)
However, a more compact way to copy a list is with a slice:
Expand|Select|Wrap|Line Numbers
  1. M = L[:]
I hope this is helpful.
Feb 9 '09 #2
bvdet
2,851 Expert Mod 2GB
To give you some alternatives, you can create a reversed list using the slice operator and the range function in a list comprehension. You could also use a for loop in combination with range() instead of the comprehension.
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [0,1,2,3,4,5,6,7,8,9]
  2. >>> anotherList = aList[::-1]
  3. >>> aList
  4. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  5. >>> anotherList
  6. [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  7. >>> anotherList2 = [aList[i] for i in range(len(aList)-1, -1, -1)]
  8. >>> anotherList2
  9. [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  10. >>> 
Feb 9 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steve Wark | last post by:
I have a Windows 2003 Web server on which I was debugging remotely with no problems. I then moved this server to a different domain and now remote debugging will not work, the error is; ...
1
by: Jeffrey Frabutt | last post by:
Hey... I'm reletively new to ASP.NET and am having some frustrating debugging issues. Everything is configured correctly for me to debug my VERY simple one-page application. That said, after just...
23
by: keyser_Soze | last post by:
I have MS Visual Studio 2003 on Windows XP Pro. I have IIS running on this machine and I am trying to debug some existing code which has both ASP and ASP.NET components. When I try and launch...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
0
by: Joseph S. | last post by:
hi all, debugging PHP applications interactively is possible, easy and free. I am talking about PHPEclipse and using it for debugging over several scripts or debugging through a session. Since I...
20
by: krypto.wizard | last post by:
Is there any editor or IDE in Python (either Windows or Linux) which has very good debugging facilites like MS VisualStudio has or something like that. I like SPE but couldn't easily use winPDP....
4
by: Greg | last post by:
I have a very simple Visual Basic .NET 2003 class library project ClassLibrary1 configured to run an external program (in this case C:\WINDOWS\system32\wscript.exe), in the Debugging Configuration...
0
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored...
33
by: fmassei | last post by:
Hello! I made a short piece of code that I find very useful for debugging, and I wanted to ask you if it is correct, somehow acceptable or if I simply reinvented the wheel. To deal with some bad...
2
by: Jeff | last post by:
hi asp.net 2.0 vista business 32bit I have placed a break point with the Page_Load event of a UserControl. The break point wasn't triggered.. I have also put Debug.WriteLine("Page_Load"); in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.