I am trying to replace a string with another string in a list, but failed and I don’t know why.
For example, I have a list:
predicts = [['__label__1'], ['__label__0'], ['__label__1'], ['__label__1'], ['__label__0'], ['__label__1']]
i want to replace __label__1 with “OK” and __label__0 with “NOT OK” and save it in different variable using :
pred_label = [] for i in predicts: if i == '__label__1': pred_label.append("OK") else: pred_label.append("NOT OK")
but it failed to replace any of it
Advertisement
Answer
The predicts variable has two dimensions, try:
pred_label = [] for i in predicts: if i[0] == '__label__1': pred_label.append("OK") else: pred_label.append("NOT OK") print(pred_label)