Creating a bigger List of Lists using a pattern - python

Let's say I have the following pattern:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and I want to use this to create the following:
PATTERN | HORIZONTAL_MIRROR (PATTERN)
VERTICAL_MIRROR(PATTERN) | HORIZONTAL_MIRROR(VERTICAL_MIRROR(PATTERN))
In other words:
[[1, 2, 3, 3, 2, 1],[4, 5, 6, 6, 5, 4],[7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
is there an efficient method that can be used in Python apart from copying each element from the PATTERN?

Just an idea:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
def patternify(l):
for sl in l:
yield sl+sl[::-1]
for sl in l[::-1]:
yield sl+sl[::-1]
list(patternify(PATTERN))
#output: [[1, 2, 3, 3, 2, 1], [4, 5, 6, 6, 5, 4], [7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]

If I understood the requirement correctly:
def mirror(pat):
return pat + pat[::-1]
SUPERPATTERN = mirror(map(mirror, PATTERN))

Related

Python: how to get random contiguous sets?

This is what I get by running train_test_split
In [1]:train_test_split([1,2,3,4,5,6,7,8,9,10],test_size = 0.2)
Out[1]: [[10, 3, 6, 5, 4, 2, 7, 9], [8, 1]]
However, what I want is a contiguous set, i.e.
[[1, 2, 3, 4, 5, 6, 7, 10], [8, 9]]
or
[[1, 2, 3, 4, 5, 8, 9, 10], [6, 7]]
or
[[1, 2, 5, 6, 7, 8, 9, 10], [3, 4]]
****** Please note that the following is also considered contiguous**
[[2, 3, 4, 5, 6, 7, 8, 9], [10, 1]]
How can I do this ?
Can you try the following:
import random
def custom_train_test_split(X, test_size=0.2):
temp = X.copy()
split_size = int(len(temp) * test_size)
start = random.randint(0, len(X) - split_size)
end = start + split_size
test = temp[start:end]
del temp[start:end]
return [temp, test]
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(custom_train_test_split(X, test_size=0.2))
print(custom_train_test_split(X, test_size=0.2))
print(custom_train_test_split(X, test_size=0.2))
Output:
[[1, 2, 5, 6, 7, 8, 9, 10], [3, 4]]
[[1, 2, 3, 4, 5, 8, 9, 10], [6, 7]]
[[3, 4, 5, 6, 7, 8, 9, 10], [1, 2]]

Update a position in list of list Python

I have a list:
[[6, 8, 5, 1, 3, 2, 9, 4, 7],
[7, 3, 4, 5, 9, 8, 2, 1, 6],
[2, 1, 9, 7, 6, 4, 8, 5, 3],
[9, 2, 6, 8, 7, 1, 5, 3, 4],
[8, 5, 1, 3, 4, 9, 6, 7, 2],
[4, 7, 3, 2, 5, 6, 1, 8, 9],
[5, 6, 8, 4, 2, 7, 3, 9, 1],
[3, 4, 2, 9, 1, 5, 7, 6, 8],
[1, 9, 7, 6, 8, 3, 4, 2, 5]]
I want to update a position in this list
for example:
update_list(position: tuple[int, int], value: Optional[int])
where value is the element that is going to replace the original element in the list
Thus update_list((1, 1), 5) should replace 3 with 5
What is the best way to code for this?
You can directly index into the inner list!
>>> content = [[6, 8, 5, 1, 3, 2, 9, 4, 7],
... [7, 3, 4, 5, 9, 8, 2, 1, 6],
... [2, 1, 9, 7, 6, 4, 8, 5, 3],
... [9, 2, 6, 8, 7, 1, 5, 3, 4],
... [8, 5, 1, 3, 4, 9, 6, 7, 2],
... [4, 7, 3, 2, 5, 6, 1, 8, 9],
... [5, 6, 8, 4, 2, 7, 3, 9, 1],
... [3, 4, 2, 9, 1, 5, 7, 6, 8],
... [1, 9, 7, 6, 8, 3, 4, 2, 5]]
>>> content[1][1]
3
>>> content[1][1] = 5
>>> content[1][1]
5
>>> content
[[6, 8, 5, 1, 3, 2, 9, 4, 7], [7, 5, 4, 5, 9, 8, 2, 1, 6], [2, 1, 9, 7, 6, 4, 8, 5, 3], [9, 2, 6, 8, 7, 1, 5, 3, 4], [8, 5, 1, 3, 4, 9, 6, 7, 2], [4, 7, 3, 2, 5, 6, 1, 8, 9], [5, 6, 8, 4, 2, 7, 3, 9, 1], [3, 4, 2, 9, 1, 5, 7, 6, 8], [1, 9, 7, 6, 8, 3, 4, 2, 5]]
This one works for me:
content = [[6, 8, 5],[1, 2, 3]]
def update_list(a, b):
content[a[0]][a[1]] = b
update_list((0,0),25) #It replace the first element by 25
The Output of content:
[[25, 8, 5], [1, 2, 3]]

Trying to convert a list which contains only one int, within a list to just an int within the list

I have a list which contains both ints and also lists of ints within the list.
I am trying to make a method which checks if there are any 1 number lists within the list, and converts that one number list into just an int. So it is no longer a list within the list.
I am using the below code as a guide, but this code works with a matrix and mine is a 3d list.
# Process grid with Possible array values as cells are solved
for row in range(0, GRIDSIZE):
for col in range(0, GRIDSIZE):
# Found correct cell value = Only 1 possible value
if np.size(P[row, col]) == 1:
singleton = P[row][col][0]
grid[row][col] = singleton
# Remove from Possible list
P[row, col].remove(singleton)
This is what I'm trying to do but I get indexerror: invalid index to scalar variable:
for row in range(0,9):
for col in range(0,9):
if np.size((values[row][col])) == 1:
singleton = values[row][col][0]
values[row][col] = singleton
Also for the 'remove from possible list' part, is there a simple pythonic command i can do to remove that singleton element from the 2d list?
This is what the list looks like at the moment:
[[[7], 8, 5, [6], 1, 3, [2, 6], [4], 9], [6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5], [[1], 2, [1, 9], 5, 7, 4, [6], 3, [8, 6]], [2, 4, 8, 3, 6, 7, 9, 5, 1], [9, 6, [1], 4, 5, 8, [7], 2, 3], [3, 5, 7, 2, [9], [1], 4, 8, [6]], [5, 7, 3, 1, [4], [6], 8, 9, 2], [4, 9, 6, [8, 7], 2, 5, 3, 1, [7]], [8, 1, 2, [7], 3, 9, 5, 6, 4]]
You can use a nested list comprehension:
original_list = [[[7], 8, 5, [6], 1, 3, [2, 6], [4], 9], [6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5], [[1], 2, [1, 9], 5, 7, 4, [6], 3, [8, 6]], [2, 4, 8, 3, 6, 7, 9, 5, 1], [9, 6, [1], 4, 5, 8, [7], 2, 3], [3, 5, 7, 2, [9], [1], 4, 8, [6]], [5, 7, 3, 1, [4], [6], 8, 9, 2], [4, 9, 6, [8, 7], 2, 5, 3, 1, [7]], [8, 1, 2, [7], 3, 9, 5, 6, 4]]
new_list = [[elem[0] if (isinstance(elem, list) and len(elem)==1) else elem for elem in sublist] for sublist in original_list ]
# new_list
"""
[[7, 8, 5, 6, 1, 3, [2, 6], 4, 9],
[6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5],
[1, 2, [1, 9], 5, 7, 4, 6, 3, [8, 6]],
[2, 4, 8, 3, 6, 7, 9, 5, 1],
[9, 6, 1, 4, 5, 8, 7, 2, 3],
[3, 5, 7, 2, 9, 1, 4, 8, 6],
[5, 7, 3, 1, 4, 6, 8, 9, 2],
[4, 9, 6, [8, 7], 2, 5, 3, 1, 7],
[8, 1, 2, 7, 3, 9, 5, 6, 4]]"""
EDIT: Using for loops:
new_list = []
for sublist in original_list:
new_sublist = []
for elem in sublist:
if (isinstance(elem, list) and len(elem) == 1):
new_sublist.append(elem[0])
else:
new_sublist.append(elem)
new_list.append(new_sublist)

How can I make this triple Sudoku for loop work

I am having some trouble with this algorithm. I am only able to grab one sub grid. I've only included the sub grid part here as I've done checkRows and checkColumns on my own. How can I connect this code to capture the rest? Your help is greatly appreciated. Thanks.
class Sudoku_Checker:
def __init__(self,board):
self.board = board
def board_validater(self,board):
self.checkSquares(board)
return self.checkSquares(board) == True
def checkSquares(self,board):
compare = [1,2,3,4,5,6,7,8,9]
hold = []
row = 0
column = 0
square = 0
for p in range(square, square+3):
for i in range(row, row + 3):
for j in range(column, column + 3):
hold.append(board[i][j])
if len(hold)==9:
if sorted(hold) == compare:
hold =[]
continue
else:
return False
square +=3
row += 3
return True
board = [ [5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]]
s = Sudoku_Checker(board)
s.board_validater(board)
So in my exemple with line and column variable i get the upper left corner of eaxh 3x3 matrix and then i iterate from there to construct the square.
Please let me know if this is what you are looking for.
board = [
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]
]
for line in range(0, 9, 3):
for column in range(0, 9, 3):
square = [[board[l][c] for c in range(column, column + 3)] for l in range(line, line + 3)]
print(square)
Result:
[[5, 3, 4], [6, 7, 2], [1, 9, 8]]
[[6, 7, 8], [1, 9, 5], [3, 4, 2]]
[[9, 1, 2], [3, 4, 8], [5, 6, 0]]
[[8, 5, 9], [4, 2, 6], [7, 1, 3]]
[[7, 6, 1], [8, 5, 3], [9, 2, 4]]
[[4, 2, 3], [7, 9, 1], [8, 5, 6]]
[[9, 6, 1], [2, 8, 7], [3, 4, 5]]
[[5, 3, 7], [4, 1, 9], [2, 8, 6]]
[[2, 8, 4], [6, 3, 5], [1, 7, 9]]
Hopefully someone finds this useful. I prefer this implementation because it is more readable and understandable to me. The other commentor also has a good solution except for the list comprehension which is too much of a loop.
class Sudoku_Checker:
def __init__(self,board):
self.board = board
def board_validater(self,board):
return self.checkSquares(board) == True
def checkSquares(self,board):
compare = range(1,10)
for i in range(0, 9, 3):
for j in range(0, 9, 3):
nums = board[i][j:j+3] + board[i+1][j:j+3] + board[i+2][j:j+3]
if sorted(nums) == compare:
continue
else:
return False
return True
board = [ [5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]]
s = Sudoku_Checker(board)
s.board_validater(board)

Insert a string at a specified index in 2D array

I would like to insert a string at a specified index in a row of an array by 2 steps. From a matrix:
A=[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[1, 1, 2, 2, 3],
[2, 3, 4, 5, 6],
[4, 5, 6, 7, 7],
[5, 7, 6, 8, 9]]
I would like to receive:
A=[[**x**, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[**x**, 1, 2, 2, 3],
[2, 3, 4, 5, 6],
[**x**, 5, 6, 7, 7],
[5, 7, 6, 8, 9]]
or:
A=[[1, 2, 3, 4, 5],
[**x**, 7, 8, 9, 10],
[1, 1, 2, 2, 3],
[**x**, 3, 4, 5, 6],
[4, 5, 6, 7, 7],
[**x**, 7, 6, 8, 9]]
or:
A=[[1, 2, **x**, 4, 5],
[6, 7, 8, 9, 10],
[1, 1, **x**, 2, 3],
[2, 3, 4, 5, 6],
[4, 5, **x**, 7, 7],
[5, 7, 6, 8, 9]]
and so on. I hope you understand my question (I used bold letters in order to distinguish strings). If I try:
def r(l):
for i in l[::2]:
i.insert(0, 'x')
return l
it returns:
'int' object has no attribute 'insert'
but I suppose it isn't my very valuable comment, if I end up with no clue how I can complete the task...
You can use simple indexing instead of insert:
def r(l, idx=0):
for i in l[::2]:
i[idx] = 'x'
return l
>>> print(r(A))
[['x', 2, 3, 4, 5], [6, 7, 8, 9, 10], ['x', 1, 2, 2, 3], [2, 3, 4, 5, 6], ['x', 5, 6, 7, 7], [5, 7, 6, 8, 9]]
The idx argument gives you the index where you want to change the entry. For example, if you wanted to change the 3rd element to x, then use:
>>> print(r(A, idx=2))
[[1, 2, 'x', 4, 5], [6, 7, 8, 9, 10], [1, 1, 'x', 2, 3], [2, 3, 4, 5, 6], [4, 5, 'x', 7, 7], [5, 7, 6, 8, 9]]

Categories