我已經做了下面的代碼,它的工作正常,但是我試圖做出更具可讀性和優化的代碼,爲此我將我的代碼替換爲列表理解來替換所有「如果條件」尋找emotion_class(雙**)。列表理解多標籤計數器
試圖優化**代碼到列表理解:
emotion_classes = ["Joy", "Fear", "Anger", "Surprise","Sadness", "Trust","Anticipation"]
[increaseCounterofJoy,increaseCounterofFear, for emotion in emotion_classes]
代碼
for idx in range(len(df_split)):
iter_df = df_split[idx]
i = 0
final_df = pd.DataFrame(columns=("Date","Joy", "Fear", "Anger", "Surprise","Sadness", "Trust","Anticipation"))
for index, row in iter_df.iterrows():
**if iter_df["Emotion Class"] = "Joy":
row["Joy"] = +1
if iter_df["Emotion Class"] = "Fear":
row["Fear"] = +1
if iter_df["Emotion Class"] = "Anger":
row["Anger"] = +1
if iter_df["Emotion Class"] = "Surprise":
row["Surprise"] = +1
if iter_df["Emotion Class"] = "Sadness":
row["Sadness"] = +1
if iter_df["Emotion Class"] = "Trust":
row["Trust"] = +1
if iter_df["Emotion Class"] = "Anticipation":
row["Anticipation"] = +1**
final_df.loc[i] = row["Date"], row["Joy"], row["Fear"], row["Anger"], row["Surprise"], row["Sadness"], row["Trust"], row["Anticipation"]
i = i + 1
我從來沒有見過像使用了''** ...操作 – bendl
等等,只是強調我們應該看的地方? – bendl
他只是用它來標記一個區域,查看if鏈的最後一行 – scnerd