I have a column titled CompanyNam and I need to extract the ownership information. For example, if CEZ is the first company then, I assign to it number 1, then Sokolovska is the next number, etc. If CEZ appears again in some other row, it will get number 1. It is important to note that if I have NULL in the column I assign a different number to every NULL row entry. I need the numbers corresponding to the CompanyNam output. I have the following code:
EmptyArray = []
d = {}
newlist = []
for gFeat in GeneratorLayer.getFeatures():
Owner = gFeat.attributes()[gProvider.fieldNameIndex('CompanyNam')].toString()
A = ([str(i) for i in Owner]) #convert from PyQt4.QtCore.QString to normal string
B = ''.join(A)
EmptyArray.append(B)
for m, n in enumerate(EmptyArray):
if n not in d:
d[n] = [m+1]
newlist.append({n: d[n]})
if n == '':
d[n] = [m+1]
newlist.append({n: d[n]}) #Every NULL gets a new number
for names in newlist:
for o, p in names.iteritems():
if o == '':
a2 = str('{},NULL'.format(p))
elif o != '':
a2 = str('{},{}'.format(p,o)
I then use a2 in further steps. The code runs fine for a column with 60-100 rows but for larger columns, the computational times are really high. Can you suggest any way in which I can re-write this code, keeping the logic? The output looks like this:
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[9],Sokolovska
[10],International
[11],ENERGOTRANS,
[12],Alpiq
[13],Mittal Steel
[14],United
[1],CEZ
[1],CEZ
[17],Dalkia.....
It would be even nicer to have the numbering [1 ], [2], [3] and not [1 ], [9], [10], but I haven't figure out how to do that.