我要澄清一下。
我正在記錄 6 個不同的變數,其中一些有錯誤(什么樣的錯誤無關緊要)。發生該錯誤時,我只想將該特定變數設定為“NA”。
這是我的意思的想法:
myDict = []
for i in data:
try:
eye = ...
nose = ...
mouth = ...
ear = ...
hair = ...
tongue = ...
myDict.append([eye, nose, mouth, ear, hair, tongue])
except eye:
eye = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
except nose:
nose = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
except mouth:
mouth = "NA"
myDict.append([eye, nose, mouth, ear, hair, tongue])
...
我必須為每個變數做一個“例外”嗎?有沒有什么辦法我可以做“除了任何變數有錯誤,將其值分配給“NA”并正常附加”?
我也不知道如果超過 1 個變數有錯誤會發生什么。
基本思想是:“如果變數(S)有錯誤,只需為它/它們分配值“NA”并繼續附加。
uj5u.com熱心網友回復:
這是一個可以滿足您要求的方法示例。
首先是一些評論:
- 您使用了術語“錯誤”,但您的示例代碼使用了 try/except,因此我假設每個“錯誤”都會導致引發例外。
- 在下面的代碼中,我使用了一個人為簡化的場景,其中對每個變數的賦值都有可能引發具有相似名稱的例外,并且我將這些例外創建為用戶定義的例外;實際上,您可能不需要定義這些,而是??將示例代碼中帶括號的例外型別替換為您想要捕獲的實際例外。
- 你有一個叫做的東西
myDict,它實際上是一個 python 串列;我將result在下面的代碼中重命名它以避免混淆。
下面代碼的邏輯可以概括為:
- 與其直接分配給命名變數(如您問題中的代碼),不如在內部回圈中迭代變數標簽串列(字串,如“eye”、“nose”等),該內部回圈中有一個 try/except 塊它;
- 在這個內部串列中,為給定標簽執行作業(與您的問題
eye = ...或mouth = ...在您的問題中完成的作業相同)并將結果附加到 listL,除非引發例外,在這種情況下,try 塊改為附加“NA “到L; - 這意味著無論是否有錯誤(更準確地說,是否引發例外),
L都會為每個變數標簽附加一個值; - 在內部回圈結束時,追加
L到結果。
這是示例代碼:
class eye_exception(Exception):
pass
class nose_exception(Exception):
pass
class mouth_exception(Exception):
pass
class ear_exception(Exception):
pass
class hair_exception(Exception):
pass
class tongue_exception(Exception):
pass
def getValueForEye(i):
return "eye_value" str(i)
def getValueForNose(i):
return "nose_value" str(i)
def getValueForMouth(i):
if i % 3 == 0:
raise mouth_exception()
return "mouth_value" str(i)
def getValueForEar(i):
return "ear_value" str(i)
def getValueForHair(i):
if i % 3 != 0:
raise hair_exception()
return "hair_value" str(i)
def getValueForTongue(i):
return "tongue_value" str(i)
data = [1, 2, 3]
result = []
for i in data:
L = []
for key in ['eye', 'nose', 'mouth', 'ear', 'hair', 'tongue']:
try:
match key:
case 'eye':
value = getValueForEye(i)
case 'nose':
value = getValueForNose(i)
case 'mouth':
value = getValueForMouth(i)
case 'ear':
value = getValueForEar(i)
case 'hair':
value = getValueForHair(i)
case 'tongue':
value = getValueForTongue(i)
L.append(value)
except (eye_exception, nose_exception, mouth_exception, ear_exception, hair_exception, tongue_exception):
L.append("NA")
result.append(L)
樣品result:
['eye_value1', 'nose_value1', 'mouth_value1', 'ear_value1', 'NA', 'tongue_value1']
['eye_value2', 'nose_value2', 'mouth_value2', 'ear_value2', 'NA', 'tongue_value2']
['eye_value3', 'nose_value3', 'NA', 'ear_value3', 'hair_value3', 'tongue_value3']
或者,如果您使用的 python 版本不支持 match/case 構造,或者您只是不想使用它,您可以使用以下代碼替換上面的回圈,該代碼使用字典從變數標記映射到函式:
funcDict = {
'eye':getValueForEye,
'nose':getValueForNose,
'mouth':getValueForMouth,
'ear':getValueForEar,
'hair':getValueForHair,
'tongue':getValueForTongue
}
for i in data:
L = []
for key, func in funcDict.items():
try:
value = func(i)
L.append(value)
except (eye_exception, nose_exception, mouth_exception, ear_exception, hair_exception, tongue_exception):
L.append("NA")
result.append(L)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/473226.html
