The update function was never getting called because nest1[i]
was an integer, but the value you were checking against was a string. Types are very important.
# This example create a single column dataset.
headers = ['col1', 'col2', 'col3']
list1= [
[1, 30, 68],
[2, 23, 133],
[3, 25, 65],
[4, 23, 145],
[5, 22, 2],
[5, 31, 8],
[5, 32, 30],
[6, 11, 3],
[6, 20, 241],
[7, 22, 2],
[7, 19, 202],
[8, 19, 2],
[8, 24, 241],
[8, 13, 30],
[9, 18, 19],
[10, 13, 4]]
mylist = system.dataset.toPyDataSet(system.dataset.toDataSet(headers, list1))
listOfRowValues=[]
for row in mylist:
listOfRowValues.append(row[0])
print (row[0],row[1],row[2])
# create a dictionary
failCodeDesc = {10: "Ch1",
11: "Ch2",
12: "Ch3",
13: "Ch4",
18: "Ch1",
19: "Ch2",
20: "Ch3",
21: "Ch4",
22: "Ch1",
23: "Ch2",
24: "Ch3",
25: "Ch4",
30: "Ch1",
31: "Ch2",
32: "Ch3",
33: "Ch4"}
newData = []
#------------------------------------------------------
def update(json, nestId, **newKeys):
index = -1
for i, obj in enumerate(json):
if obj['NestID'] == nestId:
index = i
if index >= 0:
currentValue = json[index]
currentValue.update(newKeys)
#------------------------------------------------------
nest1 = mylist.getColumnAsList(0) #channel list
failCode1 = mylist.getColumnAsList(1) #failcode list
counts1 = mylist.getColumnAsList(2) #amount of failcodes list
# get list of values using map
channels1 = list(map(failCodeDesc.get, failCode1))
#------------------------------------------------------
for data in mylist:
for i, (w,x,y,z) in enumerate(zip(nest1, failCode1,counts1,channels1)):
#checking for duplicates in the first column (duplicate NestIDs)
if ((i != 0) and (nest1[i]== nest1[i-1])): #check if the firs value of first column (nestID) is the same as the previous one
updates = {channels1[i]:str(counts1[i])}
update(newData, str(nest1[i]), **updates)
else: #else, if there's unique numbers at the first column (unique NestIDs)
newData.append({'NestID': str(nest1[i]), channels1[i]:str(counts1[i])})
break
#------------------------------------------------------
for i in range(0, len(newData)):
print(newData[i])
>>>
(1, 30, 68)
(2, 23, 133)
(3, 25, 65)
(4, 23, 145)
(5, 22, 2)
(5, 31, 8)
(5, 32, 30)
(6, 11, 3)
(6, 20, 241)
(7, 22, 2)
(7, 19, 202)
(8, 19, 2)
(8, 24, 241)
(8, 13, 30)
(9, 18, 19)
(10, 13, 4)
{'NestID': '1', 'Ch1': '68'}
{'NestID': '2', 'Ch2': '133'}
{'NestID': '3', 'Ch4': '65'}
{'NestID': '4', 'Ch2': '145'}
{'NestID': '5', 'Ch2': '8', 'Ch1': '2', 'Ch3': '30'}
{'NestID': '6', 'Ch2': '3', 'Ch3': '241'}
{'NestID': '7', 'Ch2': '202', 'Ch1': '2'}
{'NestID': '8', 'Ch2': '2', 'Ch4': '30', 'Ch3': '241'}
{'NestID': '9', 'Ch1': '19'}
{'NestID': '10', 'Ch4': '4'}