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.
Related
Knight's tour problem described in the image here, with diagram.
A knight was initially located in a square labeled 1. It then proceeded to make a
series of moves, never re-visiting a square, and labeled the visited squares in
order. When the knight was finished, the labeled squares in each region of connected
squares had the same sum.
A short while later, many of the labels were erased. The remaining labels can be seen
above.
Complete the grid by re-entering the missing labels. The answer to this puzzle is
the sum of the squares of the largest label in each row of the completed grid, as in
the example.
[1]: E.g. the 14 and 33 are in different regions.
The picture explains it a lot more clearly, but in summary a Knight has gone around a 10 x 10 grid. The picture shows a 10 x 10 board that shows some positions in has been in, and at what point of its journey. You do not know which position the Knight started in, or how many movements it made.
The coloured groups on the board need to all sum to the same amount.
I’ve built a python solver, but it runs for ages - uses recursion. I’ve noted that the maximum sum of a group is 197, based on there being 100 squares and the smallest group is 2 adjacent squares.
My code at this link: https://pastebin.com/UMQn1HZa
import sys, numpy as np
fixedLocationsArray = [[ 12, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 5, 0, 23, 0],
[ 0, 0, 0, 0, 0, 0, 8, 0, 0, 0],
[ 0, 0, 0, 14, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 20, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 33, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 28]]
groupsArray = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0,10, 0],
[0, 0, 0, 1, 0, 0, 0, 0,10, 0],
[0, 0, 1, 1, 1, 1, 9,10,10,10],
[2, 0, 1, 0, 0,11, 9, 9, 9, 9],
[2, 0, 0, 0,11,11,11,15,15, 9],
[2, 4, 4,14,11,12,12,15,15, 8],
[2, 3, 4,14,14,13,13,13,15, 8],
[2, 3, 5,14,16,16,16, 7, 7, 8],
[3, 3, 5, 6, 6, 6, 6, 6, 7, 8]]
'''
Solver
- Noted that the maximum sum of a group is 197 since the group of only 2 can have the 100 filled and then 97 on return
'''
class KnightsTour:
def __init__(self, width, height, fixedLocations, groupsArray):
self.w = width
self.h = height
self.fixedLocationsArray = fixedLocations
self.groupsArray = groupsArray
self.npfixedLocationsArray = np.array(fixedLocations)
self.npgroupsArray = np.array(groupsArray)
self.board = [] # Contains the solution
self.generate_board()
def generate_board(self):
"""
Creates a nested list to represent the game board
"""
for i in range(self.h):
self.board.append([0]*self.w)
def print_board(self): # Prints out the final board solution
print(" ")
print("------")
for elem in self.board:
print(elem)
print("------")
print(" ")
def generate_legal_moves(self, cur_pos, n):
"""
Generates a list of legal moves for the knight to take next
"""
possible_pos = []
move_offsets = [(1, 2), (1, -2), (-1, 2), (-1, -2),
(2, 1), (2, -1), (-2, 1), (-2, -1)]
locationOfNumberInFixed = [(ix,iy) for ix, row in enumerate(self.fixedLocationsArray) for iy, i in enumerate(row) if i == n+1]
groupsizeIsNotExcessive = self.groupsNotExcessiveSize(self.board, self.groupsArray)
for move in move_offsets:
new_x = cur_pos[0] + move[0]
new_y = cur_pos[1] + move[1]
new_pos = (new_x, new_y)
if groupsizeIsNotExcessive:
if locationOfNumberInFixed:
print(f"This number {n+1} exists in the fixed grid at {locationOfNumberInFixed[0]}")
if locationOfNumberInFixed[0] == new_pos:
print(f"Next position is {new_pos} and matches location in fixed")
possible_pos.append((new_x, new_y))
else:
continue
elif not locationOfNumberInFixed: # if the current index of move is not in table, then evaluate if it is a legal move
if (new_x >= self.h): # if it is out of height of the board, continue, don't app onto the list of possible moves
continue
elif (new_x < 0):
continue
elif (new_y >= self.w):
continue
elif (new_y < 0):
continue
else:
possible_pos.append((new_x, new_y))
else:
continue
print(f"The legal moves for index {n} are {possible_pos}")
print(f"The current board looks like:")
self.print_board()
return possible_pos
def sort_lonely_neighbors(self, to_visit, n):
"""
It is more efficient to visit the lonely neighbors first,
since these are at the edges of the chessboard and cannot
be reached easily if done later in the traversal
"""
neighbor_list = self.generate_legal_moves(to_visit, n)
empty_neighbours = []
for neighbor in neighbor_list:
np_value = self.board[neighbor[0]][neighbor[1]]
if np_value == 0:
empty_neighbours.append(neighbor)
scores = []
for empty in empty_neighbours:
score = [empty, 0]
moves = self.generate_legal_moves(empty, n)
for m in moves:
if self.board[m[0]][m[1]] == 0:
score[1] += 1
scores.append(score)
scores_sort = sorted(scores, key = lambda s: s[1])
sorted_neighbours = [s[0] for s in scores_sort]
return sorted_neighbours
def groupby_perID_and_sum(self, board, groups):
# Convert into numpy arrays
npboard = np.array(board)
npgroups = np.array(groups)
# Get argsort indices, to be used to sort a and b in the next steps
board_flattened = npboard.ravel()
groups_flattened = npgroups.ravel()
sidx = groups_flattened.argsort(kind='mergesort')
board_sorted = board_flattened[sidx]
groups_sorted = groups_flattened[sidx]
# Get the group limit indices (start, stop of groups)
cut_idx = np.flatnonzero(np.r_[True,groups_sorted[1:] != groups_sorted[:-1],True])
# Create cut indices for all unique IDs in b
n = groups_sorted[-1]+2
cut_idxe = np.full(n, cut_idx[-1], dtype=int)
insert_idx = groups_sorted[cut_idx[:-1]]
cut_idxe[insert_idx] = cut_idx[:-1]
cut_idxe = np.minimum.accumulate(cut_idxe[::-1])[::-1]
# Split input array with those start, stop ones
arrayGroups = [board_sorted[i:j] for i,j in zip(cut_idxe[:-1],cut_idxe[1:])]
arraySum = [np.sum(a) for a in arrayGroups]
sumsInListSame = arraySum.count(arraySum[0]) == len(arraySum)
return sumsInListSame
def groupsNotExcessiveSize(self, board, groups):
# Convert into numpy arrays
npboard = np.array(board)
npgroups = np.array(groups)
# Get argsort indices, to be used to sort a and b in the next steps
board_flattened = npboard.ravel()
groups_flattened = npgroups.ravel()
sidx = groups_flattened.argsort(kind='mergesort')
board_sorted = board_flattened[sidx]
groups_sorted = groups_flattened[sidx]
# Get the group limit indices (start, stop of groups)
cut_idx = np.flatnonzero(np.r_[True,groups_sorted[1:] != groups_sorted[:-1],True])
# Create cut indices for all unique IDs in b
n = groups_sorted[-1]+2
cut_idxe = np.full(n, cut_idx[-1], dtype=int)
insert_idx = groups_sorted[cut_idx[:-1]]
cut_idxe[insert_idx] = cut_idx[:-1]
cut_idxe = np.minimum.accumulate(cut_idxe[::-1])[::-1]
# Split input array with those start, stop ones
arrayGroups = [board_sorted[i:j] for i,j in zip(cut_idxe[:-1],cut_idxe[1:])]
arraySum = [np.sum(a) for a in arrayGroups]
print(arraySum)
# Check if either groups aren't too large
groupSizeNotExcessive = all(sum <= 197 for sum in arraySum)
return groupSizeNotExcessive
def tour(self, n, path, to_visit):
"""
Recursive definition of knights tour. Inputs are as follows:
n = current depth of search tree
path = current path taken
to_visit = node to visit, i.e. the coordinate
"""
self.board[to_visit[0]][to_visit[1]] = n # This writes the number on the grid
path.append(to_visit) #append the newest vertex to the current point
print(f"Added {n}")
print(f"For {n+1} visiting: ", to_visit)
if self.groupby_perID_and_sum(self.board, self.npgroupsArray): #if all areas sum
self.print_board()
print(path)
print("Done! All areas sum equal")
sys.exit(1)
else:
sorted_neighbours = self.sort_lonely_neighbors(to_visit, n)
for neighbor in sorted_neighbours:
self.tour(n+1, path, neighbor)
#If we exit this loop, all neighbours failed so we reset
self.board[to_visit[0]][to_visit[1]] = 0
try:
path.pop()
print("Going back to: ", path[-1])
except IndexError:
print("No path found")
sys.exit(1)
if __name__ == '__main__':
#Define the size of grid. We are currently solving for an 8x8 grid
kt0 = KnightsTour(10, 10, fixedLocationsArray, groupsArray)
kt0.tour(1, [], (3, 0))
# kt0.tour(1, [], (7, 0))
# kt0.tour(1, [], (7,2))
# kt0.tour(1, [], (6,3))
# kt0.tour(1, [], (4,3))
# kt0.tour(1, [], (3,2))
# startingPositions = [(3, 0), (7, 0), (7,2), (6,3), (4,3), (3,2)]
kt0.print_board()
Here are some observations that you could include to be able to stop more early in your backtracking.
First of all remember that for n steps the total sum in all areas can be computed with the formula n(n+1)/2. This number has to be divisible evenly into the groups i.e. it has to be divisible by 17 which is the amount of groups.
Furthermore if we look at the 12 we can conclude that the 11 and 13 must have been in the same area so we get a lower bound on the number for each area as 2+5+8+11+12+13=51.
And lastly we have groups of size two so the largest two step numbers must make up the total sum for one group.
Using those conditions we can calculate the remaining possible amount of steps with
# the total sum is divisible by 17:
# n*(n+1)/2 % 17 == 0
# the sum for each group is at least the predictable sum for
# the biggest given group 2+5+8+11+12+13=51:
# n*(n+1)/(2*17) >= 51
# since there are groups of two elements the sum of the biggest
# two numbers must be as least as big as the sum for each group
# n+n-1 >= n*(n+1)/(2*17)
[n for n in range(101) if n*(n+1)/2 % 17 == 0 and n*(n+1)/(2*17) >= 51 and n+n-1 >= n*(n+1)/(2*17)]
giving us
[50,51].
So the knight must have taken either 50 or 51 steps and the sum for each area must be either 75 or 78.
For a bit of background, I'm not a computer scientist or programmer at all (studied physics in college where I picked up some python). My problem is to find the longest path through a matrix with a given set of rules. An example matrix would look something like this:
[0, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 1, 0],
The rules are as follows:
Start on a given "1" position, these are the valid positions.
Each jump must be to another valid position on the same row or column (it's possible to jump over "0"s).
Consecutive jumps can not be in the same direction (horizontal/vertical) unless jumping to and from a position on-diagonal.
No position can be used twice.
A valid path on the example matrix would look like:
(5,4),(5,1),(3,1),(3,3),(3,2),(2,2)
An invalid path because of rule #3 would look like:
(3,1),(3,2),(3,3)
whereas the following is possible:
(3,1),(3,3),(3,2)
Though I have a bit of experience with python, I've never tried recursion (I'm pretty sure that's how to tackle this), and I can't seem to find any help online that's at my level.
There are several solutions for this. I would suggest first converting the grid to a more object oriented structure, i.e. an undirected graph with nodes for where there are 1s in the input.
Then I would distinguish three kinds of edges in that graph:
Those where one of the end points is on a diagonal ("special" edges)
Those where the above is not true, but nodes are in the same row
Those where the above is not true, but nodes are in the same column
While recurring, you would always consider the special edges, and in addition to that, the edges that are in one of the other two sets of edges (based on the previous direction taken).
Here is an implementation:
class Node:
def __init__(self, y, x, size):
self.x = x
self.y = y
self.coord = (y, x)
self.diagonal = x == y or size - 1 - y
# Separate lists of neighbors: vertical, horizontal.
# Third list is for when this node or neighbor is on diagonal
self.neighbors = [[], [], []]
def addNeighbor(self, node, direction):
self.neighbors[direction].append(node)
class Maze:
def __init__(self, grid):
def addedge(a, b):
direction = 2 if a.diagonal or b.diagonal else int(a.x == b.x)
a.addNeighbor(b, direction)
b.addNeighbor(a, direction)
# alternative grid having Node references:
self.nodes = [[None] * len(grid) for _ in grid]
colNodes = [[] for _ in grid]
for y, row in enumerate(grid):
rowNodes = []
for x, cell in enumerate(row):
if cell: # only create nodes for when there is a 1 in the grid
node = Node(y, x, len(grid))
for neighbor in rowNodes + colNodes[x]:
addedge(node, neighbor)
rowNodes.append(node)
colNodes[x].append(node)
self.nodes[y][x] = node
def findpath(self, y, x):
def recur(node, neighbors):
visited.add(node)
longest = [node.coord]
# always visit "special" neighbors
# (i.e. those on diagonal or all vert/horiz when node is on diagonal)
for neighbor in node.neighbors[2] + neighbors:
if not neighbor in visited:
# toggle direction when going further
path = recur(neighbor, node.neighbors[1-int(node.x == neighbor.x)])
if len(path) >= len(longest):
longest = [node.coord] + path
visited.remove(node)
return longest
node = self.nodes[y][x]
if not node:
raise "Cannot start from that position"
visited = set()
# look in both directions of starting node
return recur(node, node.neighbors[0] + node.neighbors[1])
grid = [
[0, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 1, 0]
]
maze = Maze(grid)
path = maze.findpath(2, 0)
print(path) # output: [(2, 0), (2, 2), (2, 1), (1, 1)]
path = maze.findpath(4, 3)
print(path) # output: [(4, 3), (4, 0), (2, 0), (2, 2), (2, 1), (1, 1)]
Note that the coordinates in this solution are zero-based, so the first row has number 0, ...etc.
See it run on repl.it
You can use recursion with a generator:
d = [[0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 1, 0]]
_d = {1:lambda a, b:'f' if b[-1] > a[-1] else 'b', 0:lambda a, b:'u' if b[0] > a[0] else 'd'}
def paths(start, _dir, c = []):
yield c
_options = [(a, b) for a in range(len(d)) for b in range(len(d[0])) if (a, b) not in c and d[a][b]]
if _options:
for a, b in _options:
if a == start[0] or b == start[-1]:
r = _d[a == start[0]](start, (a, b))
if _dir is None or r != _dir:
yield from paths((a, b), r, c+[(a, b)])
print(max(list(paths((4, 3), None, [(4, 3)])), key=len))
Output:
[(4, 3), (4, 0), (2, 0), (2, 2), (2, 1), (1, 1)]
I have the following problem. In my code
I have arrays of size L. The entries are either 0 or 1 for all arrays. Lets set L=3 for an example. Three possible arrays are (1,1,1), (1,0,0) and (0,1,1). Now I want to know how many single crossovers are possible with (1,0,0) and (0,1,1) to form (1,1,1). The answer would be one. For (1,0,1) and (0,1,0) to form (1,1,1) the answer would be 0, because I would need two crossovers. I am looking for an algorithm which does this for general L. (L is usually not larger than 9). So far I don't have any idea and that is why I have post this problem her but I will make an edit if I got one. Hope you can help me :)
Edit: The outcome can be of course not only 0 or 1 but also greater than 1.
Example: (1,1,0,0) and (0,0,0,0) to form (0,0,0,0) the outcome would be 2. (I can only take the last entry of the first array or the last 2 entries of the first array)
Edit 2: By single crossover I mean, that I can take the left/right side of the first sequence and the right/left side of the second sequence to form the given third sequence. (1,1,0,0) and (0,0,0,0) to form (0,0,0,0) --> 0,0) + (0,0, or 0) + (0,0,0,
Another way of interpreting the problem is as calculating the Hamming Distance. Here is a snippet to create a dictionary of all pairs of each Hamming Distance/crossovers.
from itertools import combinations
tuples = [(0, 0, 1), (1, 0, 0), (1, 0, 1)]
crossovers = {k: [] for k in range(len(tuples[0]))}
for a, b in combinations(tuples, r=2):
num_crossovers = sum(el1 != el2 for el1, el2 in zip(a, b))
crossovers[num_crossovers].append((a, b))
After executing crossovers will be as follows
{0: [],
1: [((0, 0, 1), (1, 0, 1)), ((1, 0, 0), (1, 0, 1))],
2: [((0, 0, 1), (1, 0, 0))]}
EDIT:
I missed that you were using numpy arrays instead of tuples, you could do
arrays = np.array([[0, 0, 1], [1, 0, 0], [1, 0, 1]])
crossovers = {k: [] for k in range(arrays[0].size)}
for a, b in combinations(arrays, r=2):
num_crossovers = sum(np.abs(a - b))
crossovers[num_crossovers].append((a, b))
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
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)]