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?
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)
range(1, 11) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> 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.
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
>>> G = [[0]*5]*5 >>> G[0][0]=2 >>> GG = [[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.
cin >>is C++ not C. Array of what: characters? integers? strings?[[int(raw_input()) for _ in range(0, 11)] for _ in range (0, 11)]but I don't really get the 'index from 1' partG[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.