Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:27:36+00:00 2026-05-30T04:27:36+00:00

This is probably a simple question, but it’s driving me crazy! I have a

  • 0

This is probably a simple question, but it’s driving me crazy! I have a python code that performs cellular automata on a land use grid. I’ve made a dictionary of cell id: land use code imported from a text file. I’ve also import of the adjacent neighbors of each cell from a text file. For each cell in the nested loop, I pick out the highest value, count the highest value of the neighboring cells. If this value is greater than the processing cell and occurred more than 4 times, then I update the dictionary for that cell id. The land use codes are ranked in priority. You will see < 6 in the code below…6 is water and wetlands which I do not want to be changed. The first time I run the code, 7509 cells changed land use based on adjacent neighbors land uses. I can comment out the reading the dictionary text file and run it again, then around 5,000 cells changed. Run it again, then even less and so on. What I would like to do is run this in a loop until only 0.0001 of the total cells change, after that break the loop.

I’ve tried several times using iterators like “for r in range(999)—something big; If End_Sim > count: break”. But it breaks after the first one, because the count goes back to zero. I’ve tried putting the count = 0 inside the loop and it adds up…I want it to start back over every time so the number of cells gets less and less. I’m stump…hopefully this is trivial to somebody!

Here’s my code (it’s a clean slate…I’ve deleted my failed attempts to create the number of simulations loop):

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

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:27:37+00:00Added an answer on May 30, 2026 at 4:27 am

    use a while loop:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a simple question but I only have Linux to test this code on
This is probably a simple question that I am just missing but I have
This is probably a simple question, but i have been unable to find a
This is probably a very simple question but I have a confusion about the
Probably a simple question but one that eludes me nonetheless. I have a class:
This is probably a very simple question, but at this time I have myself
This is probably a simple question but I am not an ASP.NET developer and
This is probably a simple question but I can't seem to find the solution.
This is probably a simple question but I can't seem to figure out how
This is probably a simple question but I am developing a web app in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.