Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
e241e8c1
Commit
e241e8c1
authored
Jan 03, 2014
by
John R. Lenton
Browse files
Implemented list.count
parent
26c21164
Changes
2
Hide whitespace changes
Inline
Side-by-side
py/objlist.c
View file @
e241e8c1
...
...
@@ -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
...
...
tests/basics/tests/list_count.py
0 → 100644
View file @
e241e8c1
# list count tests
a
=
[
1
,
2
,
3
]
a
=
a
+
a
+
a
b
=
[
0
,
0
,
a
,
0
,
a
,
0
]
print
(
a
.
count
(
2
))
print
(
b
.
count
(
a
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment