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
Related
Let's say I have a List in Kotlin like:
val lst = MutableList<Int>()
If I want the last item, I can do lst.last(), but what about the second to last?
You can use the size of the list to compute the index of the item you want.
For example, the last item in the list is lst[lst.size - 1].
The second to last item in the list is lst[lst.size - 2].
The third to last item in the list is lst[lst.size - 3].
And so on.
Be sure the list is large enough to have an n'th to last item, otherwise you will get an index error
Use Collections Slice
val list = mutableListOf("one" , "two" , "three")
print(list.slice(1..list.size - 1))
This will give you the following result
[two, three]
Suppose you have 5 items in your val lst = MutableList<Int>() and you want to access the second last, it means listObject[3](list start with 0th index).
lst.add(101) - 0th Index
lst.add(102) - 1th Index
lst.add(103) - 2th Index
lst.add(104) - 3th Index
lst.add(105) - 4th Index
So you can try this
last[lst.size - 2] = 104 3rd Index
lst.size return 5, so if you use size - 1 it means you try to access last index 4th one, but when you use size - 2 it means you try to access the second last index 3rd.
Take secondLast element with null check,
fun <T> List<T>.secondLast(): T {
if (size < 2)
throw NoSuchElementException("List has less than two elements")
return this[size - 2]}
or use this, It simply return null if the index size does not match
myList.getOrNull(myList.lastIndex - 1)
drop first
lst.drop(1)
take from last
lst.takeLast(lst.size - 1 )
slice from..to
lst.slice(1..(lst.size-1) )
You may refer to this link https://kotlinlang.org/docs/collection-parts.html
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)
ex = ['$5','Amazon','spoon']
I want to re-order this list, website - item - price.
Can I assign the index, for instance, ex.index('Amazon') = 1?
I'd like the result to be ['Amazon','spoon','$5']
I found information on how to swap positions, but I would like to know if I can assign an index for each item myself.
You cannot assign an index to an item, but you can build a permuted list according to a permutation pattern:
ex = ['$5','Amazon','spoon']
order = [1, 2, 0]
ex_new = [ex[i] for i in order]
print(ex_new)
#['Amazon', 'spoon', '$5']
Alternatively, you can overwrite the original list in place:
ex[:] = [ex[i] for i in order]
print(ex)
#['Amazon', 'spoon', '$5']
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.
I am wondering if there is a way to transform a matrix of 2 columns into a multimap or list of list.
The first column of the matrix is an id (with possibly duplicated entries) and the 2nd column is some value.
For example,
if I have to following matrix
m <- matrix(c(1,2,1,3,2,4), c(3,2))
I would like to transform it into the following list
[[1]]
3,4
[[2]]
2
With base functions, you can do something like this:
tapply(m[,2], m[,1], `[`) # outputs an array
by(m, m[,1], function(m) m[,2]) # outputs a by object, which is a list
You could use plyr:
dlply(m, 1, function(m) m[,2]) # outputs a list
dlply(m, 1, `[`, 2) # another way to do it...