a={1:[1,2]}
c,b=a[1]
print(c,b,type(c)) #1 2 <class 'int'>
c=a[1]
print(c,type(c)) #[1, 2] <class 'list'>
a={1:(1,2)}
c,b=a[1]
print(c,b,type(c)) #1 2 <class 'int'>
c=a[1]
print(c,type(c)) #(1, 2) <class 'tuple'>
a={1:{1,2}}
c,b=a[1]
print(c,b,type(c)) #1 2 <class 'int'>
c=a[1]
print(c,type(c)) #{1, 2} <class 'set'>
注:str 也是不可变的,不可以增删改
如:s='hello' s=s+'world' 会导致id(s) 发生变化 而:lst.append(300) id 不变
都可以for来遍历
列表:sort倒序为true 可以变化更改 ,可以排序
Lst=list(['hello','word',98,98])
print(Lst[0],Lst.index(98)) #hello 2
Lst=[10,20,30,40,50,60,70,80]
Lst1=Lst[-1:6:-1] #start stop step -1就是最后一个,倒序
print(Lst1) #[80]
Lst=[10,20,30,40,50,60,70,80]
Lst1=Lst[1:6:2] #[20, 40, 60]
Lst.append(100) #[10, 20, 30, 40, 50, 60, 70, 80, 100]
Lst.extend(Lst1) #[10, 20, 30, 40, 50, 60, 70, 80, 100, 20, 40, 60]
Lst[1:3]=[] #[10, 40, 50, 60, 70, 80, 100, 20, 40, 60]
Lst.sort(reverse=True) #[100, 80, 70, 60, 60, 50, 40, 40, 20, 10]
New_lst=sorted(Lst,reverse=True)
Lst.sort(reverse=False) #[10, 20, 40, 40, 50, 60, 60, 70, 80, 100]
Lst.append(Lst1) #[10, 20, 40, 40, 50, 60, 60, 70, 80, 100, [20, 40, 60]]
Lst.insert(1,90) #[10, 90, 20, 40, 40, 50, 60, 60, 70, 80, 100, [20, 40, 60]]
Lst.remove(70) #把70去了
Lst.pop(1) #把第二个数去了
Lst=[i*i for i in range(1,10)]
Lst.clear() #清空值
del Lst #删除列表
字典:可以变化更改,不可排序,键不可重复
d=dict(name='jack',age=20) #{'age': 20, 'name': 'jack'}
d1={'age':20}
d['name']
d.get('name')
d2=d1.get('name','jack') #'jack'
d.keys() #dict_keys(['name', 'age'])
d.values() #dict_values(['jack', 20])
d.items() #dict_items([('name', 'jack'), ('age', 20)])
for c in d:
print(c) #name age
Items=['fruits','books','others']
Price=[98,22,11,25]
d2={item.upper():prise for item, prise in zip(Items,Price)} #元素少的做标准
#{'BOOKS': 22, 'FRUITS': 98, 'OTHERS': 11}
d3 = {name for name,score in d2.items() if score >= 90} #生成式
print(d3) #{'FRUITS'}
元组:
t=()
t=tuple()
t2=('python','world',98)
t3=(1,)
print(t[1]) #world
print((1,1)+(1,1))
(1,1,1,1)
集合:
快成八股文了,不写了,等我用上集合这东西了再补充,没见用过。
没隔多久就用上了,我回来补充了。。。
s=set()
s={} #空字典
s={10,20,30}
s.add(80) #{10, 20, 30, 80}
s.update({100,200})#{10, 20, 30, 80, 100, 200}
s.update([100,200])#{10, 20, 30, 80, 100, 200} 没变化
#所以upgrate 更新的元素是唯一的,无所谓集合还是数组,都无法重复
s.remove(100)
s.discard(100000) #不会报错,其他和remove一样
s.pop() #随机移除一个数,无参
_dict__ 中存储了类和对象的所有成员。
这里,self.__dict__ 操作的就是对象的成员,如果我们不将我们新加入的属性值加入到我们的对象成员记录 __dict__ 中去,我们也就不能使用 . 运算符获取到该属性值;
此时等式右边的 self 中存储了该对象接受到的所有属性值信息
因此这个等式相当于将当前所有构造传入的属性值信息存储到了 __dict__ 中去,方便我们使用 Bunch 类创建的对象使用 . 运算符访问其属性值信息。
class Bunch(dict):
def __init__(self,*args,**kwargs)
super(Bunch,self).__init__(*args,kwargs)
self.__dict__=self
person =Bunch(name='wangying',age='23')
print(person)
print(person.name)
print(person['age'])
T = Bunch
t = T(left=T(left="1",right="2"),right=T(left="3",right="4"))
t.left
#{'left': '1', 'right': '2'}
t.left.right
#'2'
t['left']['right']
#'2'
'left' in t.right
#True
'3' in t.right
#False
这样就构建好了树型结构,因为Bunch模式非常灵活,其属性可以动态设置,所以可以用该模型实现很多类型。