循环直到改变的单元数量可以忽略不计

时间:2012-02-19 15:56:59

标签: python loops

这可能是一个简单的问题,但它让我发疯!我有一个python代码,在土地使用网格上执行细胞自动机。我已经制作了一个单元格id的字典:从文本文件导入的土地使用代码。我还从文本文件导入每个单元格的相邻邻居。对于嵌套循环中的每个单元格,我选出最高值,计算相邻单元格的最高值。如果此值大于处理单元格并且发生的次数超过4次,那么我将更新该单元格id的字典。土地使用代码按优先顺序排列。你会看到<下面的代码中有6个... 6是我不想改变的水和湿地。我第一次运行代码时,7509个单元基于相邻的邻居土地使用改变了土地使用。我可以注释读取字典文本文件并再次运行,然后大约5,000个单元格更改。再次运行,然后再少运行等等。我想要做的是循环运行,直到只有0.0001的总单元格改变,然后打破循环。

我已经多次尝试使用迭代器,例如“for r in range(999)--- big big;如果End_Sim> count:break”。但它在第一个之后就会中断,因为计数会回到零。我已经尝试将count = 0放在循环中并且它加起来...我希望它每次都重新开始,因此单元格的数量变得越来越少。我很残忍......希望这对某些人来说是微不足道的!

这是我的代码(这是一个干净的名单......我删除了我创建模拟循环次数的失败尝试):

import sys, string, csv

#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict =  dict()
reader = csv.reader(text_file, delimiter='\t')
for line in reader:
    FID_GC_dict[line[0]] = int(line[1])
text_file.close()

#Importing neighbor list file for each FID value
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb")
Entries = Neighbors_file.readlines()
Neighbors_file.close()

Neighbors_List = map(string.split, Entries)

#print Neighbors_List

#creates a list of the current FID
FID = [x[0] for x in Neighbors_List]

#Calculate when to end of one sweep
Tot_Cells = len(FID)
End_Sim = int(0.0001*Tot_Cells)

gridList = []
for nlist in Neighbors_List:
   row = []
   for item in nlist:
       row.append(FID_GC_dict[item])
   gridList.append(row)
#print gridList

#Performs cellular automata rules on land use grid codes

i = iter(FID)
count = 0
for glist in gridList:
    Cur_FID = i.next()
    Cur_GC = glist[0]
    glist.sort()
    lr_Value = glist[-1]
    if lr_Value < 6:
        tie_LR = glist.count(lr_Value)
        if tie_LR >= 4 and lr_Value > Cur_GC:
           FID_GC_dict[Cur_FID] = lr_Value
           #print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID]
           count += 1
print count

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

使用while循环:

cnt_total = 1234 # init appropriately
cnt_changed = cnt_total
p = 0.001

while (cnt_changed  > cnt_total*p):
  # your code here
  # remember to update the cnt_changed variable

答案 1 :(得分:1)

尝试使用while break语句

initialization stuff
while(1):
    ... 
    if x < 0.0001:
        break 
    ... 

http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

答案 2 :(得分:-1)

我修复了代码,因此一旦细胞数量变化小于总细胞的0.0001,模拟停止。我把while循环放在了错误的地方。如果有人对细胞自动机感兴趣,这是代码。

import sys, string, csv

#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict =  dict()
reader = csv.reader(text_file, delimiter='\t')
for line in reader:
    FID_GC_dict[line[0]] = int(line[1])
text_file.close()

#Importing neighbor list file for each FID value
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb")
Entries = Neighbors_file.readlines()
Neighbors_file.close()
Neighbors_List = map(string.split, Entries)
#print Neighbors_List

#creates a list of the current FID
FID = [x[0] for x in Neighbors_List]
#print FID

#Calculate when to end the simulations (neglible change in land use)
tot_cells = len(FID)
end_sim = tot_cells
p = 0.0001

#Performs cellular automata rules on land use grid codes
while (end_sim > tot_cells*p):
    gridList = []
    for nlist in Neighbors_List:
        row = []
        for item in nlist:
            row.append(FID_GC_dict[item])
        gridList.append(row)
    #print gridList

    i = iter(FID)
    count = 0
    for glist in gridList:
        Cur_FID = i.next()
        Cur_GC = glist[0]
        glist.sort()
        lr_Value = glist[-1]
        if lr_Value < 6:
            tie_LR = glist.count(lr_Value)
            if tie_LR >= 4 and lr_Value > Cur_GC:
                FID_GC_dict[Cur_FID] = lr_Value
                print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID]
                count += 1
    end_sim = count            
    print count