如何在python中实现这个C ++源代码? [英] How to implement this C++ source in python?

查看:101
本文介绍了如何在python中实现这个C ++源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

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

这是一个二维数组G,我要存储来自G [1] 1],而不是从零开始。

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

如何在python中实现这个代码?

How to implement this code in python?

推荐答案

>>> 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]]

code> random.randint 代替 input() raw_input()来显示这个工作。

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

这篇关于如何在python中实现这个C ++源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