Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect from list by index, but keep other associated items in list
    text
    copied!<p>If I have a 2dlist of points like this... </p> <pre><code>myList=[['ab','0_3','-1','1'],['bm','2_1','-3','2'],['am','4_1','-1','3'],...]] </code></pre> <p>where <code>'-1','1'</code> for example are x, y coordinates (the last 2 columns) and I want display just part of the list with its index like this...</p> <pre><code>[0] ab # I don't want to display the other list items, just the index and first col [1] bm [2] am </code></pre> <p>...so the user can select one of them by index number to make a point of origin. I figured I could enumerate as such...</p> <pre><code>for row in enumerate(myList): i, a= row col= (i, a[0]) newList.append(col) print cols </code></pre> <p>But once I ask the user to select one ie. user selects '0' and that sets the variable <code>origin='ab'</code>, how can I get the x,y (or [2],[3]) columns associated with origin to use as the origin coordinates (I need to be able to compare to the rest of the list)?</p> <p>Using this method, can I somehow use the variable assigned to the selected point ie. <code>origin = ab</code>, and then get it's x,y and assign them to x1, y1... Because enumerate gives the 2 tuples (i, a) and appends them to <code>newList</code>, is that all I have in <code>newList</code>, or can I also append the other columns but not display them? I hope my explanation is clear enough... I only have badly butchered code atm</p> <hr> <p>So I finally got most of this working with...</p> <pre><code>import csv myList=[] try: csvr = open('testfile.csv','r') next(csvr, None) theList = csv.reader(csvr) for row in theList: myList.append(row) for row in enumerate(myList): i, a = row print i, a[0] except IOError: print 'Error!!!' try: choice = raw_input("Select a set: ") # Can also enter 'e' if choice=='e' print 'exiting' else: pass user_choice = myList[int(choice)] name, id, x, y= user_choice print name, id, return float(x), float(y) except: print 'error' </code></pre> <p>It prints as expected and I can return x, y now which is great but it just keeps prompting me to enter a number. Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload