-1

I am making a project that for every picture that has taken, it will be stored in a 2d array. This is a picture of a Scrabble that I have converted into an numpy array.

arr1 = [['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' 'A' 'P' 'P' 'L' 'E' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']]

After doing this I tried to get the coordination of the letters using coordinate_tiles = np.argwhere(arr1 != '0') After using a code to get the word apple. How do I store it in a 2d array, with coordinations for example,

temp = [['apple'], [[7, 5],
       [7, 6],
       [7, 7],
       [7, 8],
       [7, 9]]]
Diego
  • 9
  • 2
  • That's what `argwhere` gives so where is the problem? – a_guest Jan 31 '19 at 13:14
  • I need to store them in a 2d array 1 array for the words and the other is for coordination. – Diego Jan 31 '19 at 13:20
  • I would recomend using a dict: `temp = {"apple": np.argwhere(arr1 != '0')}` – JohnTanner Jan 31 '19 at 13:24
  • Just curious - this is the 3rd? 4th? crossword/scrabble task-like question about "how to locate/find stuff in a grid in the last week - all by different users. Do you have a group project? You can look into [how-to-get-the-characters-in-row-and-column](https://stackoverflow.com/questions/54265653/how-to-get-the-characters-in-row-and-column) and [how-do-i-replace-a-specific-string-in-a-2d-array](https://stackoverflow.com/questions/54366184/how-do-i-replace-a-specific-string-in-a-2d-array) - maybe you can join up and create something really marvellous... – Patrick Artner Jan 31 '19 at 13:35
  • Oh yes. We are a group and we are trying to make a project about scrabble. It would be really helpful if you could help us. – Diego Jan 31 '19 at 13:41
  • "1 array for the words and the other is for coordination" That is 2 arrays not one 2d array. What do you really want? – Stop harming Monica Jan 31 '19 at 13:42
  • I really want to store the words for example for every image I take I want to store them. If another picture has been taken meaning a new word has been created. I want to check if already exist but sometimes in scrabble there could be two or more words that can exist so that is where the coordination will come and check if the coordinates are different so that for example 2 apples can be stored – Diego Jan 31 '19 at 13:45

1 Answers1

1

It seems like a dictionary with the word as the key and the coordinates as the value may work better to keep track of words and their coordinates on the board.

# Create empty dictionary
temp = dict()

# Add word and coordinates to dictionary
if 'apple' not in temp:
    temp['apple'] = [[7, 5],
                     [7, 6],
                     [7, 7],
                     [7, 8],
                     [7, 9]]

# Add leek to dictionary
if 'leek' not in temp:
    temp['leek'] = [[...]]

Then you could do

# Access coordinates for apple
apple_loc = temp['apple']

# Access coordinates for leek
leek_loc = temp['leek']

to find coordinates for apple, or any other word.

fstop_22
  • 971
  • 5
  • 9
  • maybe you should mention that the dict needs to be initialized first with: `temp = dict()` – JohnTanner Jan 31 '19 at 13:26
  • What about it more words has been producing like for example leak in the l on apple. How can I also store them? – Diego Jan 31 '19 at 13:29
  • Just add another key value pair to dictionary. Seed updated answer. – fstop_22 Jan 31 '19 at 13:31
  • 1
    Who forbids to use `apple` in scrabble multiple times? Dictionary might not be wise. – Patrick Artner Jan 31 '19 at 13:31
  • You would have to check if key is in dict before adding it. – fstop_22 Jan 31 '19 at 13:32
  • I wanted to make a 2d array so that I could also check if same words have been created. For example if it see another apple it could check if its coordination are the same – Diego Jan 31 '19 at 13:37
  • See https://stackoverflow.com/questions/10664856/make-dictionary-with-duplicate-keys-in-python for possible solution to duplicate keys. – fstop_22 Jan 31 '19 at 13:48