I've got a loop that is supposed to select features and keep looping until it is no longer selecting new features
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","polygon")
previousselectcount = -1
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
while True:
#selectCount = arcpy.GetCount_management("StreamT_StreamO1")
mylist = []
with arcpy.da.SearchCursor("antiRivStart","ORIG_FID") as mycursor:
for feat in mycursor:
mylist.append(feat[0])
liststring = str(mylist)
queryIn1 = liststring.replace('[','(')
queryIn2 = queryIn1.replace(']',')')
arcpy.SelectLayerByAttribute_management('StreamT_StreamO1',"ADD_TO_SELECTION",'OBJECTID IN '+ queryIn2 )
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","StreamT_StreamO1","","ADD_TO_SELECTION")
previousselectcount = selectcount
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
print str(selectcount), str(previousselectcount)
if selectcount == previousselectcount:
break
By my reckoning, once it starts print the name number twice it should stop, but it doesn't, its keeps print "15548 15548" over and over again. Is it ingnoring the break or is the if condition not being met?
I've also tried with
while selectcount != previousselectcount:
but this gave me the same result
Variables in Python are dynamic. Just because you initialise previousselectcount as an integer doesn't mean it will be one when you call previousselectcount = selectcount. You can feel free to get rid of that line.
If you replace:
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
With:
selectcount = int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
For both lines you'll be comparing the integer values instead of whatever the equality operator is comparing for the object.
Even better, why not write a function to do it for you:
def GetCount():
return int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
Save yourself repeating yourself.
Related
I am trying to sort a set of data in to 2 separate lists, fulltime and parttime. But it doesn't seem to be working. Can somebody point to where I'm getting this wrong?
data = [(['Andrew'], ['FullTime'], [38]),
(['Fred'], ['PartTime'], [24]),
(['Chris'], ['FullTime'], [38])]
def sort(var1, datadump):
positionlist = []
for b in range(0, len(datadump)):
temp2 = datadump[b][1]
if (temp2 == var1):
positionlist.append(datadump[b])
return (positionlist)
FullTimeList = sort("FullTime", data)
PartTimeList = sort("PartTime", data)
print(FullTimeList)
print(PartTimeList)
This is solved by altering
if (temp2 == var1):
to
if (temp2[0] == var1):
This is because the elements within each tuple are lists holding a string, not the strings themselves.
This problem could also be solved using two list comprehensions:
FullTimeList = [x for x in data if x[1][0] == 'FullTime']
PartTimeList = [x for x in data if x[1][0] == 'PartTime']
Not an answer: just a suggestion. Learn how to use the python debugger.
python -m pdb <pythonscript.py>
In this case, set a breakpoint on line 9
b 9
Run the program
c
When it breaks, look at temp2
p temp2
It tells you
['FullTime']
Look at var1
p var1
It tells you
'FullTime'
And there is your problem.
You'll get a better understanding if you name your variables and functions with descriptive names:
data = [(['Andrew'], ['FullTime'], [38]),
(['Fred'], ['PartTime'], [24]),
(['Chris'], ['FullTime'], [38])]
def filter_records(value, records):
result = []
for i in range(len(records)): # i and j are usual variable names for indices (b is not)
record = records[i]
name, work, hours = record # give names to the parts
if work[0] == value: # work[0] since the values are lists (no need for parenthesis)
result.append(record)
return result # no need for parenthesis
FullTimeList = filter_records("FullTime", data)
PartTimeList = filter_records("PartTime", data)
the pattern:
for i in range(len(records)):
record = records[i]
is an anti-pattern in Python - meaning that there is a better way to write it:
for record in records:
...
I am working on something that needs to make it's way through several levels of checking if conditions are met before either exiting all together or setting certain variables and then starting the loop over. My question revolves around how to jump back to the primary while loop from the internal for loop.
while True:
message = stomp.get
message = simplejson.loads(message.body)
if message[0]['fieldname1'] == False:
global ShutdownState
ShutdownState = True
break # Should leave the While loop all together
else:
for item in message[0]['fieldname2'][0]['fieldname2-1']
if item['fieldname2-1-1'] == True:
list1_new[len(list_new):] = [item['fieldname2-1-2']
list1-state = set(list1) == set(list1_new)
if list1-state == True:
continue # should reset the while loop
else:
list1 = list1_new # should print the new list1 and then reset the while loop
print list1
continue
It's not clear whether the sample code was meant to be representative of your whole loop, or just the beginning of it. If it was the whole thing, then there are lots of ways you can restructure it. Here's a first stab (note that are some typos in the code (e.g., list1-state rather than list1_state, and things like that), so I've had to adjust some things. You'll need to check whether it still matches up with your original code. (For more about this implementation of finding the first element in the list, and some alternatives, have a look at Python: Find in list.)
while True:
message = stomp.get
message = simplejson.loads(message.body)
# If the message doesn't match this criterion,
# we need to abort everything.
if not message[0]['fieldname1']:
global ShutdownState
ShutdownState = True
break
try:
# get the first item in message[0]['fieldname2'][0]['fieldname2-1']
# such item['fieldname2-1-1'] is true. Whether we
# find one and do this code, or don't and catch the
# StopIteration, we wrap back to the while loop.
item = next(x
for x in message[0]['fieldname2'][0]['fieldname2-1']
if item['fieldname2-1-1'])
list1_new[len(list_new),:] = item['fieldname2-1-2']
list1_state = (set(list1) == set(list1_new))
if not list1_state:
list1 = list1_new # should print the new list1 and then reset the while loop
print list1
except StopIteration:
# There was no such item.
pass
You might also clean this up by making it a do-while loop, but that's a less major factor. Based on Emulate a do-while loop in Python?, you could do something like:
def get_message():
message = stomp.get
return simplejson.loads(message.body)
message = get_message()
while message[0]['fieldname1']:
try:
# get the first item in message[0]['fieldname2'][0]['fieldname2-1']
# such item['fieldname2-1-1'] is true. Whether we
# find one and do this code, or don't and catch the
# StopIteration, we wrap back to the while loop.
item = next(x
for x in message[0]['fieldname2'][0]['fieldname2-1']
if item['fieldname2-1-1'])
list1_new[len(list_new),:] = item['fieldname2-1-2']
list1_state = (set(list1) == set(list1_new))
if not list1_state:
list1 = list1_new # should print the new list1 and then reset the while loop
print list1
except StopIteration:
# There was no such item.
pass
message = get_message()
global ShutdownState
ShutdownState = True
So I am trying to get a grasp on Hash Functions and how exactly they work. I have the following code but I keep getting an error when I try and run the code.
import sys
def part_one():
foo = open('input_table.txt')
for line in foo:
id, make, model, year = line.split(",")
print(make, model)
tuple_list = (make+model,)
return tuple_list
def hash_one(num_buffers, tuple_list):
#part_one()
# A being the first constant prime number to multiply by
# B being the prime number that we add to A*sum_of_chars
tuple_list = part_one()
A = 3
B = 5
count = 0
for item in tuple_list:
for char in item:
# sum_of_chars is the total of each letter in the word
count = ord(char)
count = count + tuple_list
index = ((A * sum_of_chars + B)) % num_buffers
return index
if __name__ == '__main__':
input_table = sys.argv[1]
num_buffers = int(sys.argv[2])
chars_per_buffer = int(sys.argv[3])
sys.argv[4] = 'make'
sys.argv[5] = 'model'
lst = []
for item in range(4, len(sys.argv)):
lst.append(sys.argv[item])
print(lst)
hash_one(lst)
What is wrong with my code that is causing the error? Can anyone help me?
1
You're calling hash() with no arguments, you have to hash something.
A hash of a number will just return the same number though, so it's not very interesting. It's for hashing things like strings.
2
part_one returns nothing, therefore when you call tuple_list = part_one(), it's value is set to None, and you can't iterate though it.
3
Passing in a list through an argument then overwriting it doesn't make any sense anyway. If you want to return a list then use a return statement.
4
It's odd to set argument variables in code, they're for reading things from the command line.
5
(Not an error, but...)
You can use a slice (lst = sys.argv[4:]) as an easier way to get a sub-section of a list.
index1 = 0
singlechar = []
def SINGLE_CHAR_VAR(filename):
firdict = vars_indents(filename)[0]
firtup_keys = firdict.keys()
firtup_val = firdict.values()
for keys in firtup_keys:
for values in firtup_val:
index = 0
for index in range(len(values)):
firvallist = firtup_val[index]
for item in firvallist:
if len(item[0]) == 1:
singlechar.append({'ERROR_TYPE': 'SINGLE_CHAR_VAR', 'LINE_NUMBER': str(keys),'COLUMN': str(item[1]),'INFO': str(item[0]),'SOURCE_LINE': str(lines[keys - 1])})
else:
continue
return singlechar
this is my code but there is no output produced or when i move around the return statement an empty list was produced. i was hoping it to give me a list of dictionaries as the output.
can somebody teach me how to fix this?
thank you
You should call the function at first to get the output.
SINGLE_CHAR_VAR(filename)
How can you expect a function to run without being called?
return != print. You need to do `print SINGLE_CHAR_VAR(filename)
return will send the value to whatever is calling that function. In this case the value/string is being returned, but you need to do something with it, hence the need for print. Alternatively, you can replace return with print if you want the function itself to print the output. However, in this case the value will not be passed along, and you won't be able to store it. It really just comes down to what works for you/what you want.
`
I think the problem is in the return statement. You should indent the return statement right below the outer for loop but not below the inner for loop. This is shown below:
def SINGLE_CHAR_VAR(filename):
firdict = vars_indents(filename)[0]
firtup_keys = firdict.keys()
firtup_val = firdict.values()
for keys in firtup_keys:
for values in firtup_val:
index = 0
for index in range(len(values)):
firvallist = firtup_val[index]
for item in firvallist:
if len(item[0]) == 1:
singlechar.append({'ERROR_TYPE': 'SINGLE_CHAR_VAR', 'LINE_NUMBER': str(keys),'COLUMN': str(item[1]),'INFO': str(item[0]),'SOURCE_LINE': str(lines[keys - 1])})
else:
continue
return singlechar # indent of return changed
I'm trying to ignore the urls that are blacklisted from my search. 'ltp_block' contains the data which contain different urls.
p = re.compile('href="(.*?)" rel="nofollow"')
url = "http://www.****.**" + p.findall(current)[0]
r = requests.get(url)
The above code is used to fetch different urls from 'ltp_block'. r.url defines the url in loop when called for.
for each_row in blacklist:
if(re.findall('\\b'+each_row[0]+'\\b', r.url, flags=re.IGNORECASE) != []):
print "found"
QUESTION - Above 'for' loop works only ONCE. When 'check' becomes 1 OR the main loop chooses another url this second 'for' loop is simply skipped like it doesn't exist. Why ?
conn = sqlite3.connect('test.db')
c = conn.cursor()
blacklist = c.execute("SELECT `name` FROM `blacklist`")
check = 0
for row in ltp_block:
p = re.compile('versan')
current = ltp_block[check]
if(p.findall(current) != []):
p = re.compile('price=(.*?)&')
ltp = p.findall(current)[0]
del p
else:
p = re.compile('Gesa: (.*?) &')
ltp = p.findall(current)[0]
del p
p = re.compile('href="(.*?)" rel="nofollow"')
url = "http://www.****.**" + p.findall(current)[0]
r = requests.get(url)
for each_row in blacklist:
if(re.findall('\\b'+each_row[0]+'\\b', r.url, flags=re.IGNORECASE) != []):
print "found"
check = check + 1
Answer -
I had to recompile blacklist = c.execute("SELECT name FROM blacklist") each time
I placed it in the main 'for' loop and everything is working now
c.execute is returning an iterator. Iterators can only be iterated over once. As a simpler example, try this:
numbers = (x for x in xrange(10)) # a simple iterator
for number in numbers:
print number
print "Repeat"
for number in numbers:
print number
Only the first loop gives any output, as the iterator is exhausted and empty at the start of the second. Compare that with:
numbers = (x for x in xrange(10))
numbers = list(numbers) # turn the iterator into a list
for number in numbers:
print number
print "Repeat"
for number in numbers:
print number
Which works as expected. In your case, you want:
blacklist = list(c.execute("SELECT `name` FROM `blacklist`"))
The only way for a for loop to be "simply skipped like it doesn't exist" is if there is nothing to iterate over. If that is truly the case, the only explanation is that blacklist is empty. There simply is no other explanation if what you report is true.
Such a thing is very easy to prove. Add a print statement immediately before the loop, and print out the value of blacklist:
print "blacklist:", blacklist
for each_row in blacklist:
...