python2
- 1.list和tuple的区别
- 2.学到的序列有哪几种
- 3.list中所有方法的使用
- 4.将元组和列表转换成字典
- 5.dict中所有方法的使用
- 6.list或dict浅拷贝画图加代码解释
1.list和tuple的区别
list 是一个长度可变,内容可变的序列
tuple 是一长度不可变,内容不可变的序列,但是,如果元组中套用列表或其他可变序列,由于列表和其他可变序列可变,所以一旦列表发生改变,元组中的列表也会发生改变,导致元组发生改变。
str 字符串
byte 字节
tuple 元组
list 列表
dict 字典
| append(self, object, /)
| Append object to the end of the list.
| # 在list的最后添加一个元素
"""
list_var = [1, 2, 3]
list_var.append(4)
print(list_var)
"""
| clear(self, /)
| Remove all items from list.
| # 删除list中所有的元素
list_var = [1, 2, 3]
list_var.clear()
print(list_var)
| copy(self, /)
| Return a shallow copy of the list.
| #
| count(self, value, /)
| Return number of occurrences of value.
| # 统计元素出现得次数(执行时没找到不会报错)
"""
list_var = [1, 2, 3, 1]
number = list_var.count(1)
print(number)
"""
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.
# 扩张列表 将一个可迭代对象(序列)添加到list的最后面
"""
list_var = [1, 2, 3, 1]
str_var = "123456789"
byte_var = b'123456789'
tuple_var = (1, 2, 3, 4)
list_var1 = [1, 2, 3, 1]
list_var1.extend(str_var)
print(list_var1)
list_var1.extend(list_var)
print(list_var1)
list_var1.extend(byte_var)
print(list_var1)
list_var1.extend(tuple_var)
print(list_var1)
"""
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
| # 返回元素第一次出现的下标(如果值不存在,会报错)
| Raises ValueError if the value is not present.
"""
list_var1 = [1, 2, 3, 1]
print(list_var1.index(1, 1, 4))
"""
|
| insert(self, index, object, /)
| Insert object before index.
| # 在指定的下标处添加一个元素(index,value)
"""
list_var1 = [1, 2, 4]
list_var1.insert(2, 3)
print(list_var1)
"""
| pop(self, index=-1, /)
| Remove and return item at index (default last).
# 弹出对应下标的值
| """
list_var = [1, 2, 3]
number = list_var.pop()
print(number)
print(list_var)
list_var1 = [1, 2, 3]
number1 = list_var1.pop(1)
print(number1)
print(list_var1)
"""
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
| # 删除传入的值(第一个),值不存在,则会报错
"""
list_var = [1, 2, 2, 3]
list_var.remove(2)
print(list_var)
"""
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
| # 反转倒置(不进行排序)
"""
list_var = [1, 3, 2]
list_var.reverse()
print(list_var)
"""
| sort(self, /, *, key=None, reverse=False)
| Sort the list in ascending order and return None.
| # 正向排序
"""
list_var = [1, 3, 5, 4, 6, 9, 8]
list_var.sort()
print(list_var)
"""
# 自己改变规则
"""
def sort1(str_data):
return str_data[-1], str_data[-2], str_data[-1]
# return str_data[-1]
str_list = ['banana', "drain", "pineapple", "cherry", "strawberry", "kiwifruit", "peach", "grape"]
str_list.sort()
print(str_list)
str_list.sort(reverse=True)
print(str_list)
str_list.sort(key=sort1)
print(str_list)
"""
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
extend 和 append的区别
extend是将一个序列里面的每一个元素给存储到列表里
append是将一个序列(一个元素)作为一个元素给存储到列表里
tuple_var = (1, 2, 3, 4) list_var1 = [1, 2, 3, 1] list_var1.extend(tuple_var) print(list_var1) list_var1.append(tuple_var) print(list_var1)4.将元组和列表转换成字典
dict() key: value 序列(每个元素包含两个元素的序列)中应该包含两部分,一部分对应字典的key,一部分对应字典的value
tuple_var = ((1, 2), (3, 4)) list_var = [[1, 2], [3, 4]] dict_var1 = dict(tuple_var) dict_var2 = dict(list_var) print(dict_var1) print(dict_var2)5.dict中所有方法的使用
clear(...)
| D.clear() -> None. Remove all items from D.
# 清空字典
"""
dict_var = {1: 2, 2: 3}
print(dict_var)
dict_var.clear()
print(dict_var)
"""
|
| copy(...)
| D.copy() -> a shallow copy of D
# 拷贝
|
| get(self, key, default=None, /)
| Return the value for key if key is in the dictionary, else default.
| # 得到key为?的值,如果key不存在,返回None
"""
dict_var = {1: 2, 2: 3}
print(dict_var)
var = dict_var.get(1)
var1 = dict_var.get(3)
print(var, var1)
"""
| items(...)
| D.items() -> a set-like object providing a view on D's items
# 返回字典中所有键值对
| """
a = {'数学': 95, '语文': 89, '英语': 90}
print(a.items())
"""
| keys(...)
| D.keys() -> a set-like object providing a view on D's keys
| # 用于返回字典中所有的键。
"""
a = {'数学': 95, '语文': 89, '英语': 90}
print(a.keys())
"""
| pop(...)
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised
| # 弹出字典中key对应的值
"""
dict_var = {1: (1, 2, 3), 2: 3}
var = dict_var.pop(1)
print(var)
print(dict_var)
"""
| popitem(self, /)
| Remove and return a (key, value) pair as a 2-tuple.
| # 弹出字典中最后一个键值对,并为元组
'''
dict_var = {1: (1, 2, 3), 2: 3, 7: 4}
var = dict_var.popitem()
print(var)
print(dict_var)
'''
| Pairs are returned in LIFO (last-in, first-out) order.
| Raises KeyError if the dict is empty.
|
| setdefault(self, key, default=None, /)
| Insert key with a value of default if key is not in the dictionary.
|
| Return the value for key if key is in the dictionary, else default.
|
| update(...)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
| If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
|
| values(...)
| D.values() -> an object providing a view on D's values
# 返回字典所有键对应的值
"""
a = {'数学': 95, '语文': 89, '英语': 90}
print(a.values())
"""
6.list或dict浅拷贝画图加代码解释
b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)
a,b 只要其中一个发生改变,两个都发生改变
a = {1: [1,2,3]}
b = a.copy()
print(a, b)
# {1: [1, 2, 3]}, {1: [1, 2, 3]}
a[1].append(4)
print(a, b)
# {1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]}
print(id(a[1]), id(b[1]))
# 2698953608384, 2698953608384
b = copy.deepcopy(a): 深度拷贝, a 和 b 完全拷贝了父对象及其子对象,两者是完全独立的。
a, b 不管其中那一个发生改变,互不影响。
深拷贝
import copy
a = {1: [1,2,3]}
b = copy.deepcopy(a)
print(a, b)
# {1: [1, 2, 3]}, {1: [1, 2, 3]}
a[1].append(5)
print(a, b)
# {1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]}
print(id(a[1]), id(b[1]))
# 2307260074176 2307259917760



