1
arr = [
"xxyyyxxxx",
"xxxeeexxx",
"xwwwxxxxx",
]

I've seen code similar to this used to simulate 2 dimensional arrays in python by parsing the contents using for in row and for in col. Using this method what would be the easiest way to identify a specific "index" (or rather the location of a character within a certain string). If you don't have to modify the array and having to type out the entire array isn't an issue would there still be a better way to simulate a 2 dimensional array?

3
  • 1
    Can you provide more specifications about your questions ? Please be more descriptive about the issue with a test-case if possible Commented Dec 12, 2013 at 10:17
  • 1
    What is your question again? The one above seems confusing. One sentence, perhaps? Commented Dec 12, 2013 at 10:17
  • Two answers and yet the question isn't even clear. Great! Commented Dec 12, 2013 at 10:21

2 Answers 2

1

Strings are immutable sequences that can be indexed just like lists. So here,

arr[0][2]

Would take the string with index 0, and from that the character with index 2 -- "y". So that works.

Better ways to do it depends on what you need to do exactly. Real 2D arrays are available in Numpy.

Sign up to request clarification or add additional context in comments.

Comments

0

to get the position of a specific character you could do a 2D loop like so

for i in range(len(arr)):
    for j in range(len(arr[i])):
        if arr[i][j]=="e":
            print str(i), ", ", str(j)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.