checking if a pair of value is inside a 2D array python
You can simply run
print([0,1] in arr)
to see if [0,1]
exists in the first level loop. It should return True
in the example you have provided
Finding matching pairs (intersection) of values between two 2D arrays?
Use a structured array.
import numpy as np
# Define a dtype with x and y integers
arr1 = np.empty(6, dtype=[('x', int), ('y', int)])
arr2 = np.empty(6, dtype=[('x', int), ('y', int)])
# Add the data to the structured array
arr1['x'] = np.array([ 32, 32, 32, 32, 32, 39])
arr1['y'] = np.array([449, 451, 452, 453, 454, 463])
arr2['x'] = np.array([ 39, 34, 32, 32, 37, 32])
arr2['y'] = np.array([463, 393, 453, 452, 261, 449])
Use intersect1d:
>>> np.intersect1d(arr1, arr2)
array([(32, 449), (32, 452), (32, 453), (39, 463)],
dtype=[('x', '<i8'), ('y', '<i8')])
Python - How can I find if an item exists in multidimensional array?
You need to iterate over all the indices of your list to see if an element is a value in one of the nested lists. You can simply iterate over the inner lists and check for the presence of your element, e.g.:
if not any(0 in x for x in board):
pass # the board is full
Using any()
will serve as a short-stop whenever it encounters an element with a 0
in it so you don't need to iterate over the rest.
For numpy 2d array, how to find all pairs of adjacent elements?
I think the requirements can be met without explicit loops in numpy.
import numpy as np
np.random.seed(1234) # Make random array reproduceable
arr = np.random.randint( 0, 2, size = (10,10))
leftshifted = arr[ :, 1:] # Shift arr 1 col left, shape = (10, 9)
downshifted = arr[ 1: ] # Shift arr 1 row down, shape = ( 9, 10)
hrows, hcols = np.where( arr[ :, :-1 ] & leftshifted )
# arr[ :,:-1 ] => ignore last column for the comparison
# returns rows and columns where arr and leftshifted = 1
# i.e. where two adjacent columns in a row are 1
vrows, vcols = np.where( arr[ :-1 ] & downshifted )
# arr[ :-1 ] => ignore last row for the comparison
# returns rows and columns where arr and downshifted = 1
# i.e. where two adjacent rows in a column are 1
print(arr, '\n')
# [[1 1 0 1 0 0 0 1 1 1]
# [1 1 0 0 1 0 0 0 0 0]
# [0 0 0 0 1 0 1 1 0 0]
# [1 0 0 1 0 1 0 0 0 1]
# [1 1 0 1 1 0 1 0 1 0]
# [1 1 1 1 0 1 0 1 1 0]
# [0 1 0 0 1 1 1 0 0 0]
# [1 1 1 1 1 1 1 0 1 0]
# [1 0 1 0 0 0 0 0 0 0]
# [0 1 1 1 0 1 0 0 1 1]]
print('Row indices :', hrows)
print('Col start ix :', hcols)
print('Col end ix :', hcols+1)
# Row indices : [0 0 0 1 2 4 4 5 5 5 5 6 6 7 7 7 7 7 7 9 9 9]
# Col start ix : [0 7 8 0 6 0 3 0 1 2 7 4 5 0 1 2 3 4 5 1 2 8]
# Col end ix : [1 8 9 1 7 1 4 1 2 3 8 5 6 1 2 3 4 5 6 2 3 9]
print('\nStart Row:', vrows, '\nEnd Row :',vrows+1, '\nColumn :', vcols)
# Start Row: [0 0 1 3 3 4 4 4 4 5 5 6 6 6 6 7 7 8]
# End Row : [1 1 2 4 4 5 5 5 5 6 6 7 7 7 7 8 8 9]
# Column : [0 1 4 0 3 0 1 3 8 1 5 1 4 5 6 0 2 2]
Can an element be in more than one pair? In the above it can be.
Do two diagonally touching elements count as a pair? If so a shifted_left_and_down array would be required too.
How to find the closest matching string pairs in 2D array
You do the coding yourself but this might get you started:
transform the 2D array to a flat list
L
with tuples(i, j, distance)
Sort the list
L
based ondistance
Pick the largest elements from the sorted list and do your comparison.
How to find how many values are divisible in to certain value in 2d array in python
You can use sum()
builtin for the task:
l = [[2, 10], [1, 6], [4, 8]]
print( sum(v % 3 == 0 for a, b in l for v in range(a, b+1)) )
Prints:
6
EDIT: To count number of perfect squares:
def is_square(n):
return (n**.5).is_integer()
print( sum(is_square(v) for a, b in l for v in range(a, b+1)) )
Prints:
5
EDIT 2: To print info about each interval, just combine the two examples above. For example:
def is_square(n):
return (n**.5).is_integer()
for a, b in l:
print('Pair {},{}:'.format(a, b))
print('Number of divisible 3: {}'.format(sum(v % 3 == 0 for v in range(a, b+1))))
print('Number squares: {}'.format(sum(is_square(v) for v in range(a, b+1))))
print()
Prints:
Pair 2,10:
Number of divisible 3: 3
Number squares: 2
Pair 1,6:
Number of divisible 3: 2
Number squares: 2
Pair 4,8:
Number of divisible 3: 1
Number squares: 1
Related Topics
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
How to Connect to a Remote Windows Machine to Execute Commands Using Python
How to Block Comment Code in the Ipython Notebook
Creating Random Pairs from Lists
How to Save Xlsm File With Macro, Using Openpyxl
Python List - Only Keep Only-Positive or Only-Negative Values
Python: Getting Around Division by Zero
How to Compute Mean() for Particular Column in Pandas Dataframe Without Considering Nan Values
Efficient Date Range Overlap Calculation
Calculate Angle (Clockwise) Between Two Points
Access Is Denied When Trying to Pip Install a Package on Windows
How to Extract Data from Text Field in Pandas Dataframe
Python While Loop for Finding Prime Numbers