Reputation: 309
Lists of lists are kicking my butt. I've done a fair amount of research trying to find how to update the nth element of the nth list in a list of lists python data structure. I see a lot of advice on updating the nth element or combining lists into an existing list, but nothing about updating a particular element within a nested list.
My actual code reads through a file searching for tid:somestring then adds tid:somestring to a set. I now want to loop through the set and make that value the 0th y for each occurrence. My next step in the code will add another search item to the 1st y for the corresponding tid value that I previously placed in the 0th y position (and so on, for the remaining search values). I plan to increment with a counter to move the index value for x...Essentially, I need update a two dimensional array. There are 9 values that will be on the y axis. The x axis is dependent on the number of unique instances of tid:somestring, which is 10,000 or more for each file.
How do I update/insert or otherwise change the nth item of the nth list in my list of lists?
samlfile="2013-08-18 22:47:55,248 tid:b7c31fbac DEBUG" #I have a file with 10000+ "tid"
tidset = set()
tidcount = len(tidset)
tidassert = [[0 for x in range(tidcount)] for y in range(9)]
xcount = 0
for line in samlfile:
if "tid:" in line:
str=line
tid = re.search(r'(tid:.*?)(?= )', str)
if tid.group() not in tidset:
tidset.add(tid.group())
tid = tid.group()
for tid in tidset:
tidassert[xcount][0] = tid
xcount = xcount + 1
print tidassert
Upvotes: 0
Views: 1249
Reputation: 947
try:
_list[n][n] = 'new val'
except IndexError:
print 'No such index'
Upvotes: 1