diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-03 23:57:28 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-03 23:57:28 +0000 |
commit | e241e8c1698e5299deb10b718ffa30e2cca39301 (patch) | |
tree | 9a876bf692abd7d206f5c3f481911b5a70617ff7 /py/objlist.c | |
parent | 26c211648bc00085233ece9c53f19a64ecca41fa (diff) | |
download | micropython-e241e8c1698e5299deb10b718ffa30e2cca39301.tar.gz micropython-e241e8c1698e5299deb10b718ffa30e2cca39301.zip |
Implemented list.count
Diffstat (limited to 'py/objlist.c')
-rw-r--r-- | py/objlist.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/py/objlist.c b/py/objlist.c index e371057981..8b841d4bf0 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -140,9 +140,23 @@ static mp_obj_t list_copy(mp_obj_t self_in) { return mp_obj_new_list(self->len, self->items); } +static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) { + assert(MP_OBJ_IS_TYPE(self_in, &list_type)); + mp_obj_list_t *self = self_in; + int count = 0; + for (int i = 0; i < self->len; i++) { + if (mp_obj_equal(self->items[i], value)) { + count++; + } + } + + return mp_obj_new_int(count); +} + 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_copy_obj, list_copy); +static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count); static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop); static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); @@ -159,6 +173,7 @@ const mp_obj_type_t list_type = { { "append", &list_append_obj }, { "clear", &list_clear_obj }, { "copy", &list_copy_obj }, + { "count", &list_count_obj }, { "pop", &list_pop_obj }, { "sort", &list_sort_obj }, { NULL, NULL }, // end-of-list sentinel |