Hello guys I am working on a prolog game and I need to write a piece of code that will:
Take a number (ArmyNo) from the user.
Take an X coordinate
Take an Y coordinate.
Then I have a list that is named TempBoard and it looks like this:
([
(1,1,-,-),(1,2,-,-),(1,3,-,-),(1,4,-,-),
(2,1,-,-),(2,2,-,-),(2,3,-,-),(2,4,-,-),
(3,1,-,-),(3,2,-,-),(3,3,-,-),(3,4,-,-),
(4,1,-,-),(4,2,-,-),(4,3,-,-),(4,4,-,-)
]).
before I add this (X,Y,w,ArmyNO) to the list I first want to check it if it is already there.
I attempted to do that by using this code but it does not seem to work properly:
%#######Got the number####
repeat,
%Get Cordinates X & Y.
writelist( [TempBoard,'select coordinates for the horizontal axis 1 to 4 to place your soldier Mr. Human',nl]),
read(X),
writelist(['select coordinates for the vertical axis 1 to 4 to place your soldier Mr. Human',nl]),
read(Y),
%Check if they are in the list.
(
member( (X,Y,w,ArmyNo),TempBoard ) ->
( replace((X,Y,w,ArmyNo),TempBoard,NewBoard) ) ;
(
writelist(['selected positions are not available in the table Mr.Human',nl]) , fail
)
).
%%
(X, Y, w, ArmyNo)
cannot be unified with any member of your example list because w doesn't unify with -. You may have meant W.
Related
I need to interpolate two variables written in the 3D grid on another 3D grid. I tried the inverse distance method, but I get only two values that do not represent the distribution on the original grid, assigned to each point of the new grid. Here is an example of my code:
text=text[pstart:pend]
x=[]
y=[]
z=[]
for line in text:
coords=line.split()
x.append(float(coords[2])) #coordinates of the new grid
y.append(float(coords[1]))
z.append(float(coords[0]))
Xg=np.asarray([x,y,z])
# Gather mean flow data
xd=[]
yd=[]
zd=[]
cd=[]
rhod=[]
with open(meanflowdata,'rb') as csvfile:
spamreader=csv.reader(csvfile, delimiter=',')
for row in spamreader:
if len(row)>2:
xd.append(float(row[0])) #coordinates and values of the source file
yd.append(float(row[1]))
zd.append(float(row[2]))
cd.append(float(row[3]))
rhod.append(float(row[4]))
Xd=np.asarray([xd,yd,zd])
Zd=np.asarray([cd,rhod])
leafsize = 20
print "# setting up KDtree"
invdisttree = Invdisttree( Xd.T, Zd.T, leafsize=leafsize, stat=1 )
print "# Performing interpolation"
interpol = invdisttree( Xg.T )
c=interpol.T[0]
rho=interpol.T[1]
As far as I could check, the problem lies when I call the invdisttree function, which does not work properly. Does someone have an idea or an alternative method to suggest for the interpolation?
Where do interpol.T[0], interpol.T[1] come from,
where did your Invdisttree come from ?
This
on SO has
invdisttree = Invdisttree( X, z ) -- data points, values
interpol = invdisttree( q, nnear=3, eps=0, p=1, weights=None, stat=0 )
In your case X could be 100 x 3, z 100 x 2,
query points q 10 x 3 ⟶ interpol 10 x 2.
(invdisttree is a function, which you call to do the interpolation:
interpol = invdisttree( q ...) . Is that confusing ?)
I am a novice at geospatial coding. I have two lists made that contain 3 string items in each. I am working with the arcpy clip function and need to clip the items in list 1 by the items in list 2 using a for function. I cannot figure out how to call a variable in list one and list two and then iterate the function to call the next variables.
I have tried defining new variables for the lists based on the variable created in the for statement.
lst1= ['boulder_OpenSpace.shp', 'lafayette_OpenSpace.shp', 'louisville_OpenSpace.shp']
lst2= ['sites53242bldBuff_3000.shp', 'sites430183lafBuff_3000.shp', 'sites329231louBuff_3000.shp']
for a in [range(len(lst1))]:
town= lst1(a)
buff= lst2(a)
arcpy.Clip_analysis(town, buff, 'focused'+a)
print(a, 'clipped to buffered sites')
I expect the function to select an item in list 1 and clip it to each item in list 2, then move on to the 2nd item in list 1 to again clip to each item in list 2. I am aware I am not close with this approach, but am having trouble figuring out where to start.
It sounds like what you want is for every town in list1, you want to clip it to each buff in list2. That can be done with a nested loop or using a function in the itertools library.
nested loop
lst1= ['boulder_OpenSpace.shp', 'lafayette_OpenSpace.shp', 'louisville_OpenSpace.shp']
lst2= ['sites53242bldBuff_3000.shp', 'sites430183lafBuff_3000.shp', 'sites329231louBuff_3000.shp']
for t,town in enumerate(lst1):
for b,buff in enumerate(lst2):
output = "focused_{}_{}.shp".format(t,b)
arcpy.Clip_analysis(town, buff, output)
print("{} clipped to {}'.format(town, buff)
itertools.product()
import itertools
lst1= ['boulder_OpenSpace.shp', 'lafayette_OpenSpace.shp', 'louisville_OpenSpace.shp']
lst2= ['sites53242bldBuff_3000.shp', 'sites430183lafBuff_3000.shp', 'sites329231louBuff_3000.shp']
for i,(town, buff) in enumerate(itertools.procduct(lst1, lst2)):
output = "focused_{}.shp".format(i)
arcpy.Clip_analysis(town, buff, output)
print("{} clipped to {}'.format(town, buff)
I am working with a list of points in python 2.7 and running some interpolations on the data. My list has over 5000 points and I have some repeating "x" values within my list. These repeating "x" values have different corresponding "y" values. I want to get rid of these repeating points so that my interpolation function will work, because if there are repeating "x" values with different "y" values it runs an error because it does not satisfy the criteria of a function. Here is a simple example of what I am trying to do:
Input:
x = [1,1,3,4,5]
y = [10,20,30,40,50]
Output:
xy = [(1,10),(3,30),(4,40),(5,50)]
The interpolation function I am using is InterpolatedUnivariateSpline(x, y)
have a variable where you store the previous X value, if it is the same as the current value then skip the current value.
For example (pseudo code, you do the python),
int previousX = -1
foreach X
{
if(x == previousX)
{/*skip*/}
else
{
InterpolatedUnivariateSpline(x, y)
previousX = x /*store the x value that will be "previous" in next iteration
}
}
i am assuming you are already iterating so you dont need the actualy python code.
A bit late but if anyone is interested, here's a solution with numpy and pandas:
import pandas as pd
import numpy as np
x = [1,1,3,4,5]
y = [10,20,30,40,50]
#convert list into numpy arrays:
array_x, array_y = np.array(x), np.array(y)
# sort x and y by x value
order = np.argsort(array_x)
xsort, ysort = array_x[order], array_y[order]
#create a dataframe and add 2 columns for your x and y data:
df = pd.DataFrame()
df['xsort'] = xsort
df['ysort'] = ysort
#create new dataframe (mean) with no duplicate x values and corresponding mean values in all other cols:
mean = df.groupby('xsort').mean()
df_x = mean.index
df_y = mean['ysort']
# poly1d to create a polynomial line from coefficient inputs:
trend = np.polyfit(df_x, df_y, 14)
trendpoly = np.poly1d(trend)
# plot polyfit line:
plt.plot(df_x, trendpoly(df_x), linestyle=':', dashes=(6, 5), linewidth='0.8',
color=colour, zorder=9, figure=[name of figure])
Also, if you just use argsort() on the values in order of x, the interpolation should work even without the having to delete the duplicate x values. Trying on my own dataset:
polyfit on its own
sorting data in order of x first, then polyfit
sorting data, delete duplicates, then polyfit
... I get the same result twice
I have a number given in this form 623746xyz3 and i have to code a Python script that prints on screen all numbers that can be created with the combination of all values (from 0 to 9 ) that x,y,z can assume.
Can someone help me?
If those xyz are always next to each other, you can just loop from 0 to 999 and replace that part of the string accordingly.
s = "623746xyz3"
for xyz in range(1000):
sxyz = s.replace('xyz', str(xyz))
print int(sxyz)
In case the x, y, and z can be more 'spread out', you will need three nested loops:
for x in range(10):
sx = s.replace('x', str(x))
for y in range(10):
sxy = sx.replace('y', str(y))
for z in range(10):
sxyz = sxy.replace('z', str(z))
print int(sxyz)
(And in case you do not know the 'variables' a priori, you will first need to find the non-digit characters and use a recursive approach to replace them.)
My first idea:
for x in range(0, 10)
for y in range(0. 10)
for z in range (0, 10)
print 6*1000000000+2*100000000+3*10000000+7*1000000+4*100000+6*10000+x*1000+y*100+z*10+3
Doing a vertical reflection symmetry on dots on a screen, please consider the following.
scrWidthCM=40
originals={{14.2065, 10.609, 0.974938}, {19.5653, 6.92721, 0.974938},
{30.4607,17.4802, 0.974938}, {27.4621, 10.0393, 0.974938},
{15.915, 20.4278,0.974938}, {28.6921, 5.2132, 1.53205},
{27.0317, 24.8346,1.53205}, {20.8853, 18.8588, 1.53205}}
Where each sublist corresponds to : {Xcoordinate,Ycoordinate,radius}
Applying the symmetrical transfer to each of the 8 points :
(scrWidthCM - #[[1]]) & /#originals
How could I replace the first value of each sublists rather than simply compute its reflected X coordinate ?
Assuming that you want to directly modify your originals object:
originals[[All, 1]] = scrWidthCM - originals[[All, 1]]
If you want a copy, then use:
{scrWidthCM - #, ##2} & ### originals