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:
JavaScript
x
8
1
predicts =
2
[['__label__1'],
3
['__label__0'],
4
['__label__1'],
5
['__label__1'],
6
['__label__0'],
7
['__label__1']]
8
i want to replace __label__1 with “OK” and __label__0 with “NOT OK” and save it in different variable using :
JavaScript
1
7
1
pred_label = []
2
for i in predicts:
3
if i == '__label__1':
4
pred_label.append("OK")
5
else:
6
pred_label.append("NOT OK")
7
but it failed to replace any of it
Advertisement
Answer
The predicts variable has two dimensions, try:
JavaScript
1
8
1
pred_label = []
2
for i in predicts:
3
if i[0] == '__label__1':
4
pred_label.append("OK")
5
else:
6
pred_label.append("NOT OK")
7
print(pred_label)
8