-1

This is the code:

for(int i=1; i<=10; ++i)
    for(int j=1; j<=10; ++j)
        cin >> G[i][j];

This is a two dimension array G, and I want to store the data from G[1][1], not from zero.

How to implement this code in python?

4
  • 3
    cin >> is C++ not C. Array of what: characters? integers? strings? Commented Apr 13, 2012 at 8:38
  • 2
    I'd do something like [[int(raw_input()) for _ in range(0, 11)] for _ in range (0, 11)] but I don't really get the 'index from 1' part Commented Apr 13, 2012 at 8:38
  • Not sure why you would need to do something like this... Commented Apr 13, 2012 at 8:58
  • Why would you "want to store the data from G[1][1]", when the array will start at 0 no matter what you "want", and [10] will not be a valid index unless the array size is at least [11]? C++ doesn't care about what you want; it cares only about what you tell it to do. Commented Apr 13, 2012 at 9:17

5 Answers 5

3
for i in xrange(1, 11):
    for j in xrange(1, 11):
        G[i][j] = raw_input()

or a more pythonic way, assuming G is empty before your loops:

G = [[raw_input() for j in xrange(1, 11)] for i in xrange(1, 11)]

(doesn't work, those lists would have the first value at element 0 which is not what the OP wants)

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

4 Comments

what's G's type? if it's a list or tuple, it's still 0-based, which may not be what he wants.
also remove that ugly semicolon!
@ott--: The end value is exclusive. range(1, 11) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
the first one is more readable than the second one. Or maybe I'm not used to pythonic ways...
2
>>> import random
>>> G = [[0]*11 for _ in range(11)]
>>> G
[[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], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> for i in range(1, 11):
        for j in range(1, 11):
            G[i][j] = random.randint(1,10)


>>> G
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 7, 8, 10, 5, 7, 9, 8, 10, 8, 3], [0, 7, 9, 7, 7, 6, 6, 10, 2, 8, 9], [0, 10, 6, 3, 7, 10, 7, 9, 6, 1, 7], [0, 5, 7, 1, 10, 3, 3, 1, 2, 5, 6], [0, 7, 3, 5, 4, 4, 2, 10, 10, 8, 1], [0, 10, 3, 3, 5, 4, 4, 2, 5, 3, 1], [0, 6, 6, 6, 6, 2, 3, 8, 1, 6, 4], [0, 7, 8, 5, 8, 1, 9, 5, 5, 2, 9], [0, 3, 2, 4, 1, 1, 4, 7, 7, 5, 5], [0, 3, 4, 10, 1, 2, 5, 3, 10, 9, 7]]

random.randint was used in place of input() or raw_input() to show that this works.

Comments

2

Initialising G:

G = [[0 for i in xrange(11)] for i in xrange(11)]

or G = [[0]*11]*11

Populating G:

for i in xrange(10):
    for j in xrange(10):
        G[i+1][j+1] = int(raw_input())

If you want to construct G in-place, you could do it with a nested list comprehension similar to the initialisation

9 Comments

@PreetKukreti, try: >>> G = [[0]*5]*5 >>> G[0][0]=2 >>> G
G = [[0]*11]*11 is the wrong way to initialize G. The same object is shared between G[i][0]. When you modify G[0][0], it'll also modify all G[i][0] value.
@warwaruk yep you are right thanks, 11 references to the same list. Not sure how I missed that
@PreetKukreti, this is classical :)
I thought the same thing so i did mine correctly then when i saw @Preet's code i thought that was quicker but forgot to test it...
|
1

Using dictionaries:

G = {}
for i in xrange(1, 11):
    G[i] = {}
    for j in xrange(1, 11):
        G[i][j] = raw_input()

Using lists (but starting indexes from 0):

G = []
for _ in xrange(1, 11):
    row = []
    G.append(row)
    for _ in xrange(1, 11):
        row.append(raw_input())

Comments

1

If readability counts, this is also quite readable

G=[[0]*11 for _ in range(11)]
for (i,j) in itertools.product(range(1,11),range(1,11)):
    while True:
        try: 
            G[i][j]=int(raw_input())
            break
         except ValueError:
            None

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.