I am trying to cache a function and I’m getting an error that is pointing towards these lines of code:
result = [[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in list1]
filtered_inside_radius = [[1 if y <= radius and y > 0 else 0 for y in x] for x in result]
The code calculates the distance between points in two lists and then filters the results based on “radius” which is a streamlit slider.
I don’t see anything that would obviously be causing an error. Any thoughts?
Still stuck on this. The more precise error that is being generated is:
UserHashError: 'coords2'
I’ve since learned that lists are not hashable so I’ve converted them to the variables to tuples but it is still not working:
result = tuple([[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in
list1])
filtered_inside_radius = tuple([[1 if y <= radius and y > 0 else 0 for y in x] for x in result])
Okay I figured it out. But what I don’t understand is why it works. Changing the code to the below allowed me to successfully cache:
result = []
for coords2 in list1:
results2 = []
for coords1 in list2:
distance = geopy.distance.geodesic(coords1,coords2).km
results2.append(distance)
result.append(results2)
result = list([[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in list1])
filtered_inside_radius = []
for x in result:
results4 = []
for y in x:
if y <= radius and y > 0:
y = 1
results4.append(y)
else:
y = 0
results4.append(y)
filtered_inside_radius.append(results4)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.