日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python基礎之矩陣輸入的實例_python

作者:ThitherShore ? 更新時間: 2022-07-04 編程語言

Python矩陣輸入

經常在嘗試python一些函數功能時想隨便輸入一個矩陣感覺怪麻煩……python是拿list表示數組的,畢竟不是矩陣(Matrix)實驗室(Laboratory)嘛2333

Python直接復制格式標準的數據是可以識別成list的,但我要是輸入一個規(guī)整的矩陣就繁瑣了些。比如這種

1     4     7    10
2     5     8    11
3     6     9    12

Python里面可以這樣輸入

count = 1;A = []
for i in range(0, 3):
? ? temp = []
? ? for j in range(0, 4):
? ? ? ? temp.append(count)
? ? ? ? count += 1
? ? A.append(temp)
print A

輸出為

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

事實上在matlab里面很方便,就兩句

A=1:12;
B=reshape(A,3,4);

又比如下面這段輸出這樣的矩陣[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

A = []
for i in range(0, 3):
? ? tmp = []
? ? for j in range(0, 3):
? ? ? ? tmp.append(j)
? ? A.append(tmp)
print A

python輸入(數組、矩陣)

一維輸入對應不同變量

n,x,y = map(int,input().split())
print(n,x, y)

輸入:10 2 3

輸出: 10 2 3

一維數組的輸入問題

arr = input()
arr = [int(n) for n in arr.split()]
print(arr)

輸入:1 9 0 0 1

輸出:[1, 9, 0, 0, 1]

二維數組的輸入問題

n = int(input())
arr = []
for i in range(n):
?? ?arr.append(list(map(int, input().rstrip().split())))
print(arr)

輸入:

3

1 2 3
4 2 9
0 3 8

輸出:

[[1, 2, 3], [4, 2, 9], [0, 3, 8]]

輸入 :

4

?1 3?
98 1?
2 7
2 9

輸出:

[[1, 3], [98, 1], [2, 7], [2, 9]]

這里的rstrip()函數為刪除結尾處的空白符

原文鏈接:https://blog.csdn.net/thither_shore/article/details/52333976

欄目分類
最近更新