Q:

Python: Trouble organizing list values imported from .csv

I am having difficulty managing a large list Python2.7:
I imported a .csv with three columns: InputID, TargetID, and distance (between the two).
I have imported all of the values into a single list which reads:
['InputID1','TargetID1','Distance1','InputID1','TargetID2','Distance2',InputID1','TargetID3','Distance3',...]

So for each InputID, there are three corresponding TargetIDs.
What I am trying to do is group all records containing a similar InputID, and find the corresponding TargetID with the shortest distance. And ensure once found, no InputID/TargetID's would be repeated.
Here's what I have so far:
import csv

ids=[]     ##Unique InputID list##
e=[]       ##Empty .csv list##
f=open("D_Matrix.csv")
csv_f = csv.reader(f)
n=0

for row in csv_f:
    str_list0=str(row[0])
    str_list1=str(row[1])
    str_list2=str(row[2])
    while n>0:
        x=str_list0       ##InputID##
        y=str_list1       ##TargetID##
        z=str_list2       ##Distance##
        if not x in ids:
            ids.append(x)
        e.append(x)     ##Add Origin to list e##
        e.append(y)     ##Add Target to list e##
        e.append(z)     ##Add Distance to list e##
        break
    else:
        n+=1         ##Prevents column labels from being added to list

print e
f.close()

I am still quite new to python programming, so let me know if there is anything I can do to further clarify!
I greatly appreciate any comments/advice.
-Mailman

A:

If you need to have the flat list, then IMHO you should first read the data and build your list and after process the already created list.
The reading sequence would be like you have it now, with the exception of the ids[] update. Once you have loaded it, you can start processing. You need to create an array with the shortest distance and update it, meaning that you need to go twice through your values (plus however it takes to iterate through the other list).
If there's not reason to have the flat list, I'd build something like a dict of dicts:
import csv

ids = {}

f=open("D_Matrix.csv")
csv_f = csv.reader(f)
n=0

for row in csv_f:
    str_list0=str(row[0])
    str_list1=str(row[1])
    str_list2=str(row[2])

    if not str_list0 in ids.keys():
        ids[str_list0] = {}

    input_dict = ids[str_list0]

    if not str_list1 in input_dict.keys():
        input_dict[str_list1] = str_list2 # your value
    else:
        # we already have the key, check if the new value is shorter:
        #
        if input_dict[str_list1] > str_list2:
            input_dict[str_list1] = str_list2

# Now you have a dict like:
#
# {'InputID1': {'TargetID1' : minimum target distance}}
for input_key in ids.keys():
    for target_key in ids[input_key].keys():
        print "In: [%s], Tgt: [%s], Min distance: [%s]" % \
            (input_key, target_key, ids[input_key][target_key])

This has the advantage that you need to iterate only once O(n) plus lookups in the dict. 

