Implements list.remove (in terms of list.index and list.pop).
Fixes issue #63.
This commit is contained in:
parent
f1c6ad46af
commit
49fb6e53b3
11
py/objlist.c
11
py/objlist.c
|
@ -198,6 +198,15 @@ static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
|
||||||
|
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
|
||||||
|
mp_obj_t args[] = {self_in, value};
|
||||||
|
args[1] = list_index(2, args);
|
||||||
|
list_pop(2, args);
|
||||||
|
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
|
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
|
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
|
||||||
|
@ -205,6 +214,7 @@ static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
|
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
|
static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
|
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
|
||||||
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
|
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
|
||||||
|
|
||||||
const mp_obj_type_t list_type = {
|
const mp_obj_type_t list_type = {
|
||||||
|
@ -224,6 +234,7 @@ const mp_obj_type_t list_type = {
|
||||||
{ "index", &list_index_obj },
|
{ "index", &list_index_obj },
|
||||||
{ "insert", &list_insert_obj },
|
{ "insert", &list_insert_obj },
|
||||||
{ "pop", &list_pop_obj },
|
{ "pop", &list_pop_obj },
|
||||||
|
{ "remove", &list_remove_obj },
|
||||||
{ "sort", &list_sort_obj },
|
{ "sort", &list_sort_obj },
|
||||||
{ NULL, NULL }, // end-of-list sentinel
|
{ NULL, NULL }, // end-of-list sentinel
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
a = [1, 2, 3]
|
||||||
|
print(a.remove(2))
|
||||||
|
print(a)
|
||||||
|
try:
|
||||||
|
a.remove(2)
|
||||||
|
except ValueError:
|
||||||
|
print("Raised ValueError")
|
||||||
|
else:
|
||||||
|
raise AssertionError("Did not raise ValueError")
|
Loading…
Reference in New Issue