标签:lse 字符串 lin ima read 序号 pytho strong 建立
序列是一个基类类型
hobby_list = [hobby, ‘run‘, ‘girl‘]
print(id(hobby_list))#4558605960
print(type(hobby_list))#<class ‘list‘>
print(hobby_list)#[‘read‘, ‘run‘, ‘girl‘]
列表索引取值的方法,牢记索引序号从0开始。
hobby_list = [‘read‘, ‘run‘, ‘girl‘]
# 索引序号 0 1 2
# 取出第二个爱好
print(hobby_list[1])#run
hobby_list[0] = ‘write‘
复合列表
hobby_list = [‘read‘, ‘run‘, [‘girl_name‘, 18, ‘shanghai‘]] # 取出girl的年龄 print(hobby_list[2][1])# 18
s + t: 连接两个列表和t,s*n:将列表s复制n次。
ls1 = [‘python‘, 123] ls2 = [‘java‘, 456] print(ls1 * 2); # [‘python‘, 123, ‘python‘, 123] print(ls1 + ls2); # [‘python‘, 123, ‘java‘, 456]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list[0:3:2]) # [‘nick‘, ‘tank‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(len(name_list)) # 4
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(‘tank sb‘ in name_list) # False print(‘nick handsome‘ not in name_list) # True
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list.append(‘tank sb‘) print(name_list) # [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘, ‘tank sb‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] del name_list[2] print(name_list) # [‘nick‘, ‘jason‘, ‘sean‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] for name in name_list: print(name)
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list.insert(1, ‘handsome‘) print(name_list) # [‘nick‘, ‘handsome‘, ‘jason‘, ‘tank‘, ‘sean‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list.pop(1)) # jason print(name_list) # [‘nick‘, ‘tank‘, ‘sean‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list.remove(‘nick‘)) # None print(name_list) # [‘jason‘, ‘tank‘, ‘sean‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list.count(‘nick‘)) # 1
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list.index(‘nick‘)) # 0
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list.clear() print(name_list) # []
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] print(name_list.copy()) # [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list2 = [‘nick handsome‘] name_list.extend(name_list2) print(name_list) # [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘, ‘nick handsome‘]
name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list.reverse() print(name_list) # [‘sean‘, ‘tank‘, ‘jason‘, ‘nick‘]
# list之sort(),使用sort列表的元素必须是同类型的 name_list = [‘nick‘, ‘jason‘, ‘tank‘, ‘sean‘] name_list.sort() print(name_list) # [‘jason‘, ‘nick‘, ‘sean‘, ‘tank‘] name_list.sort(reverse=True) print(name_list) # [‘tank‘, ‘sean‘, ‘nick‘, ‘jason‘]
有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
stu_info_list = [ {‘name‘: ‘nick‘, ‘age‘: 19, ‘sex‘: ‘male‘}, {‘name‘: ‘egon‘, ‘age‘: 18, ‘sex‘: ‘male‘}, {‘name‘: ‘tank‘, ‘age‘: 20, ‘sex‘: ‘female‘}, {‘name‘: ‘tank‘, ‘age‘: 20, ‘sex‘: ‘female‘}, {‘name‘: ‘egon‘, ‘age‘: 18, ‘sex‘: ‘male‘}, ] new_stu_info_list = [] for stu_info in stu_info_list: if stu_info not in new_stu_info_list: new_stu_info_list.append(stu_info) for new_stu_info in new_stu_info_list: print(new_stu_info)
元组是一种列表类型,一旦创建就不能被修改。
creature = "cat", "dog", "tiger", "human" color = (0x001100, "blue", creature) print(type(creature)) # <class ‘tuple‘> print(type(color)) # <class ‘tuple‘>
注意与字符串区别:
name_str = (‘egon‘) # ()只是普通包含的意思 name_tuple = (‘egon‘,) print(type(name_str)) # <class ‘str‘> print(type(name_tuple)) # <class ‘tuple‘>
索引取值
切片(顾头不顾尾,步长)
长度len
成员运算in和not in
循环
count
index
集合是多个元素的无序组合
集合元素不可更改,不能是可变数据类型
建立空集合类型,必须使用set()
s = {1, 2, 1, ‘a‘} # s = set({1,2,‘a‘}) print(s) # {‘a‘, 1, 2} s = set(‘hello‘) # 使用set()建立集合 print(s) # {‘h‘, ‘l‘, ‘o‘, ‘e‘}
集合类型主要应用于:包含关系比较、数据去重
ls = ["p", "p", "y", "y", 123] s = set(ls) # 利用了集合无重复元素的特
1.长度len
s = {1, 2, ‘a‘} print(len(s)) #len(s): 3
2.成员运算in和not in
s = {1, 2, ‘a‘} print(1 in s) # True
3.|:并集
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} print(pythoners|linuxers) # {‘egon‘, ‘tank‘, ‘kevin‘, ‘jason‘, ‘nick‘, ‘sean‘} print(pythoners.union(linuxers)) #{‘egon‘, ‘tank‘, ‘kevin‘, ‘jason‘, ‘nick‘, ‘sean‘}
4.&:交集
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} print(pythoners&linuxers) #{‘nick‘} print(pythoners.intersection(linuxers)}) {‘nick‘}
5.-:差集
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} print(pythoners-linuxers) #{‘tank‘, ‘jason‘, ‘sean‘} print(pythoners.difference(linuxers)) #{‘tank‘, ‘jason‘, ‘sean‘}
6.^:补集
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} print(pythoners^linuxers) # {‘egon‘, ‘tank‘, ‘kevin‘, ‘jason‘, ‘sean‘} print(pythoners.symmetric_difference(linuxers))# {‘egon‘, ‘tank‘, ‘kevin‘, ‘jason‘, ‘sean‘}
7.==
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} javers = {‘nick‘, ‘egon‘, ‘kevin‘} print(pythoners==linuxers) #False print(javers==linuxers) # True
8.父集:>、>=
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} javaers = {‘jason‘, ‘nick‘} print(pythoners>linuxers) # False print(pythoners>=linuxers) # False print(pythoners>=javaers) # True print(pythoners.issuperset(javaers)) #True
9.子集:<、<=
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} javaers = {‘jason‘, ‘nick‘} print(pythoners<linuxers) # False print(pythoners<=linuxers) #False print(javaers.issubset(javaers)) #True
10.add(x):如果x不在集合S中,将x增加到S
s = {1, 2, ‘a‘} s.add(3) print(s) #{1, 2, 3, ‘a‘}
11.remove(x):移除S中元素x,如果x不在集合S中,产生KeyError异常
s = {1, 2, ‘a‘} s.remove(1) print(s) #{2, ‘a‘}
12.difference_update()
pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} pythoners.difference_update(linuxers) print(pythoners) #{‘tank‘, ‘jason‘, ‘sean‘}
13.discard(x):移除S中元素x,如果x不在集合S中,不报错
s = {1, 2, ‘a‘} # s.remove(3) # 报错 s.discard(3) print(s) #{1, 2, ‘a‘}
14.isdisjoint()
# set之isdisjoint(),集合没有共同的部分返回True,否则返回False pythoners = {‘jason‘, ‘nick‘, ‘tank‘, ‘sean‘} linuxers = {‘nick‘, ‘egon‘, ‘kevin‘} pythoners.isdisjoint(linuxers) print(pythoners.isdisjoint(linuxers)) # False
15、set(x):将其他类型变量x转变为集合类型
用来存取多个值,按照key:value的方式存值,取的时候可以通过key而非索引去取值,key对value具有描述性的作用。存放数据的种类各种各样并且数据较多的时候可以使用字典。
在{}内用逗号分隔开多个元素,每一个元素都是key: value的格式,其中value是任意格式的数据类型,key由于具有描述性的作用,所以key通常是字符串类型。
创建空字典必须使用 { }。
user_info = {‘name‘: ‘nick‘, ‘gender‘: ‘male‘, ‘age‘: 19, ‘company_info‘: [‘oldboy‘, ‘shanghai‘, 50]} print(id(user_info))#4396183344 print(type(user_info))#<class ‘dict‘> print(user_info)#{‘name‘: ‘nick‘, ‘gender‘: ‘male‘, ‘age‘: 19, ‘company_info‘: [‘oldboy‘, ‘shanghai‘, 50]}
字典取值方式不再依赖于索引,而是依赖于key,通过[key]即可获取key对应的value值。可存可取
字典套列表
user_info = {‘name‘: ‘nick‘, ‘gender‘: ‘male‘, ‘age‘: 19,
‘company_info‘: [‘oldboy‘, ‘shanghai‘, 50]}
print(user_info[‘name‘])#nick
user_info[‘name‘])=’a’
print(user_info[‘company_info‘][0])#oldboy
字典套字典
user_info = {‘name‘: ‘nick‘, ‘gender‘: ‘male‘, ‘age‘: 19, ‘company_info‘: {
‘c_name‘: ‘oldboy‘, ‘c_addr‘: ‘shanghai‘, ‘c_num_of_employee‘: 50}}
print(user_info[‘name‘])#nick
print(user_info[‘company_info‘][‘c_name‘])#oldboy
dic = dict(a=1, b=2, c=3) print(dic) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
长度len
dic = {‘a‘: 1, ‘b‘: 2} print(len(dic)) #2
成员运算in和not in
dic = {‘a‘: 1, ‘b‘: 2} print(‘a‘ in dic) # True print(1 in dic) # False
删除del,pop,popitem
dic = {‘a‘: 1, ‘b‘: 2} del dic[‘a‘] print(dic.get(‘a‘)) # None dic = {‘a‘: 1, ‘b‘: 2} dic.pop(‘a‘) # 指定元素删除 print(dic.pop(‘b‘)) # 2 print(dic.get(‘a‘)) # None dic = {‘a‘: 1, ‘b‘: 2} print(dic.popitem()) #(‘b‘, 2), popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。
键keys()、值values()、键值对items()
# python2中取出的是列表(鸡蛋);python3中取出的是元组(鸡) dic = {‘a‘: 1, ‘b‘: 2} print(dic.keys()) # dict_keys([‘a‘, ‘b‘]) print(dic.values()) # dict_values([1, 2]) print(dic.items()) # dict_items([(‘a‘, 1), (‘b‘, 2)])
for循环
# dic是无序的,但是python3采用了底层优化算法,所以看起来是有序的,但是python2中的字典是无序 dic = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4} for k, v in dic.items(): # items可以换成keys()、values() print(k, v)
get(k):键k存在,则返回相应值,不在则返回值
dic = {‘a‘: 1, ‘b‘: 2} print(dic.get(‘a‘)) # 1 print(dic.get(‘c‘)) # None
update
dic1 = {‘a‘: 1, ‘b‘: 2} dic2 = {‘c‘: 3} dic1.update(dic2) print(dic1) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
fromkeys
dic = dict.fromkeys([‘name‘, ‘age‘, ‘sex‘], None) print(dic) # {‘name‘: None, ‘age‘: None, ‘sex‘: None}
setdefault
# dic之setdefault(),有指定key不会改变值;无指定key则改变值 dic = {‘a‘: 1, ‘b‘: 2} print(dic.setdefault(‘a‘, 3)) # 1 print(dic) # {‘a‘: 1, ‘b‘: 2} print(dic.setdefault(‘c‘, 3)) # 3 print(dic) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
统计s=‘hello tank tank say hello sb sb‘
中每个单词的个数
结果如:{‘hello‘: 2, ‘tank‘: 2, ‘say‘: 1, ‘sb‘: 2}
s=‘hello tank tank say hello sb sb‘ l=s.split() dic={} for item in l: if item in dic: dic[item]+=1 else: dic[item]=1 print(dic)
不可变类型 没有提供修改对象自身的方法,对数据进行操作时,id会发生改变(重新分配地址)。
a = "hello" #定义一个字符串的变量 print(id(a)) #第一次的地址 print(a) #a = hello a = a.upper() # 单纯的a.upper() 执行过后,无法存储到a本身,必须得重新赋值给a 换句话说,a在被upper之后,重新指向了一个新的地址 print(id(a)) #第二次的地址 print(a)
第一段代码执行结果:
可变类型提供了修改自身这些方法,对数据进行操作 ,id不会发生改变(使用原来的地址)。
b = [11,22,33,44,55] #定义一个列表的变量 print(id(b)) #第一次的地址 print(b) #b = [11,22,33,44,55] b.append(99) #单纯的b.append()执行过后,不需要存储到b,因为b已经被更改 print(id(b)) #检查第一次的地址 print(b) #发现在第一次地址当中,b已经改变
第二段代码执行结果:
如果l2是l1的拷贝对象,则l1内部的任何数据类型的元素变化,则l2内部的元素也会跟着改变,因为可变类型值变id不变。
l1 = [‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]] l2 = l1 l1.append(‘g‘) print(l1) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘], ‘g‘] print(l2) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘], ‘g‘]
如果l2是l1的浅拷贝对象,则l1内的不可变元素发生了改变,l2不变;如果l1内的可变元素发生了改变,则l2会跟着改变。
import copy l1 = [‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]] l2 = copy.copy(l1) l1.append(‘g‘) print(l1) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘], ‘g‘] print(l2) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]]
l1[3].append(‘g‘) print(l1) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘, ‘g‘], ‘g‘] print(l2) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘, ‘g‘]]
如果l2是l1的深拷贝对象,则l1内的不可变元素发生了改变,l2不变;如果l1内的可变元素发生了改变,l2也不会变,即l2永远不会因为l1的变化而变化。
import copy l1 = [‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]] l2 = copy.deepcopy(l1) l1.append(‘g‘) print(l1) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘], ‘g‘] print(l2) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]] l1[3].append(‘g‘) print(l1) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘, ‘g‘], ‘g‘] print(l2) #[‘a‘, ‘b‘, ‘c‘, [‘d‘, ‘e‘, ‘f‘]]
标签:lse 字符串 lin ima read 序号 pytho strong 建立
原文地址:https://www.cnblogs.com/springsnow/p/11944380.html