How to convert Array in C++ to Python? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question 开发者_如何学编程How can I convert this C++ code
for(int i=0; i<3; ++i){
array[i][0]=i*4+0;
array[i][1]=i*4+1;
array[i][2]=i*4+2;
array[i][3]=i*4+3;}
to Python ?
When your IDE says xrange
is an undefined function,please look at @ThiefMaste'comments :
Just because your IDE says something is not defined, it doesn't mean it's not defined. However, if you are using Python3 it is not defined since it was renamed to range (and the original, non-iterator range was removed)
How about this:
In [3]: [[i*4+j for j in xrange(4)] for i in xrange(3)]
Out[3]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
array = []
for i in range(3):
list.append([i*4, i*4+1, i*4+2, i*4+3])
or to be more exact (in case the list is not empty before):
for i in range(3):
list[i] = [i*4, i*4+1, i*4+2, i*4+3]
and a more pythonic approach to generate the 4 elements would be:
[i*4+n for n in range(4)]
精彩评论