我会使用一个列表,就像您附加项目一样,并使用一个单独的字典来获取数字:
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)
m = 1
for n in EmptyArray: # no need to enumerate as numbers should increment only on uniques
if n not in d: # this is a unique
d[n] = [m] # put number in dictionary, with key as val.
m += 1 # so increment
elif n == '': # if it's blank (and if it is already in the dict)
d[n].append(m) # append new number, as blanks always increment
m += 1 # increment
for name in EmptyArray: # looping less as only want to get names
if name == '': # if it's a blank, we want to pop out the first item in the list.
a2 = str('{},NULL'.format(d[name].pop(0)))
else: # otherwise we just index the first item.
a2 = str('{},{}'.format(d[name][0], name))
以上应该可以工作。
这样你就不必循环太多,逻辑更清晰一些。希望这有助于缩短计算时间,但也可以为您提供正确的数字。
在循环遍历 EmptyArray 时,我们总是附加到一个列表,这可能不是最好的方法,因为你只需要一个空白列表,并且不使用列表直接访问一个项目会更快 -但是当列表只有 1 项时,我怀疑会有很大的不同。
对于空格,我们需要使用一个列表,因此它可以接受多个数字(每个空格一个)。为了得到每个空格的正确数字,我们只需要在每次遇到空格时从列表前面弹出数字 - 这应该对应于最初分配给该空格的数字。
我们也不需要创建一个新列表,只需在 EmptyArray 上重复我们的循环即可。
为了提高效率
我们可以完全摆脱 EmptyArray 上的第二个循环(我没有看到任何额外的逻辑使它成为必要),只需对第一个循环执行以下操作:
for n in EmptyArray: # no need to enumerate as numbers should increment only on uniques
if n == '' : # blanks always increment - no need to store as we treat them as new
a2 = str('{},NULL'.format(m))
m += 1 # so increment
elif n not in d: # this is unique, so add to dict and increment
d[n] = m # add to dict for future reference
a2 = str('{},{}'.format(m, n))
m += 1 # increment
else: # it's in the dict, and not a blank, so grab from dict.
a2 = str('{},{}'.format(d[n], n))
这样我们就摆脱了大量的循环,程序的效率应该会大大提高。它还消除了存储空白对应数字的需要,从而节省了额外的精力 - 我们可以使用对数字的直接引用来处理其他所有事情。
所以我做了两个函数来尝试复制你想要的:
EmptyArray = ['',1,2,3,'',1,'',3,2,'',1]
m = 1
a2 = ''
def f1 (EmptyArray, d, m, a2):
for n in EmptyArray: # no need to enumerate as numbers should increment only on uniques
if n not in d: # this is a unique
d[n] = [m] # put number in dictionary, with key as val.
m += 1 # so increment
elif n == '': # if it's blank (and if it is already in the dict)
d[n].append(m) # append new number, as blanks always increment
m += 1 # increment
for name in EmptyArray: # looping less as only want to get names
if name == '': # if it's a blank, we want to pop out the first item in the list.
a2 += str('{},NULL\n'.format(d[name].pop(0)))
else: # otherwise we just index the first item.
a2 += str('{},{}\n'.format(d[name][0], name))
print(a2)
def f2 (EmptyArray, d, m, a2):
for n in EmptyArray: # no need to enumerate as numbers should increment only on uniques
if n == '' : # blanks always increment - no need to store as we treat them as new
a2 += str('{},NULL\n'.format(m))
m += 1 # so increment
elif n not in d: # this is unique, so add to dict and increment
d[n] = m # add to dict for future reference
a2 += str('{},{}\n'.format(m, n))
m += 1 # increment
else: # it's in the dict, and not a blank, so grab from dict.
a2 += str('{},{}\n'.format(d[n], n))
print(a2)
f1(EmptyArray, {}, m, a2)
f2(EmptyArray, {}, m, a2)
这里是调用的输出:
1,NULL
2,1
3,2
4,3
5,NULL
2,1
6,NULL
4,3
3,2
7,NULL
2,1
1,NULL
2,1
3,2
4,3
5,NULL
2,1
6,NULL
4,3
3,2
7,NULL
2,1
分别是f1和f2的时间:
3.4341892885662415e-05
1.5789376039385022e-05
所以 f2 比 f1 花费的时间少了一半。