习题
Write a function remove_duplicates
that takes in a list and removes elements of the list that are the same.
For example: remove_duplicates([1, 1, 2, 2])
should return [1, 2]
.
- Don't remove every occurrence, since you need to keep a single occurrence of a number.
- The order in which you present your output does not matter. So returning
[1, 2, 3]
is the same as returning[3, 1, 2].
自己解:
def remove_duplicates(raw_list):
d = {}
new_raw_list = []
for item in raw_list:
s_count = 0
for i in range(len(raw_list)):
if raw_list[i] == item:
s_count += 1
if s_count >=1:
d[item] = s_count
new_raw_list = d.keys()
return new_raw_list
看了提示的解:
def remove_duplicates1(raw_list):
new_raw_list = []
for item in raw_list:
if item not in new_raw_list:
new_raw_list.append(item)
return new_raw_list
总结:
... ...