2016-12-28 42 views

回答

0

沒有本地方法。 您可以使用,例如:每行使用1行矩陣行,行間數字之間使用分隔符。像這樣。

matrix = [] 
DEFAULT_VALUE = 3 
DELIMITER = ' ' 

try: 
    N = int(input("Enter N:")) 
except: 
    print("Parsing error. Default N = 3") 
    N = DEFAULT_VALUE 

i = 0 
while i < N: 
    row = input("Enter row %d (cell delimiter: '%s'): " % (i + 1, DELIMITER)) 
    try: 
     parsed_row = list(map(int, row.split(DELIMITER))) 
    except: 
     print("Conversion to int error. Please repeat") 
     continue 

    if len(parsed_row) != N: 
     print("Please enter exactly %d numbers" % N) 
     continue 

    matrix.append(parsed_row) 
    i += 1 

print("Your matrix:") 
print("\n".join(map(lambda x: " ".join(map(lambda y: "%5d" % y, x)), matrix))) 
0

如果您的用戶(一個或多個)理解了Python/NumPy的語法,你可以讓你的程序接受有效的代碼輸入:

import numpy as np 
# Input from user. 
# Note that surrounding [] are allowed but not necessary 
s = '[1,2], [3,4]' 
# Convert input to matrix 
matrix = np.array(eval(s)) 
print(matrix) 

這種做法是相當強大的,因爲它是。例如,輸入'[(1,2), [3,4]]'產生相同的矩陣。

爲了進一步提高魯棒性(現在允許非法Python語法),你可以使用

import re, numpy as np 
# Input from user 
s = '(1 2) (3 4)' 
# Convert input to matrix 
matrix = np.array(eval(re.sub('\s+', ',', s.strip()))) 
print(matrix) 

現在使空間內使用(以及逗號,因爲之前)的矩陣元素分開。

0

那麼有多種方式來回答這個...

  1. 那麼,如果矩陣是N * N,與第一輸入線意味着你知道的輸入線數(不,它的輸入是不可能的( )不能以按鍵輸入結束)。因此,你需要這樣的事情:

    arr = [] 
    arr.append(input().split()) 
    for x in range(len(arr[0]) - 1): 
        arr.append(input().split()) 
    

    我使用範圍(LEN(ARR [0]) - 1),所以它輸入線的其餘部分(因爲矩陣的寬度和高度是相同的,並且一個第一線路已被從讀輸入)。

    另外我用.split()沒有「」作爲參數,因爲它是默認參數。

2.or您可以使用此...

>>> import math 
>>> line = ' '.join(map(str, range(4*4))) # Take input from user 
    '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15' 
>>> items = map(int, line.split()) # convert str to int 
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
>>> n = int(math.sqrt(len(items))) # len(items) should be n**2 
    4 
>>> matrix = [ items[i*n:(i+1)*n] for i in range(n) ] 
    [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] 

3.

嘗試這樣的事情,而不是使用現有列表(S)通過一個設置矩陣之一:

# take input from user in one row 
nn_matrix = raw_input().split() 
total_cells = len(nn_matrix) 
# calculate 'n' 
row_cells = int(total_cells**0.5) 
# calculate rows 
matrix = [nn_matrix[i:i+row_cells] for i in xrange(0, total_cells, row_cells)] 

實施例:

>>> nn_matrix = raw_input().split() 
    1 2 3 4 5 6 7 8 9 
>>> total_cells = len(nn_matrix) 
>>> row_cells = int(total_cells**0.5) 
>>> matrix = [nn_matrix[i:i+row_cells] for i in xrange(0, total_cells, row_cells)] 
>>> matrix 
    [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']] 
0

最接近numpy內建的是np.matrix functon。作爲MATLAB克隆它接受,除其他事項外,一個字符串:

In [1550]: np.matrix('1 2;3 4') 
Out[1550]: 
matrix([[1, 2], 
     [3, 4]]) 

結果是matrix子類,所以我建議將其轉換爲立即數組:與input

In [1551]: np.matrix('1 2;3 4').A 
Out[1551]: 
array([[1, 2], 
     [3, 4]]) 

和已婚:

In [1552]: np.matrix(input('>')).A 
>1 2 3; 4 5 6 
Out[1552]: 
array([[1, 2, 3], 
     [4, 5, 6]]) 

這並不強制垂直度。你必須提供那個測試。

這對小玩具輸入應該沒問題。但對於任何大事,我建議從文件輸入,用戶可以在手之前編輯。

np.loadtxtnp.genfromtxt是用於從csv格式文件創建數組的好工具。有很多關於使用這些的問題。

基本數組定義是一個列表的列表:

alist = [[1,2,3],[4,5,6]] 
arr = np.array(alist) 

所以任何可以構建從用戶輸入這樣的名單可以工作。例如,你可以接受來自該列表的結果「數字」

In [1558]: alist = ['1 2 3'.split(),'4 5 6'.split()] 
In [1559]: alist 
Out[1559]: [['1', '2', '3'], ['4', '5', '6']] 

使陣列線串D型

In [1560]: np.array(alist) 
Out[1560]: 
array([['1', '2', '3'], 
     ['4', '5', '6']], 
     dtype='<U1') 

,但你可以指定dtype(爲整數或浮點數)和array會嘗試轉換字符串。輸入後,您也可以將字符串轉換爲數字。

In [1561]: np.array(alist, dtype=int) 
Out[1561]: 
array([[1, 2, 3], 
     [4, 5, 6]]) 

你也可以接受[],但這些都是更多的處理痛苦。

相關問題