【python基础】之元组 集合 字典

元组

元组:元组和列表类似。但是元组中的元素是固定的(不能给一个元组添加,删除和替换元素以及重新排序)

1.创建元组

t1 =  () #创建一个空元组
t2 = (1, 2, 3) 
t3 = tuple([2 * x for x in range(1, 5)]) #从列表中创建元组
t4 = tuple("abac") #从字符串中创建元组  t4 = ['a', 'b', 'a', 'c']

 

2.使用

可以使用len, min,max, sum函数操作元组;用for循环遍历元组的元素;in和not in判断元素是否在元组中;用比较运算符对元素进行比较。
>>> tuple1 = tuple([7,1,2,23,4,5])
>>> tuple1
(7, 1, 2, 23, 4, 5)
>>> len(tuple1)  
6
>>> min(tuple1)  #tuple1元组中有非数字时会报错
1
>>> max(tuple1)  #tuple1元组中有非数字时会报错
23
>>> sum(tuple1)  #tuple1元组中有非数字时会报错
42
>>> for x in tuple1: #用for循环遍历元组的元素
    print(x)

    
7
1
2
23
4
5
>>> 7 in tuple1 #in和not in判断元素是否在元组中
True
>>> 3 in tuple1
False
>>> 

 

 


集合

集合中的元素是不重复且不按任何顺序放置的。

数组元组和字符串元组是不可变的。

1.创建集合(set函数)

s1 = set() #创建空集合
s2 = {1, 3, 5} 
s3 = set((1, 3, 5)) #从元组中创建集合
s4 = set([x * 2 for x in range(1, 10)]) #从列表中创建集合
s5 = set("abac") #从字符串中创建集合 s5 = {'a', 'b', 'c'}

 

集合可以包含类型相同或不同的元素

s6 = {1, 3, "abc", 'a'} 

2.操作和访问集合

add()和remove()

使用add()和remove()方法添加和删除元素
>>>s1 = {1, 2, 4}
>>>s1.add(6)
{1, 2, 4, 6}
>>>s1.remove(6)
{1, 2, 4, 6}

 

下文引用s1集合,承接下文

 

len(),min(), max(),sum() (后三个只对集合元素全为数字有效)

>>>len(s1) #求集合的长度,即元素的个数
3
>>>max(s1) #求集合<span style="font-family: Arial, Helvetica, sans-serif;">元素中的</span>最大值
4
>>>min(s1) #求集合元素中的最小值
1
>>>sum(s1) #求集合元素的和
7

 

for循环遍历集合中的元素

>>>for x in s1:
    print(x)

    
1
2
4

 

 

in和not in判断元素是否在集合中

若in和not in的表达式为真则返回True,否则返回False
>>> 1 in s1
True
>>> 2 in s1
True
>>> 1 not in s1
False

 

 

3.子集和超集 (issubset()函数和issuperset()函数)

子集:如果集合s1中的每个元素都在集合s2中,则称s1是s2的子集。
超集:如果一个集合S1中的每一个元素都在集合S2中,且集合S2中可能包含S1中没有的元素,则集合S2就是S1的一个超集。

使用issubset()判断集合s1是否是s2的子集,issuperset()判断集合s1是否是s2的超集。

>>> s1 = {1, 2, 4}
>>> s2 = {1, 3 ,4 , 5, 2}
>>> s1.issubset(s2) #s1是s2的一个子集
True
>>> s2.issuperset(s1) #s2是s1的一个超集
True

 

若s1是s2的子集则s2一定是s1的超集

 

4.关系运算符(==, !=, >, >=, <,<=)

==               若s1和s2相等(包含相同的元素),返回true
!=    若s1和s2不相等(包含不相同的元素),返回true
> 如果s1是s2的真超集,则s1>s2返回true
>= 如果s1是s2的超集,则s1>=s2返回true
< 如果s1是s2的真子集,则s1<s2返回true
<= 如果s1是s2的子集,则s1<=s2返回true
注意:集合中的元素是没有顺序的。

 

