使用嵌套循环在Python中输入一个二维数组。

huangapple go评论67阅读模式
英文:

inputting a 2D array in python using Nested Loops

问题

你好,以下是翻译好的代码部分:

hello guys I'm new to python and I just wanted to give values to a 2D array using nested loops like how we were doing it in java and etc. but says list index out of range and I don&#39t know how to fix it. 

this is my code.

prices= [ [] *8] *20
images= []

for i in range (1,8):
images.append(i)

for page in range (8):
for price in range (20):
prices[page][price].append(input())


我没有在互联网上找到任何相关信息。

<details>
<summary>英文:</summary>

hello guys I&#39;m new to python and I just wanted to give values to a 2D array using nested loops like how we were doing it in java and etc. but says list index out of range and I don&#39;t know how to fix it. 

this is my code.

prices= [ [] *8] *20
images= []

for i in range (1,8):
images.append(i)

for page in range (8):
for price in range (20):
prices[page][price].append(input())



I didn&#39;t found any things in internet abut this.

</details>


# 答案1
**得分**: 0

```python
定义数组
```python
prices = []
images = []

并将其他数组附加到第一个数组中,这将创建一个二维数组。

for i in range(1, 8):
    images.append(i)

for page in range(8):
    for price in range(20):
        prices.append(['test', 1])

print(prices)
print(images)

输出

[['test', 1], ['test', 1], ['test', 1],
['test', 1], ['test', 1], ['test', 1], ['test', 1],
..........
['test', 1], ['test', 1], ['test', 1], ['test', 1],
['test', 1], ['test', 1], ['test', 1], ['test', 1]]
[1, 2, 3, 4, 5, 6, 7]
prices[0][0] -> test

prices[0][1] -> 1

<details>
<summary>英文:</summary>

Define arrays

prices = []
images = []


And append other array in first array, this will create 2D array

for i in range (1,8):
images.append(i)

for page in range (8):
for price in range (20):
prices.append(['test', 1])

print(prices)
print(images)


Output

[['test', 1], ['test', 1], ['test', 1],
['test', 1], ['test', 1], ['test', 1], ['test', 1],
..........
['test', 1], ['test', 1], ['test', 1], ['test', 1],
['test', 1], ['test', 1], ['test', 1], ['test', 1]]
[1, 2, 3, 4, 5, 6, 7]

prices[0][0] -> test

prices0 -> 1


</details>



# 答案2
**得分**: 0

当你使用 `[] * 8]` 这个语句时,它将创建一个包含8个指向相同空列表的引用的列表。这段代码将创建8行,每行包含20个元素。

```python
prices = []
for row in range(8):
    rows = []
    for col in range(20):
        rows.append(input())
    prices.append(rows)

输出:

这里添加了一张图片,显示它创建了每行包含20个元素的8行。

使用嵌套循环在Python中输入一个二维数组。

英文:

When you use [] *8] this statement it will create a list with 8 references to the same empty list. This piece of code will create 8 rows having 20 elements in each.

 prices = []
for row in range(8):
    rows = []
    for col in range(20):
        rows.append(input())
    prices.append(rows)

Output:

Here adding the picture which shows it creates 8 rows with 20 elements in each.

使用嵌套循环在Python中输入一个二维数组。

答案3

得分: 0

我搜索了很多关于它的信息,并向他们询问,他们告诉我这很容易,我只需使用NumPy库。它完美而简单地完成了我想要的事情。
像这样:

images=[]
for i in range(1,9):
    images.append(i)

prices=np.array([])

with open('prices.csv', mode='r') as file:
    csvFile=csv.reader(file)
    for line in csvFile:
        prices = np.append(prices,
])
prices = prices.reshape(8,20)

如您所请求,这是翻译好的代码部分。

英文:

I searched and asked a lot about it, and they told me it's so easy, I just had to use NumPy library. it done what I wanted so perfect and easy.
like this:

images=[]
for i in range(1,9):
   images.append(i)

prices=np.array([])

with open(&#39;prices.csv&#39;, mode =&#39;r&#39;)as file:
   csvFile=csv.reader(file)
   for line in csvFile:
       prices = np.append(prices,
]) prices = prices.reshape(8,20)

huangapple
  • 本文由 发表于 2023年2月23日 21:49:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定