我有三列GDMID,重叠后的百分比和重量。
我试过的代码:
prevFieldValue = ''
counter = 1
def GetDuplicateCounter(myFieldValue):
global prevFieldValue
global counter
if myFieldValue == prevFieldValue:
counter += 1
else:
counter = 1
prevFieldValue = myFieldValue
return counter
GetDuplicateCounter(!GDMID!)
但是在重量值下,较高的值有时仅代表“1”。 所以请纠正我,我错了。我知道这个问题太绿了,但需要任何帮助。
答案 0 :(得分:0)
由于我不知道您的数据文件的格式是什么以及哪个模块更适合阅读和编辑它,我将使用数组/列表来说明如何在python中执行此操作。然后,您可以将代码转换为您将要使用的任何模块。
根据问题和评论,我将使用以下列表来说明:
data = [
[22505910, 100, ""],
[22505911, 100, ""],
[22505920, 0, ""],
[22505920, 100, ""],
[22505921, 100, ""],
[22505922, 0, ""],
[22505922, 100, ""],
[22505925, 45, ""],
[22505925, 55, ""],
[22505926, 100, ""]]
每个子列表代表数据表中的一行,每个子列表中的项目1,2和3代表" GDMID","重叠后的百分比1和#34 ;和"体重"分别是数据表的列条目。每行的起始权重值为空,由""
表示。
以下是为每行提供权重的代码:
prevFieldValue = ""
def GetDuplicateCounter(myData):
global prevFieldValue
for row in myData:
indx = myData.index(row)
if row[0] == prevFieldValue:
if row[1] > myData[indx-1][1]:
myData[indx][2] = 2
elif row[1] < myData[indx-1][1]:
myData[indx][2] = 1
myData[indx-1][2] = 2
else:
myData[indx][2] = 1
prevFieldValue = row[0]
return myData
#I tried the above code on the following data:
data1 = [
[22505910, 100, ""],
[22505911, 100, ""],
[22505920, 0, ""],
[22505920, 100, ""],
[22505921, 100, ""],
[22505922, 0, ""],
[22505922, 100, ""],
[22505925, 45, ""],
[22505925, 55, ""],
[22505926, 100, ""]]
data2 = [
[22505910, 100, ""],
[22505911, 100, ""],
[22505920, 50, ""],
[22505920, 10, ""],
[22505921, 100, ""],
[22505922, 0, ""],
[22505922, 100, ""],
[22505925, 65, ""],
[22505925, 55, ""],
[22505926, 100, ""]]
print GetDuplicateCounter(data1)
print GetDuplicateCounter(data2)
# Results:
result1 = [
[22505910, 100, 1],
[22505911, 100, 1],
[22505920, 0, 1],
[22505920, 100, 2],
[22505921, 100, 1],
[22505922, 0, 1],
[22505922, 100, 2],
[22505925, 45, 1],
[22505925, 55, 2],
[22505926, 100, 1]]
result2 = [[22505910, 100, 1],
[22505911, 100, 1],
[22505920, 50, 2],
[22505920, 10, 1],
[22505921, 100, 1],
[22505922, 0, 1],
[22505922, 100, 2],
[22505925, 65, 2],
[22505925, 55, 1],
[22505926, 100, 1]]