user1131308
user1131308

Reputation: 309

removing characters from a list of strings

I have a character matrix that looks something like this:

["AAAAAAAA",
 "AAAXAAAA",
 "AAAAAAAA"]

I have numeric x/y coordinates for the location of the x and I want to get a string that contains all the letters in that matrix except for the x, what is the best way to do this?

Upvotes: 1

Views: 99

Answers (2)

arthaigo
arthaigo

Reputation: 453

Ok, here is a solution using Python's awesome list comprehension:

coordinates = (1,3) # (line, column)
matrix = ["AAAAAAAA",
    "AAAXAAAA",
    "AAAAAAAA"]

matrix = [list(line) for line in matrix] # convert the strings to create a "real" matrix" 
del matrix[coordinates[0]][coordinates[1]] # delete the specified element

"".join([item for line in matrix for item in line]) # flatten out the matrix and creating a string

Upvotes: 0

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

Not the most efficient, but a starting point:

>>> matrix = [
...     "AAAAAAAA",
...     "AAAXAAAA",
...     "AAAAAAAA"]
>>> x, y = 4, 2

>>> linear = list(itertools.chain.from_iterable(matrix))
>>> del linear[x - 1 + (y - 1) * 8]
>>> ''.join(linear)
'AAAAAAAAAAAAAAAAAAAAAAA'

Upvotes: 1

Related Questions