python - Counting first appearance of a field in a CSV file -
using format of following csv file:
pos id name 1 0001l01 50293 2 0002l01 128864 3 0003l01 172937 4 0004l01 12878 5 0005l01 demo 6 0004l01 12878 7 0004l01 12878 8 0005l01 demo
i want include in dictionary: [id], {pos, name, firsttime
} firsttime
corresponds position id
first appears in csv file. instance id = 0005l01
have: [0005l01],{5,demo,5},{8,demo,5}
i have managed store [id], {pos,name}
struggling firsttime
. far i've got:
# csv reader, save list dlist=[] row in reader: # store non empty lines if any(row): dlist.append(row) d={} row in dlist: d.setdefault(row[1],[]).append([row[0],row[2]])
it's easier if compute firsttime
first, , fill dictionary:
# csv reader, save list dlist=[] row in reader: # store non empty lines if any(row): dlist.append(row) firsttime={} row in dlist: if row[1] not in firsttime: firsttime[row[1]] = row[0] d={} row in dlist: d.setdefault(row[1],[]).append([row[0],row[2],firsttime[row[1]]])
Comments
Post a Comment