This question already has answers here:
Does "IndexError: list index out of range" when trying to access the N'th item mean that my list has less than N items?
(7 answers)
Closed 5 years ago.
def calcDistance(x1, y1, x2, y2):
distance = sqrt((x1-x2)**2 + (y1-y2)**2)
return distance
def make_dict():
return defaultdict(make_dict)
# Capture 1 input from the command line.
# NOTE: sys.argv[0] is the name of the python file
# Try "print sys.argv" (without the quotes) to see the sys.argv list
# 1 input --> the sys.argv list should have 2 elements.
if (len(sys.argv) == 2):
print "\tOK. 1 command line argument was passed."
# Now, we'll store the command line inputs to variables
myFile = str(sys.argv[1])
else:
print 'ERROR: You passed', len(sys.argv)-1, 'input parameters.'
quit()
# Create an empty list:
cities = []
# Create an empty dictionary to hold our (x,y) coordinate info:
myCoordinates = {}
# Open our file:
myFile = '%s.csv' % (myFile)
with open(myFile, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
# Only read rows that do NOT start with the "%" character.
if (row[0][0] != '%'):
# print row
id = int(row[0])
isHome = int(row[1])
x = float(row[2])
y = float(row[3])
myCoordinates[id] = {'x': x, 'y': y}
# print myCoordinates[id]['x']
# print myCoordinates[id]['y']
if (isHome == 1):
# Store this id as the home city
homeCity = id
cities.append(id)
print homeCity
print cities
# Create a TSP tour.
# VERSION 1 -- Using range() and for() loops:
myTour = []
for i in range(homeCity, len(cities)+1):
myTour.append(i)
for i in range(1, homeCity+1):
myTour.append(i)
print myTour
# VERSION 2 -- Using only range()
'''
firstPart = range(homeCity, len(cities)+1)
secondPart = range(1, homeCity+1)
myTour = firstPart + secondPart
print myTour
'''
tau = defaultdict(make_dict)
for i in cities:
# print "distance[%d][%d] = 0" % (i, i)
tau[i][i] = 0
for j in range(i+1, len(cities)+1):
# print "distance[%d][%d] > 0" % (i, j)
tau[i][j] = calcDistance(myCoordinates[i]['x'], myCoordinates[i]['y'], myCoordinates[j]['x'], myCoordinates[j]['y'])
# print "distance[%d][%d] = distance[%d][%d]" % (j, i, i, j)
tau[j][i] = tau[i][j]
# FIXME -- Edit the code below...
# Calculate the total distance of our TSP solution:
i = myTour[i]
for myIndex in range(1, len(myTour)+1):
j = myTour[myIndex]
print j
Function to calculate cost based on distance. Need to be modified.
def cost(rate,j):
cost = rate * j
cost = cost(1000,j)
print cost
Also I need to calculate cost based on distance traveled. with myIndex i am getting an error of list index out of range. I am not knowing what exactly is going there. The j is like total distance calculated.
List in python have 0 based index. If you add n elements to a list the indexes are from 0 to n-1. But you are running the loop from 1 to n. So, it getting list index out of range error.
You should do this-
for myIndex in range(0, len(myTour)):
j = myTour[myIndex]
print(j)
If you are getting list index out of range error then change the loop where you are getting the error and accessing a list using 1-based indexing, from range(1,len(some_list)+1) to range(0,len(some_list)). Or you can simply write range(len(some_list)). When there is no start value passed in the range function it starts from 0 by default.
To calculate cost try this -
for myIndex in range(0, len(myTour)):
j = myTour[myIndex]
cost = rate * j
print(cost)
Set the value of rate before starting the loop.
Related
So I have this code counting the number of comparisons used to sort by QuickSort method under three different ways in choosing the pivot point. Choose the first element in the array; choose the final element in the array; choose the median of the first, middle (n-th element if len(array)=2n), and final element.
The input file contains all of the integers between 1 and 10,000 (inclusive, with no repeats) in unsorted order.
This code however only outputs the result of the first case correctly, and outputs zero as the result of the later two cases, I have no idea which part caused this please help!
This is the code
from pathlib import Path
file_path = Path("c:/Users/M/Desktop/Al/")
inFile = open(file_path / "QuickSort.txt", 'r')
with inFile as f:
lines = [x.split() for x in f] # a list of lists
f.close()
tempList = zip(*lines) # a list of tuples := transposed lines
tempList = list(tempList)
tempList = tempList[0] # a list of strings
A = map(int,tempList) # a list of integers
def choosePivot(alist,first,last,pivotID):
if pivotID == 'first':
pass
if pivotID == 'last':
(alist[first], alist[last]) = (alist[last], alist[first])
elif pivotID == 'middle':
mid = (last-first)//2 + first
listTemp = [alist[first], alist[last], alist[mid]]
listTemp.sort()
if listTemp[1] == alist[first]:
pivotIndex = first
elif listTemp[1] == alist[last]:
pivotIndex = last
else:
pivotIndex = mid
(alist[first], alist[pivotIndex]) = (alist[pivotIndex], alist[first])
def partition(alist, first, last):
pivotVal = alist[first] # initialise pivot as the first element
leftmark = first+1
for rightmark in range(first+1,last+1):
if alist[rightmark] < pivotVal:
(alist[leftmark],alist[rightmark]) = (alist[rightmark],alist[leftmark])
leftmark = leftmark + 1
(alist[leftmark-1],alist[first]) = (alist[first],alist[leftmark-1])
return leftmark-1 # partition point := where pivot finally occupies
def quickSort(alist,first,last,pivotID):
numComp = last -first
if last <= first:
return (alist, 0)
else:
choosePivot(alist,first,last,pivotID)
splitpoint = partition(alist,first,last)
(Lsorted,numCompL) = quickSort(alist, first, splitpoint-1, pivotID)
(Rsorted,numCompR) = quickSort(alist, splitpoint+1, last, pivotID)
numComp = numComp + numCompL + numCompR
return (alist, numComp)
def main(fileName):
pivotList = ['first', 'middle', 'last']
for pivotID in pivotList:
A = list(fileName)
(A, n) = quickSort(A,0,len(A)-1,pivotID)
print ("number of comparisons: %d", n)
if __name__ == '__main__':
# unit test
# main('test.txt')
main(A)
That's because (A, n) = quickSort(A,0,len(A)-1,pivotID) sorts the list. The first algo returns correct results, subsequent ones return 0 because the list is already sorted by now.
To avoid this, create a copy of the main list using copy = A[:] and then pass a new copy in each iteration. Something like
def main(fileName):
pivotList = ['first', 'middle', 'last']
mainList = list(filename)
for pivotID in pivotList:
A = mainList[:]
(A, n) = quickSort(A,0,len(A)-1,pivotID)
print ("number of comparisons: %d", n)
I have a .txt-file called ecc.txt. It contains more than 8000 lines of numbers. I want to count the average of every 360 lines in that file.
Here is the code:
import math
f = open(r'ecc.txt').read()
data = []
for line in data:
sum = 0
for i in range (len(data)):
if i % 360 != 0:
sum = sum + ecc[i]
else:
average = sum / 360
print(average)
sum = 0
When I am running it, nothing happens. I didn't get any results. The code just running and end without any result.
Is there something wrong with this code?
Thank you.
avg_dict = {}
with open('ecc.txt') as f:
data = f.read().split(' ')
sum = 0
i = 0
for str_number in data:
sum += int(str_number)
i += 1
if i % 360 == 0:
avg_dict[i] = sum/360
sum = 0
I've assumed that your file text has an empty space as separator. Otherwise, you can change the sep value in the split method. If there is not separator change data as:
data = list(f.read())
You code would work with some changes:
import math
data=[]
with open(r'ecc.txt') as f:
for i in f:
data.append(int(i))
for line in data:
sum = 0
for i in range (len(data)):
if i%360 !=0:
sum = sum + ecc[i]
else:
average = sum/360
print(average)
sum=0
Be aware though, that this code doesn't include values for each 360th element (i guess it's not a problem for an average) and also you don't have average for last elements
My problem:
I am trying to compare two elements from two different arrays but the operator is not working.
Code Snippet in question:
for i in range(row_length):
print(f"ss_record: {ss_record[i]}")
print(f"row: {row[i + 1]}")
#THIS IF STATEMENT IS NOT WORKING
if ss_record[i] == row[i + 1]:
count += 1
#print()
#print(f"row length: {row_length}")
#print(f"count: {count}")
if count == row_length:
print(row[0])
exit(0)
What I have done: I tried to print the value of ss_record and row before it runs through the if statement but when it matches, count doesn't increase. I tried storing the value of row in a new array but it bugs out and only store the array length and first 2 value of row and repeats those values every next instance.
What I think the issue: I think the issue with my code is that row is being read from a CSV file and is not being converted into an integer as a result, it appears they are the same but one is an integer while the other is a string.
Entire Code:
import csv
import sys
import re
from cs50 import get_string
from sys import argv
def main():
line_count = 0
if len(argv) != 3:
print("missing command-line argument")
exit(1)
with open(sys.argv[1], 'r') as database:
sequence = open(sys.argv[2], 'r')
string = sequence.read()
reader = csv.reader(database, delimiter = ',')
for row in reader:
if line_count == 0:
row_length = len(row) - 1
ss_record = [row_length]
for i in range(row_length):
ss_record.append(ss_count(string, row[i + 1], len(row[i + 1])))
ss_record.pop(0)
line_count = 1
else:
count = 0
for i in range(row_length):
print(f"ss_record: {ss_record[i]}")
print(f"row: {row[i + 1]}")
#THIS IF STATEMENT IS NOT WORKING
if ss_record[i] == row[i + 1]:
count += 1
if count == row_length:
print(row[0])
exit(0)
#ss_count mean the # of times the substring appear in the string
def ss_count(string, substring, length):
count = 1
record = 0
pos_array = []
for m in re.finditer(substring, string):
pos_array.append(m.start())
for i in range(len(pos_array) - 1):
if pos_array[i + 1] - pos_array[i] == length:
count += 1
else:
if count > record:
record = count
count = 1
if count > record:
record = count
return record
main()
Values to use to reproduce issue:
sequence (this is a text file) = AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG
substring (this is a csv file) =
name,AGATC,AATG,TATC
Alice,2,8,3
Bob,4,1,5
Charlie,3,2,5
Gist of the CSV file:
The numbers beside Alice means how many times a substring(STR/Short Tandem Repeat) appears in a row in the string(DNA sequence). In this string, AGATC appears 4 times in a row, AATG appears 1 time in a row, and TATC appears 5 times in a row. For this DNA sequence, it matches Bob and he outputted as the answer.
You were right, when you compare ss_record[i] == row[i + 1]: there is a type problem, the numbers of ss_record are integers while the numbers of the row are strings. You may acknowledge the issue by printing both ss_record and row:
print("ss_record: {}".format(ss_record)) -> ss_record: [4, 1, 5]
print("row: {}".format(row)) -> row: ['Alice', '2', '8', '3']
In order for the snippet to work you just need to change the comparison to
ss_record[i] == int(row[i + 1])
That said, I feel the code is quite complex for the task. The string class implements a count method that returns the number of non-overlapping occurrences of a given substring. Also, since the code it's working in an item basis and relies heavily in index manipulations the iteration logic is hard to follow (IMO). Here's my approach to the problem:
import csv
def match_user(dna_file, user_csv):
with open(dna_file, 'r') as r:
dna_seq = r.readline()
with open(user_csv, 'r') as r:
reader = csv.reader(r)
rows = list(reader)
target_substrings = rows[0][1:]
users = rows[1:]
num_matches = [dna_seq.count(target) for target in target_substrings]
for user in users:
user_matches = [int(x) for x in user[1:]]
if user_matches == num_matches:
return user[0]
return "Not found"
Happy Coding!
I am a beginner at Python, and will appreciate some explanation on the faults in my attempts. I'd also appreciate some help on how to proceed from here. I'll link the challenging statements first, and the verbose code block after.
I have defined a dictionary: loc_dictionary to receive key-value pairs.
The key is a user-inputted name and the value is a list of two values to represent latitude and longitude, also inputted by the user.
I am trying to find the difference between the latitude in two dictionary keys in a loop:
deltalat = loc_dictionary[i + 1][0] - loc_dictionary[i][0]
print(deltalat)
The above code is meant to access the variable in the next iteration minus the variable in the current iteration of the dictionary. It tells me that I cannot concatenate the string to int ([i + 1]).
TypeError: can only concatenate str (not "int") to str
Then, I tried another method using nested for loops:
for i in range(0, len(loc_dictionary)):
for j in range(1, len(loc_dictionary)):
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
print(deltalat)
This throws the following error:
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
KeyError: 1
Which I believe means that the dictionary key being referenced cannot be found. Using Jupyter, I got some more help on an error telling me dictionary keys do not support indexing. What should I do next? Kindly find the complete code below:
loc_dictionary = {}
# Method for accepting coordinates
def route():
count = 0
primary_loc_key = input('Please type in the name of first location.\n>>> ').strip()
primary_loc_value = [
eval(input('Please type in the latitude of \'' + primary_loc_key + '\'.\n>>> ').strip()),
eval(input('Please type in the longitude of \'' + primary_loc_key + '\'.\n>>> ').strip())]
loc_dictionary[primary_loc_key] = primary_loc_value
location_exit_loop = 'done'
loc_input_key = ''
loc_input_value = []
print('<<<Type \'Done\' as the location name when all locations have been inputted.>>>')
while loc_input_key.strip().lower() != location_exit_loop:
loc_input_key = input('Please type in the name of the ' + str(count + 1) + ' stop.\n>>> ').strip()
if loc_input_key.strip().lower() != location_exit_loop:
loc_input_value = [eval(input('Please type in the latitude of ' + loc_input_key + '.\n>>> ').strip()),
eval(input('Please type in the longitude of ' + loc_input_key + '.\n>>> ').strip())]
else:
loc_input_value = None
loc_dictionary[loc_input_key] = loc_input_value
count += 1
if (count - 1) < 2:
print('You have a single stop.')
else:
print('You have ', count - 1, 'stops to be calculated.')
del loc_dictionary['done']
print(loc_dictionary)
return loc_dictionary
# Calculate through loop
def coordinates():
latitude = 0
longitude = 0
# loop through items in the loc_dictionary
for i in loc_dictionary:
latitude = loc_dictionary[i][0]
longitude = loc_dictionary[i][1]
print(i + '\nLatitude: ' + str(latitude) + '\nLongitude: ' + str(longitude))
# for i in loc_dictionary:
# deltalat = loc_dictionary[i + 1][0] - loc_dictionary[i][0]
# print(deltalat)
for i in range(0, len(loc_dictionary)):
for j in range(1, len(loc_dictionary)):
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
print(deltalat)
route()
coordinates()
Your first error
TypeError: can only concatenate str (not "int") to str
is probably being given because you are storing the numbers for your latitude & longitude values as strings. You can convert these to numbers so they can be added together using float()
eg
float(latitude1) - float(latitude2)
as for accessing the dictionary, the easiest way to iterate through a dict is like this:
for each key, value in dictionary:
print(key)
print(value)
Note that dictionaries in python are not normally ordered, however, so you are better off using an array, ie changing your code where you put the data from:
loc_dictionary[loc_input_key] = loc_input_value
to
loc_array += [[loc_name, loc_longitude, loc_latitude]]
and instead of assigning an empty value to the key 'done' and then deleting it, put the assignment behind an if statement that checks that the location name is not 'done'
Using an array will then allow you to loop through it as you are expecting to be able to in your code.
edit:
Looking at your code in more detail, you are correct in your comment that your numbers are stored as numbers. However, on your first loop attempt you have looped as so:
for i in loc_dictionary:
This is an iteration through the keys of the dictionary, and so i is a string, not an integer. Therefore you can't add 1 to it to try and get the next iteration. You should be able to see this as the previous loop will output the key along with the location numbers, not an integer
I've also updated my code so that the array assign actually works
I had to use #nosklo's solution which you can find here:
The first thing to be done was to change the loc_dictionary variable from a dictionary to a list as #Thering suggested:
loc_dictionary = []
primary_loc_list = []
Then, I type the new method:
# Method for accepting coordinates
def accept_coordinates():
count = 0
primary_loc_list.append(input('Please type in the name of first location.\n>>> ').strip())
primary_loc_list.append(eval(input('Please type in the latitude of \'' + primary_loc_list[0] + '\'.\n>>> ').strip()))
primary_loc_list.append(eval(input('Please type in the longitude of \'' + primary_loc_list[0] + '\'.\n>>> ').strip()))
# [[location_name, latitude, longitude]]
loc_dictionary.append(primary_loc_list)
location_exit_loop = 'done'
loc_input_key = ''
print('<<<Type \'Done\' as the location name when all locations have been inputted.>>>')
while loc_input_key.strip().lower() != location_exit_loop:
# for every time the loop runs, reset other_loc_list to empty
other_loc_list = []
loc_input_key = input('Please type in the name of the ' + str(count + 1) + ' stop.\n>>> ').strip()
if loc_input_key.strip().lower() != location_exit_loop:
other_loc_list.append(loc_input_key)
if loc_input_key.strip().lower() != location_exit_loop:
other_loc_list.append(eval(input('Please type in the latitude of ' + loc_input_key + '.\n>>> ').strip()))
other_loc_list.append(eval(input('Please type in the longitude of ' + loc_input_key + '.\n>>> ').strip()))
else:
loc_input_value = None
# append all the values in other_loc_list as a list to the loc_dictionary before running the loop again.
loc_dictionary.append(other_loc_list)
else:
break
count += 1
if count < 2:
print('You have a single stop.')
else:
print('You have ', count, 'stops to be calculated.')
return loc_dictionary
A dictionary key and corresponding list as the dictionary value didn't work. I found it better to create a new input list every loop for the name, latitude and longitude and append that to the master list:
while loc_input_key.strip().lower() != location_exit_loop:
# for every time the loop runs, reset other_loc_list to empty
other_loc_list = []
loc_input_key = input('Please type in the name of the ' + str(count + 1) + ' stop.\n>>> ').strip()
if loc_input_key.strip().lower() != location_exit_loop:
other_loc_list.append(loc_input_key)
if loc_input_key.strip().lower() != location_exit_loop:
other_loc_list.append(eval(input('Please type in the latitude of ' + loc_input_key + '.\n>>> ').strip()))
other_loc_list.append(eval(input('Please type in the longitude of ' + loc_input_key + '.\n>>> ').strip()))
else:
loc_input_value = None
# append all the values in other_loc_list as a list to the loc_dictionary before running the loop again.
loc_dictionary.append(other_loc_list)
Now, the function below takes an argument. That argument will be the list we want to get the values from.
prevs appends the argument to None. This is because the previous element at the start of a list doesn't exist.
nexts slices the argument and appends None to the end. The next item at the end of a list doesn't exist.
# function to get previous, next and current items in a list.
def next_and_previous(x):
prevs, item, nexts = tee(x,3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return zip(prevs, item, nexts)
In essence, it appears to divide a single list into three per iteration: prevs will serve the function of the previous list item,
item is the current item, and
nexts serves the function of the next item in the list.
Please see nosklo's answer for a proper explanation.
The function below is responsible for getting the variables and working on them:
# Calculate through loop
def calculate_distance():
count = 0
total_distance = 0
# loop through items in the loc_dictionary
for i in loc_dictionary:
latitude = i[1]
longitude = i[2]
print('\n' + i[0] + '\nLatitude: ' + str(latitude) + '\nLongitude: ' + str(longitude))
for prev, item, nxt in next_and_previous(loc_dictionary):
if count != len(loc_dictionary):
count += 1
# print('The next item is', nxt, 'and the current item is', item, 'and the previous item is', prev)
try:
lat2 = math.radians(nxt[1])
lat1 = math.radians(item[1])
lon2 = math.radians(nxt[2])
lon1 = math.radians(item[2])
...
Nesting lists allows the program to access a particular index within a list:
# loop through items in the loc_dictionary
for i in loc_dictionary:
latitude = i[1]
longitude = i[2]
Here is the implementation of the next_and_previous() function:
for prev, item, nxt in next_and_previous(loc_dictionary):
if count != len(loc_dictionary):
count += 1
# print('The next item is', nxt, 'and the current item is', item, 'and the previous item is', prev)
try:
lat2 = math.radians(nxt[1])
lat1 = math.radians(item[1])
lon2 = math.radians(nxt[2])
lon1 = math.radians(item[2])
Python allows a program to operate on called variables directly in some situations. lat2 is the next item at index 1 of the list. lat1 is the current item at index 1 one of the list. Remember, the name of the location is stored at index 0. We don't need to operate on that.
lon2 and lon1 do the same as above for the items at index 2.
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!
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
Try with the while break statements
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
I fixed the code so the simulations stop once the number of cells change is less than 0.0001 of the total cells. I had the while loop in the wrong place. Here's the code if anyone is interested in cellular automata.
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