How to use two conditions in an if statement - python

I have the following equation:
result=[(i,j,k) for i in S for j in S for k in S if sum([i,j,k])==0]
I want to add another condition in the if statement such that my result set does not contain (0,0,0). I tried to do the following:
result=[(i,j,k) for i in S for j in S for k in S if sum([i,j,k])==0 && (i,j,k)!=(0,0,0)] but I am getting a syntax error pointing to the &&. I tested my expression for the first condition and it is correct.

You are looking for the and boolean operator instead:
result=[(i,j,k) for i in S for j in S for k in S if sum([i,j,k])==0 and (i,j,k)!=(0,0,0)]
&& is JavaScript, Java, Perl, PHP, Ruby, Go, OCaml, Haskell, MATLAB, R, Lasso, ColdFusion, C, C#, or C++ boolean syntax instead.

Apart from that error instead of triple nested for-loops you can also use itertools.product here to get the Cartesian product of S * S * S:
from itertools import product
result=[ x for x in product(S, repeat = 3) if sum(x)==0 and x != (0,0,0)]
Demo:
>>> S = [1, -1, 0, 0]
>>> [ x for x in product(S, repeat = 3) if sum(x) == 0 and x != (0,0,0)]
[(1, -1, 0), (1, -1, 0), (1, 0, -1), (1, 0, -1), (-1, 1, 0), (-1, 1, 0), (-1, 0, 1), (-1, 0, 1), (0, 1, -1), (0, -1, 1), (0, 1, -1), (0, -1, 1)]

result = [(i, j, k) for i in S
for j in S
for k in S
if sum([i, j, k]) == 0 and (i, j, k) != (0, 0, 0)]

Related

Move a shape inside a list of lists

I have the following list of lists representing a matrix:
space = [
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
]
The number 1s represent an upside down L (like a gamma, "Γ"). How can I make this "object" move to the right, left, up and down as if it was a block? I move it with "asdw" keys.
Important: I am not able to use numpy, so thats makes the work way more difficult.
This is my failed method to make a RIGHT direction movement (btw it doesnt make the movement correctly), but i dont think its the best way and dont really think I can escalate it to other movements:
def show_space(space):
for line in space:
print(line)
x = input("Movement: ")
if x == 'd':
for i in range(4):
for j in range(4):
if space[i][j] == 1:
try:
if space[i][j+1] == 0:
space[i][j] = 0
space[i][j+1] = 1
if space[i][j+1] == 1 and space[i][j+2] == 0:
space[i][j] = 0
space[i][j+2] = 1
except IndexError:
pass
show_space(space)
Is there any other method I could try? Or correct the one Im using? Thanks in advance
EDIT:
The gamma not only should be able to move right up down left, it should also be able to move 90 degrees, mirror itself, and all possible shapes that form can take. So if i had to hardcode all possible gamma or L combinations, i would have to hardcode 48 possibilities. I dont know wether hardcoding that is the optimal way to be honest.
Im not saying hardcoding the postions is not acceptable, it could definitely be a solution, but I just dont feel like its the correct way. i may be wrong of course.
What do you think?
Here's how I'd suggest doing something like this:
Calculate all the indices for each gamma shape in the matrix, kept stored in a dictionary with the each corner index tuple as the dictionary keys, then whenever the corner moves, figure out the indices that should be 1s, and assign to a copy of a matrix of zeros.
positions = {(0, 0): ((0, 1), (1, 0), (2, 0)),
(0, 1): ((0, 2), (1, 1), (2, 1)),
(0, 2): ((0, 3), (1, 2), (2, 2)),
(1, 0): ((1, 1), (2, 0), (3, 0)),
(1, 1): ((1, 2), (2, 1), (3, 1)),
(1, 2): ((1, 3), (2, 2), (3, 2))}
def move_gamma(corner):
board = [[0 for _ in range(4)] for _ in range(4)]
try:
for (i, j) in (corner, *positions[corner]):
board[i][j] = 1
return board
except KeyError:
print("You can't move there!")
return board
def flip_h(board):
return [[*reversed(row)] for row in board]
def flip_v(board):
return [*reversed(board)]
def transpose(board):
return [[*t] for t in zip(*board)]
Demo:
In [3]: board = move_gamma((1, 1))
In [4]: print(*board, sep="\n")
[0, 0, 0, 0]
[0, 1, 1, 0]
[0, 1, 0, 0]
[0, 1, 0, 0]
In [5]: board = move_gamma((1, 2))
In [6]: print(*board, sep="\n")
[0, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 1, 0]
[0, 0, 1, 0]
In [7]: print(*transpose(board), sep="\n")
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 0, 0]
Just a heads up, you'd still need to implement the logic for mapping WASD to movement relative to the current corner indices.
If you need to stay within the confines of the standards library, you can use collection.deque, which has a rotate method that does exactly what you need and will have to implement in lists if you can't use deque.
This is what I can offer with deque.
NB. this wraps around the edges which might not be intended,
from collections import deque
space = deque([
deque([0, 1, 1, 0]),
deque([0, 0, 1, 0]),
deque([0, 0, 1, 0]),
deque([0, 0, 0, 0]),
])
def move(mvt):
if mvt == "s":
space.rotate(1)
elif mvt == "w":
space.rotate(-1)
elif mvt == "d":
[x.rotate(1) for x in space]
elif mvt == "a":
[x.rotate(-1) for x in space]
else:
raise NotImplementedError
move("d") # etc
Your answer is in your question - instead of keeping track of all space (array with mostly 0s), use an object (python dict) to instead specify the shape, and where to start drawing it e.g.:
{
'abs_pos': (0,1),
'rel_points': [(0,0), (0,1), (1,0), (2,0)]
}
Then moving it only amounts to updating the location of (e.g.) the upper-left corner abs_pos.
When it's time to print your map of all space, start from abs_pos and then add a dot at each point in rel_points.

