Python:How to do operations on a grid - python-2.7

For a word game(something similar to image provided),where inside a grid various letters are entered on different tile,enter image description here I had to create a 6*6 grid and then do operations on it like:
a)place the tile on a specific location in the grid and
b)return the location of any tile on the grid
c)determining the top scoring words in the grid
Till now I have managed to create the grid but i have no clue of how to place a tile on a specific grid or fetch the location of a tile on the grid.I have created the following grid:
grid = [[" _" for x in range(6)]]
for y in range(6):
list1 = []
for x in range(13):
if x%2 == 0:
list1.append("|")
else:
list1.append("_")
grid.append(list1)
for row in grid:
print("".join(row))
I am new to python and any help would be appreciated.

Did you try to run this piece of code? I don't think so because the indentation here is bad, so it won't work.
You should rename your variable list1 to row for comprehension
purpose : your variable names should always be as descriptive as
possible.
Here to initialize in a more compact way your grid you could do:
grid = [["_" if x%2==0 else "|" for x in range(13)] for x in range(6)]
To access a specific tile you can do:
grid[y][x]
In example, the following command would print the cell on the third row second column:
print(grid[3][2])
To set the value you can do:
grid[y][x] = value
But I think that you should see a course about learning Python before trying to do these things.
Google it and good luck!

Related

An expression for positioning an element

I’ve got a problem with creating a proper expression when it comes to a position of an image.
I do have this expression placed in position: thisComp.layer("TA1").transform.position
My goal is adding to this expression (thisComp.layer("TA1").transform.position)
An extra line which can move the whole image by 200 pixels down
I don’t know what to type in order to make it work or how to solve it. I’ve tried to do my research on expressions but so far unable to find any answers myself.
Thank you in advance
enter image description here
To add to Stib's comment above (I still don't have enough rep to comment). You can use the pickwhip tool to access the individual x and y properties. The pickwhip tool is the one with the spiral icon below the property values.
If I click on the pickwhip and drag it to the x value of my position, I get:
temp = transform.position[0];
[temp, temp]
If I then add a line between those and pickwhip the y value, I'll have:
temp = transform.position[0];
transform.position[1]
[temp, temp]
Then I can change it to:
x = transform.position[0];
y = transform.position[1] + 200;
[x, y]
This should also give you what you want.
Of course this could also be done to a seperate layer giving you:
x = thisComp.layer("TA1").transform.position[0];
y = thisComp.layer("TA1").transform.position[1] + 200;
[x, y]

Iterating over sublists in a sliding slope function gives index error

I'm rather new to Python so bear with me. I'm trying to write a bit of code that will calculate a sliding slope for each frame of a movie, where each frame is a sublist in a larger list. I want to calculate the slopes for every n number of values ("width" in the code) to produce a new list of lists with the slopes for each frame in their own sublist.
This code worked with six values in each sublist but when I run it with more numbers in each sublist it gives me the "list index out of range" error in reference to YSD.append(SlidingSlope(smoothedarrayX,array_y[:][k],3)).
Here's the code:
def SlidingSlope(array_x,array_y,width):
arrayofslopes=[]
for i in range(0, len(array_x)):
x=array_x[i:i+width]
y=array_y[i:i+width]
arrayofslopes.append(sp.stats.linregress(x,y))
return arrayofslopes
def YFramesSlopes(array_y):
YSlopedata=[]
for j in range(0,len(array_y)):
YSD=[]
YSlopedata.append(YSD)
for k in range(0,len(array_y[j])):
YSD.append(SlidingSlope(smoothedarrayX,array_y[:][k],3))
return YSlopedata
So I guess the overarching issue I'm struggling with is how to make this sliding slope function iterate over the values in the Y sublists using the same list of X values, if that makes sense. Any help is greatly appreciated.

Join strings from the same column in ´pandas´ using a placeholder condition

I have a series of data that I need to filter.
The df consists of one col. of information that is separated by a row with with value NaN.
I would like to join all of the rows that occur until each NaN in a new column.
For example my data looks something like:
the
car
is
red
NaN
the
house
is
big
NaN
the
room
is
small
My desired result is
B
the car is red
the house is big
the room is small
Thus far, I am approaching this problema by building a function and applying it to each row in my dataframe. See below for my working code example so far.
def joinNan(row):
newRow = []
placeholder = 'NaN'
if row is not placeholder:
newRow.append(row)
if row == placeholder:
return newRow
df['B'] = df.loc[0].apply(joinNan)
For some reason, the first row of my data is being used as the index or column title, hence why I am using 'loc[0]' here instead of a specific column name.
If there is a more straight forward way to approach this directly iterating in the column, I am open for that suggestion too.
For now, I am trying to reach my desired solution and have not found any other similiar case in Stack overflow or the web in general to help me.
I think for test NaNs is necessary use isna, then greate helper Series by cumsum and aggregate join with groupby:
df=df.groupby(df[0].isna().cumsum())[0].apply(lambda x: ' '.join(x.dropna())).to_frame('B')
#for oldier version of pandas
df=df.groupby(df[0].isnull().cumsum())[0].apply(lambda x: ' '.join(x.dropna())).to_frame('B')
Another solution is filter out all NaNs before groupby:
mask = df[0].isna()
#mask = df[0].isnull()
df['g'] = mask.cumsum()
df = df[~mask].groupby('g')[0].apply(' '.join).to_frame('B')

How to use the dimension of a python matrix in a loop

I am working with a matrix, lets call it X, in python.
I know how to get the dimension of the matrix using X.shape but I am interested specially on using the number of rows of the matrix in a for loop, and I dont know how to get this value in a datatype suitable for a loop.
For example, imagine tihs simple situation:
a = np.matrix([[1,2,3],[4,5,6]])
for i in 1:(number of rows of a)
print i
How can I get automatically that "number of rows of a"?
X.shape[0] == number of rows in X
A superficial search on numpy will lead you to shape. It returns a tuple of array dimensions.
In your case, the first dimension (axe) concerns the columns. You can access it as you access a tuple's element:
import numpy as np
a = np.matrix([[1,2,3],[4,5,6]])
# a. shape[1]: columns
for i in range(0,a.shape[1]):
print 'column '+format(i)
# a. shape[0]: rows
for i in range(0, a.shape[0]):
print 'row '+format(i)
This will print:
column 0
column 1
column 2
row 0
row 1

Remove blanks at ends of DataVisualization chart x axis

I am using Microsoft's DataVisualization.Charting.Chart, and I have integer values along the X axis, using line-style graphs. However, the chart is adding an extra blank item at the beginning and end of the x-axis, with no value labels to explain what they are.
How can I remove these empty items and make the lines go right up to the ends?
Use the IsMarginVisible property of the xaxis. I believe that will do the trick.
To test this, I changed one of my own charts to be a line chart and then set the value in the code:
ReactivityChart.ChartAreas(0).AxisX.IsMarginVisible = False
Tell me if this is what you were hoping to get or if I have totally misunderstood the question:
(note that I do not have a high enough rep to post this image)
http://www.rectorsquid.com/chartmargintest.gif
You should set the Maximum and Minimum properties in ChartArea.AxisX, e.g. :
this.chart1.ChartAreas[0].AxisX.Minimum = 0; // if your minimum X = 0
this.chart1.ChartAreas[0].AxisX.Maximum = 100; // if your maximum X = 100
In this way, your chart area will show only the values between Minimum and Maximum.