473,399 Members | 2,774 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,399 software developers and data experts.

Subtracting units in an array from themselves

I have a list of coordinates and am trying to ouptput the distance between each coordinate into a new list.

((x1,y1,z1),(x2,y2,z2),...)

End resulst

((x2-x1),(y2-y1),(z2-z1)),...

Thus far my strategy is to use the enumerate function to create a new array offset from the original array, and then subtract them.

Expand|Select|Wrap|Line Numbers
  1. #creates a list called dataList from and external coordinates file
  2. dataList = [[float(s) for s in item.strip().split(",")] for item in open("coordinates.txt").readlines()]
  3.  
  4. #creates an empty new array
  5. new_array=[]
  6.  
  7. #enumerates the list
  8. for i,points in enumerate(dataList):
  9.   x,y,z = points
  10.   x2,y2,z2 = dataList[i+1]
  11.   new_array.append((x2-x, y2-y, z2-z))
  12.  
  13. print new_array
  14.  
  15.  
I get the following error:
File "C:\Python27\coordinate scheme.py", line 9, in <module>
x2,y2,z2 = dataList[i+1]
IndexError: list index out of range

I'm new at Python and especially workining with nested lists and am stuck on where to go from here.
Can you make recommendations on how to address this? Not really sure what list index out of range refers too.
Thanks
Jun 29 '11 #1
1 2004
bvdet
2,851 Expert Mod 2GB
It appears that you have a list of lists, and each sublist has 3 elements of type float. If you use enumerate, each sublist is processed from the first to the very last. When the last sublist is processed and you attempt to extract the next element which does not exist (dataList[i+1]), you will receive the index error. You can use range(len(dataList)-1), add a try/except block, etc. to get around the error.
Jun 29 '11 #2

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

Similar topics

5
by: Grant Austin | last post by:
What would be the correct syntax for setting up a dynamic array of structs? Suppose you have a struct declared: struct relation { FILE * binFile; unsigned int numAttrs; struct attrList *...
7
by: Netocrat | last post by:
The code at the bottom illustrates the nature of a situation discussed on comp.lang.asm.x86. Basically an unsigned integer is being used as a negative index in a loop, and it works as though the...
2
by: Russ | last post by:
I know that python packages are available for representing physical units, but I am getting frustrated trying to find them and determine which is the best. Rather than just using conventional...
3
by: Martin Joergensen | last post by:
Hi, Just a small problem (about taken the previous unsigned value in a 1D- array and copying it to the current value). Suppose there's an outer loop over the line shown below, so...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
3
by: assgar | last post by:
Hi I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2) a display function on the HTML form and 3) a...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
1
by: assgar | last post by:
Hi I need help solving a porblem. I have a form that displays a checkbox, service code, description and dropdown with fees on each row. The fee_money and unit array only returns a...
3
by: nicekul | last post by:
I am having problems subtracting from unitsInStock for all records on my table, for instance; I have Quantity sold, Unit Price,UnitsInStock,Total sales, Stock Balance. I declared Dim num1 Dim...
29
by: MAx | last post by:
Hi, Im kinda stuck in a project at a point where i need an array to be converted to a integer using some kind of math. This board does not support functions like scanf, sscanf etc as it does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.