How can I add a random binary info into current 'coordinate'? (Python)

This is part of the code I'm working on: (Using Python)
import random
pairs = [
(0, 1),
(1, 2),
(2, 3),
(3, 0), # I want to treat 0,1,2,3 as some 'coordinate' (or positional infomation)
]
alphas = [(random.choice([1, -1]) * random.uniform(5, 15), pairs[n]) for n in range(4)]
alphas.sort(reverse=True, key=lambda n: abs(n[0]))
A sample output looks like this:
[(13.747649802587832, (2, 3)),
(13.668274782626717, (1, 2)),
(-9.105374057105703, (0, 1)),
(-8.267840318934667, (3, 0))]
Now I'm wondering is there a way I can give each element in 0,1,2,3 a random binary number, so if [0,1,2,3] = [0,1,1,0], (By that I mean if the 'coordinates' on the left list have the corresponding random binary information on the right list. In this case, coordinate 0 has the random binary number '0' and etc.) then the desired output using the information above looks like:
[(13.747649802587832, (1, 0)),
(13.668274782626717, (1, 1)),
(-9.105374057105703, (0, 1)),
(-8.267840318934667, (0, 0))]
Thanks!!
One way using dict:
d = dict(zip([0,1,2,3], [0,1,1,0]))
[(i, tuple(d[j] for j in c)) for i, c in alphas]
Output:
[(13.747649802587832, (1, 0)),
(13.668274782626717, (1, 1)),
(-9.105374057105703, (0, 1)),
(-8.267840318934667, (0, 0))]
You can create a function to convert your number to the random binary assigned. Using a dictionary within this function would make sense. Something like this should work where output1 is that first sample output you provide and binary_code would be [0, 1, 1, 0] in your example:
def convert2bin(original, binary_code):
binary_dict = {n: x for n, x in enumerate(binary_code)}
return tuple([binary_code[x] for x in original])
binary_code = np.random.randint(2, size=4)
[convert2bin(x[1], binary_code) for x in output1]

How to segment a matrix by neighbouring values?

