区别1:
- List中可添加重复的元素
- Set中不可添加重复的元素
- Map中键不可以重复但是值可以重复
区别2:
- List和Set是存储单列数据的集合,Map是存储键值对的双列数据的集合;
1. Python中的map()函数
-
用法:map(function, iterable, …)
-
参数function: 传的是一个函数名,可以是python内置的,也可以是自定义的。
参数iterable :传的是一个可以迭代的对象,例如列表,元组,字符串… -
功能: 将iterable中的每一个元素执行一遍function
栗子:
list_num1 = [1, 2, 3, 0, 8, 0, 3]
list_num2 = [1, 2, 3, 4, 6.6, 0, 9]
print(list(map(lambda x, y: x+y, list_num1, list_num2)))
2. Set删除列表中的重复元素
栗子:
a = [1, 1, 2, 4, 5]
print(list(set(a)))
3. List移除列表中的元素
- list.pop(index):用于从列表中删除下标为index的元素,该语句还能返回被删除的元素
栗子:
a = [0, 1, 2, 3, 4, 5]
print(a.pop(1)) # 0
print(a) # [0,2,3,4,5]
- list.remove(value):用于从列表中删除元素value
栗子:
a = [0, 1, 2, 3, 4, 5]
print(a.remove(1)) # None,不会有返回值
print(a) # [0,2,3,4,5]