# 將參數（x）指定爲具有特定列數的多維數組？

2017-11-11 18 views
1
``````import numpy as np

def validation(x):
x = np.asarray(x)
if len(x) != 16:
return("Card doesn't have exactly 16 digits. Try again")
values = []
rwhat = x[::-1] # reverse the order of the credit card numbers
rwhat

checkDig = rwhat[0] # the leftmost [originally rightmost] digit which is the checkDigit ... I'm just doing this because it's easier for me to work with
checkDig
withCheck = [] # to append later when we add all single digits

everySec = rwhat[1:16:2] # we don't want to double the checkDigit, but we're extracting every second digit starting from the first, leftmost digit [tho we omit this checkDigit
everySec

def double(num): # to double the extracted second digit values
return [j * 2 for j in everySec]
xx = double(everySec)
xx

def getSingle(y): # to add the sum of the digits of any of the new doubled numbers which happen to be greater than 9
u = 0
while y:
u += y % 10
y //= 10
return u
yy=list(map(getSingle,xx))
yy
withCheck.append(checkDig)
withCheck
new_vv = withCheck + yy
new_vv # now we include the omitted checkDigit into this new list which should all be single digits

sumDig = sum(new_vv)
sumDig # now have the sum of the the new_vv list.

def final(f):
if sumDig % 10 == 0: # if the calculated sum is divisible by 10, then the card is valid.
return("Valid")
else:
return("Invalid")
go = final(sumDig)
values.append(go) # basically just appending into values[] for the sake of the validation(x) function, and so we can return something for this function. in this case we'd return values as seen below.
return values
``````

`def validation(x)`工作內的東西，我已經在實際製作上述函數之前測試過它，但我只是不知道如何指定這個函數[也就是這個程序基本上是]在一個多維數組中16列。

+0

+0

+0

## 回答

0

``````assert len(x.shape) == 2, "input must be 2D"
assert x.shape[1] == 16, "input must have 16 columns"
assert np.issubdtype(x.dtype, np.integer), "input must be integers"
``````
+0

+0

「Two Dee」不是「二十」。第一行檢查輸入是否有兩個維度，例如， 1D或3D等 –

+0

2

• 斷言，所有的數字都在範圍0-9：

``````assert np.all((x >= 0) & (x <= 9))
``````
• 請注意您是使用行還是使用列。如果您有`n`行，每行16列，`checkDig`應該是`x[:, 0]`，這是第一列，而不是`x[0]`，這是第一行，相當於`x[0, :]`

• 無需反轉陣列：`checkDig`只是最後一個元素：`x[:, -1]`; `everySec`變成`x[:, 1:-1:2]`。考慮到它的使用方式，沒有必要對其進行逆轉。
• 功能`double`僅僅是一個爛攤子：

1. 聲明一個未使用的參數`num`
2. 你然後`everySec`運行在封閉命名空間
3. 您應用列表理解到numpy的陣列，這是比較慢，很難理解，並不會爲二維數組正常工作。

你可以只用`xx = everySec * 2`取代它，甚至擺脫`xx`，只是做`everySec *= 2`

• `getSingle`是矯枉過正。你的數字是九倍以下，所以結果不能超過兩位數（總和不能超過九位）。 `yy = (xx // 10) + (xx % 10)`應該做得很好。通過維護numpy數組而不是列表，您可以使所有操作適用於2D數組，而不必遍歷列表中的所有單個元素。
• 您操作的其餘都有點不清楚。您似乎正在執行Luhn algorithm，但沒有嘗試添加非加倍數字。非加倍數字`x[:, :-1:2]`
• 中調用內建`sum`將阻止你沒有一個循環處理多個輸入。使用`np.sum``axis=1`對每行中的列進行求和。
• `values.append(go)`只調用一次。如果你想處理多個數字，你將不得不編寫某種循環。讓`go`成爲布爾數組而不是單個布爾值會容易得多。

``````def validation(x):
x = np.asanyarray(x)
assert x.ndim == 2, "input must be 2D"
assert x.shape[1] == 16, "input must have 16 columns"
assert np.issubdtype(x.dtype, np.integer), "input must be integers"
assert np.all((x >= 0) & (x <= 9))
checkDig = x[:, -1]
xx = x[:, 1:-1:2] * 2
yy = x[:, :-1:2]
sumDig = np.sum(xx, axis=1) + np.sum(yy, axis=1) + checkDig
return ['Invalid' if s % 10 else 'Valid' for s in sumDig]
``````

``````def validation(x):
x = np.array(x, copy=True, subok=True)
assert x.ndim == 2, "input must be 2D"
assert x.shape[1] == 16, "input must have 16 columns"
assert np.issubdtype(x.dtype, np.integer), "input must be integers"
assert np.all((x >= 0) & (x <= 9))
y = x[1:-1:2]
x[1:-1:2] = ((2 * y) // 10) + ((2 * y) % 10)
sumDig = np.sum(x, axis=1)
return ['Invalid' if s % 10 else 'Valid' for s in sumDig]
``````