Suppose I have a matrix like this:
m = [0, 1, 1, 0,
1, 1, 0, 0,
0, 0, 0, 1]
And I need to get the coordinates of the same neighbouring values (but not diagonally):
So the result would be a list of lists of coordinates in the "matrix" list, starting with [0,0], like this:
r = [[[0,0]],
[[0,1], [0,2], [1,0], [1,1]],
[[0,3], [1,2], [1,3], [2,0], [2,1], [2,2]]
[[2,3]]]
There must be a way to do that, but I'm really stuck.
tl;dr: We take an array of zeros and ones and use scipy.ndimage.label to convert it to an array of zeros and [1,2,3,...]. We then use np.where to find the coordinates of each element with value > 0. Elements that have the same value end up in the same list.
scipy.ndimage.label interprets non-zero elements of a matrix as features and labels them. Each unique feature in the input gets assigned a unique label. Features are e.g. groups of adjacent elements (or pixels) with the same value.
import numpy as np
from scipy.ndimage import label
# make dummy data
arr = np.array([[0,1,1,0], [1,1,0,0], [0,0,0,1]])
#initialise list of features
r = []
Since OP wanted all features, that is groups of zero and non-zero pixels, we use label twice: First on the original array, and second on 1 - original array. (For an array of zeros and ones, 1 - array just flips the values).
Now, label returns a tuple, containing the labelled array (which we are interested in) and the number of features that it found in that array (which we could use, but when I coded this, I chose to ignore it. So, we are interested in the first element of the tuple returned by label, which we access with [0]:
a = label(arr)[0]
b = label(1-arr)[0]
Now we check which unique pixel values label has assigned. So we want the set of a and b, repectively. In order for set() to work, we need to linearise both arrays, which we do with .ravel(). We have to subtract {0} in both cases, because for both a and b we are interested in only the non-zero values.
So, having found the unique labels, we loop through these values, and use np.where to find where on the array a given value is located. np.where returns a tuple of arrays. The first element of this tuple are all the row-coordinates for which the condition was met, and the second element are the column-coordinates.
So, we can use zip(* to unpack the two containers of length n to n containers of length 2. This means that we go from list of all row-coords + list of all column-coords to list of all row-column-coordinate pairs for which the condition is met. Finally in python 3, zip is a generator, which we can evaluate by calling list() on it. The resulting list is then appended to our list of coordinates, r.
for x in set(a.ravel())-{0}:
r.append(list(zip(*np.where(a==x))))
for x in set(b.ravel())-{0}:
r.append(list(zip(*np.where(b==x))))
print(r)
[[(0, 1), (0, 2), (1, 0), (1, 1)],
[(2, 3)],
[(0, 0)],
[(0, 3), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2)]]
That said, we can speed up this code slightly by making use of the fact that label returns the number of features it assigned. This allows us to avoid the set command, which can take time on large arrays:
a, num_a = label(arr)
for x in range(1, num_a+1): # range from 1 to the highest label
r.append(list(zip(*np.where(a==x))))
A solution with only standard libraries:
from pprint import pprint
m = [0, 1, 1, 0,
1, 1, 0, 0,
0, 0, 0, 1]
def is_neighbour(x1, y1, x2, y2):
return (x1 in (x2-1, x2+1) and y1 == y2) or \
(x1 == x2 and y1 in (y2+1, y2-1))
def is_value_touching_group(val, groups, x, y):
for d in groups:
if d['color'] == val and any(is_neighbour(x, y, *cell) for cell in d['cells']):
return d
def check(m, w, h):
groups = []
for i in range(h):
for j in range(w):
val = m[i*w + j]
touching_group = is_value_touching_group(val, groups, i, j)
if touching_group:
touching_group['cells'].append( (i, j) )
else:
groups.append({'color':val, 'cells':[(i, j)]})
final_groups = []
while groups:
current_group = groups.pop()
for c in current_group['cells']:
touching_group = is_value_touching_group(current_group['color'], groups, *c)
if touching_group:
touching_group['cells'].extend(current_group['cells'])
break
else:
final_groups.append(current_group['cells'])
return final_groups
pprint( check(m, 4, 3) )
Prints:
[[(2, 3)],
[(0, 3), (1, 3), (1, 2), (2, 2), (2, 0), (2, 1)],
[(0, 1), (0, 2), (1, 1), (1, 0)],
[(0, 0)]]
Returns as a list of groups under value key.
import numpy as np
import math
def get_keys(old_dict):
new_dict = {}
for key, value in old_dict.items():
if value not in new_dict.keys():
new_dict[value] = []
new_dict[value].append(key)
else:
new_dict[value].append(key)
return new_dict
def is_neighbor(a,b):
if a==b:
return True
else:
distance = abs(a[0]-b[0]), abs(a[1]-b[1])
return distance == (0,1) or distance == (1,0)
def collate(arr):
arr2 = arr.copy()
ret = []
for a in arr:
for i, b in enumerate(arr2):
if set(a).intersection(set(b)):
a = list(set(a+b))
ret.append(a)
for clist in ret:
clist.sort()
return [list(y) for y in set([tuple(x) for x in ret])]
def get_groups(d):
for k,v in d.items():
ret = []
for point in v:
matches = [a for a in v if is_neighbor(point, a)]
ret.append(matches)
d[k] = collate(ret)
return d
a = np.array([[0,1,1,0],
[1,1,0,0],
[0,0,1,1]])
d = dict(np.ndenumerate(a))
d = get_keys(d)
d = get_groups(d)
print(d)
Result:
{
0: [[(0, 3), (1, 2), (1, 3)], [(0, 0)], [(2, 0), (2, 1)]],
1: [[(2, 2), (2, 3)], [(0, 1), (0, 2), (1, 0), (1, 1)]]
}

non Invertible of a ARIMA model

I am trying to write a code to generate a series of arima model and compare different models.The code is as follow.
p=0
q=0
d=0
pdq=[]
aic=[]
for p in range(6):
for d in range(2):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit(transparams=True)
x=arima_mod.aic
x1= p,d,q
print (x1,x)
aic.append(x)
pdq.append(x1)
keys = pdq
values = aic
d = dict(zip(keys, values))
print (d)
minaic=min(d, key=d.get)
for i in range(3):
p=minaic[0]
d=minaic[1]
q=minaic[2]
print (p,d,q)
Where 'df' is the time series data.And the output is as follow,
(0, 0, 0) 1712.55522759
(0, 0, 1) 1693.436483044094
(0, 0, 2) 1695.2226857997066
(0, 0, 3) 1690.9437925956158
(0, 1, 0) 1712.74161799
(0, 1, 1) 1693.0408994539348
(0, 1, 2) 1677.2235087182808
(0, 1, 3) 1679.209810237856
(1, 0, 0) 1700.0762847127553
(1, 0, 1) 1695.353190569905
(1, 0, 2) 1694.7907607467605
(1, 0, 3) 1692.235442716487
(1, 1, 0) 1714.5088374907164
ValueError: The computed initial MA coefficients are not invertible
You should induce invertibility, choose a different model order, or you can
pass your own start_params.
i.e for order (1,1,1) the model is non invertible. so the process stops there.How can i skip such non invertible combination of p,d,q and go on with other combination
Use try: ... except: ... to catch the exception and continue
for p in range(6):
for d in range(2):
for q in range(4):
try:
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit(transparams=True)
x=arima_mod.aic
x1= p,d,q
print (x1,x)
aic.append(x)
pdq.append(x1)
except:
pass
# ignore the error and go on

Project Euler #101 - how to work around numpy polynomial overflow?

Project Euler #101
I just started learning Numpy and it so far looks pretty straightforward to me.
One thing I ran into is that when I evaluate the polynomial, the result is a int32, so an overflow would occur.
u = numpy.poly1d([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1])
for i in xrange(1, 11):
print(i, u(i))
The results are:
(1, 1)
(2, 683)
(3, 44287)
(4, 838861)
(5, 8138021)
(6, 51828151)
(7, 247165843)
(8, 954437177)
(9, -1156861335)
(10, 500974499)
The last two items are clearly incorrect.
The work around I can think of is factoring the coefficients by 100
u = numpy.poly1d([0.01, -0.01, 0.01, -0.01, 0.01, -0.01, 0.01, -0.01, 0.01, -0.01, 0.01])
for i in xrange(1, 11):
print(i, int(u(i) * 100))
This time the results are correct
(1, 1)
(2, 682)
(3, 44286)
(4, 838860)
(5, 8138020)
(6, 51828151)
(7, 247165843)
(8, 954437177)
(9, 3138105961L)
(10, 9090909091L)
Is there a better way? Does Numpy allow me to change the data type? Thanks.
It wasn't the scaling by 100 that helped, but the fact that the numbers given were floats instead of ints, and thus had a higher range. Due to the floating-point calculations, there are some inaccuracies introduced to the calculations as you have seen.
You can specify the type manually like this:
u = numpy.poly1d(numpy.array([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1], dtype=numpy.int64))
The calculations for this problem fit in 64-bit ints, so this will work.
The supported types are listed here.
Interjay posted a better answer while I was writing this up, but I figured you might want an alternative anyway.
Here's a simple implementation for the examples you showed:
class Poly(object):
def __init__(self, coefficients):
assert len(coefficients) > 0
self.coefficients = coefficients
def __call__(self, value):
total = self.coefficients[0]
for c in self.coefficients[1:]:
total = total * value + c
return total
along with some tests
assert Poly([5])(1) == 5
assert Poly([7])(1) == 7
assert Poly([2,3])(5) == 13
assert Poly([1,0,0,0,0])(-2) == 16
u = Poly([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1])
for i in range(1, 11):
print (i, u(i))
and the rather useless
assert Poly([2,"!"])("Hello ") == "Hello Hello !"

Categories