5.集合运算(并集,交集,差集,对称差集合)

并集

定义:两个集合的的所有元素的集合
方法:union 或 运算符 "|"
>>> s1 = {1, 2, 4}
>>> s2 = {1, 3, 5}
>>> s1.union(s2)
{1, 2, 3, 4, 5}
>>> s1 | s2
{1, 2, 3, 4, 5}

 

下文引用此处集合s1和s2。

 

交集

定义:两个集合共同元素的集合
方法:intersction 或 运算符 "&"
>>> s1.intersection(s2)
{1}
>>> s1 & s2
{1}

 

 

差集

定义:出现在集合s1中但不出现在集合s2中元素的集合。
方法:difference 或 运算符"-"
>>> s1.difference(s2)
{2, 4}
>>> s1 - s2
{2, 4}

 

 

对称差集合

定义:除了两个集合中共同元素之外所有在两个集合中的元素之和。
定义:symmertric_difference 或运算符"^"
>>> s1.symmetric_difference(s2)
{2, 3, 4, 5}
>>> s1 ^ s2
{2, 3, 4, 5} 

 

字典

一个字典就是一个存储键值对集合的容器对象。

1.创建

字典中由数个条目组成:key:value

>>> students = {"001":"sky","002":"will"}  #创建有两个条目的字典
>>> students["001"]
'sky'
>>> student2 = {}  #创建一个空字典 

2.添加,修改和获取值

格式:dictionaryName[key] = value

>>> students["003"] = "good"
>>> students 
{'002': 'will', '001': 'sky', '003': 'good'}  #字典students中新增条目
>>> students["003"] 
'good'
>>> 

 

注意:若你要添加的关键字key值已存在,则替换该关键字对应的value值。

 

3.删除条目

格式:del dictionName[key]

>>> del students["003"]
>>> students          #删除第三个条目
{'001': 'sky','002': 'will'}
>>> 

 

若字典中不存在关键字key则会出现KeyError异常

 

4.常用的操作

使用for循环来遍历条目

>>> for viewKey in students:
    print(viewKey + ":" + str(students[viewKey]))

"001":"sky"
"002":"will"

len函数

>>> len(students)
2

 


in和not in判断关键字key是否在一个字典中

>>> "123" in students
False
>>> "001" in students
True
>>> "002" not in students
False

 

 

相等性检测( == , !=)

>>> students2 = {"1":45}
>>> students == students2
False
>>> students != students2
True

 

注意:比较时不管条目在字典中的顺序。不能使用比较运算符(>, >=, <, <=)对字典进行比较,字典中的条目顺序是没有顺序的。

 

 

5.常用的字典类dict

Python中的字典类是dict
keys(): tuple 返回一个关键字key序列
values(): tuple 返回一个值value序列
items(): tuple 返回一个元组序列,每个元组都是一个条目的(键, 值)
clear(): None 删除所有条目
get(key): value 返回这个关键字对应的值
pop(key): value 删除这个关键字对应的条目并返回它的值
popitem(): tuple 返回一个随机选择的键值对作为元组并删除这个被选择的条目
get(key)方法除了当关键字key不在字典中返回None而不是抛出一个异常。其他都有dictionName[key]类似。pop(key)与del dictionName[key]类似
 
例如:
>>> students.keys()
dict_keys(['001', '002'])
>>> students.values()
dict_values(['sky', 'will'])
>>> students.items()
dict_items([('001', 'sky'), ('002', 'will')])
>>> students.get("001")
'sky'
>>> students.pop("001")
'sky'
>>> students
{'002': 'will'}
>>> students.popitem()
('002', 'will')
>>> students.clear()
>>> students;
{}
>>>  


 
 
posted @ 2017-01-30 12:55  天秤libra  阅读(572)  评论(0编辑  收藏  举报