Python中除了有list外还有 set, dict字典
Python100天31:如何理解数组(list)、二级数组、series Python100天30:如何理解表格与列表数组list
list元素是可以重复的 ,如下 下面的例子中有两个重复的 1 这在list是允许 的
ls = ["1","2","3","4","1","1"]
print(ls)
# ['1', '2', '3', '4', '1', '1']
有两个重复的
set 元素不能重复
- set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
ls = ["1","2","3","4","1","1"]
print(ls)
print("--------set元素不能重复--------")
myset = set(["1","2","3","4","1","1"])
print(myset)
set 集合元素进行操作-交差并补
在高中我们学过两个集合间的 ,其实程序的世界也是如此
x = set('runoob')
y = set('google')
print("------")
print(x, y)
print("---交集---")
print(x & y)
print("---并集---")
print(x | y)
print("---差集---")
print(x - y )
------
{'o', 'n', 'u', 'b', 'r'} {'e', 'l', 'g', 'o'}
---交集---
{'o'}
---并集---
{'n', 'o', 'l', 'u', 'e', 'g', 'b', 'r'}
---差集---
{'u', 'b', 'r', 'n'}
字典dict:字典存储(键,值)对
类似于Java中的Map或Javascript中的对象。你可以像这样使用它
d = {
'cat': 'cute',
'dog': 'furry'
}
print(d['cat'])
print('cat' in d)
d['fish'] = 'wet'
print(d['fish'])
print(d.get('monkey', 'N/A'))
print(d.get('fish', 'N/A'))
del d['fish']
print(d.get('fish', 'N/A')) #
print(d.get('fish'))
cute
True
wet
N/A
wet
N/A
None
- cat': 'cute', 分别代表key 与value
- del 表示删除某个键值对
- d.get('fish', 'N/A') 表示获取fish这个key 的值,如果没有这个key则返回 N/A这个默认值
字典dictionary 的定义
d = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
MLB_team = {
'Colorado' : 'Rockies',
'Boston' : 'Red Sox',
'Minnesota': 'Twins',
'Milwaukee': 'Brewers',
'Seattle' : 'Mariners'
}
type(MLB_team) # dict
如果key不存在 ,则会出现异常
MLB_team['Toronto']
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
MLB_team['Toronto']
KeyError: 'Toronto'
del MLB_team['Seattle']
MLB_team
{'Colorado': 'Rockies', 'Boston': 'Red Sox', 'Minnesota': 'Twins',
'Milwaukee': 'Brewers', 'Kansas City': 'Royals'}
有时你也可以用dic 模拟list,但是本质 是不一样的
d = {3: 'd', 2: 'c', 1: 'b', 0: 'a'}
# {3: 'd', 2: 'c', 1: 'b', 0: 'a'}
print ( d[0] ) # 'a'
print( d[2] ) # 'c'
语法 看起来来list访问 元素是一样的,但是实际上你不能这样访问dict
d[-1]
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
d[-1]
KeyError: -1
如果你想学list一样通过 -1来访问dict是会报错的,因为dict中没有 -1 这个key
创建一个空的字典 {},, value可以放置任务Python数据
person = {}
type(person) # <class 'dict'>
person['fname'] = 'Joe'
person['lname'] = 'Fonebone'
person['age'] = 51
person['spouse'] = 'Edna'
person['children'] = ['Ralph', 'Betty', 'Joey']
person['pets'] = {'dog': 'Fido', 'cat': 'Sox'}
person
'''
{'fname': 'Joe', 'lname': 'Fonebone', 'age': 51, 'spouse': 'Edna',
'children': ['Ralph', 'Betty', 'Joey'], 'pets': {'dog': 'Fido', 'cat': 'Sox'}}
'''
print( person['pets']['cat'] )
例如key pets的值为一个字典,我们取到 pets的值后,可以取到 猫的名字
添加了元素后,如何清空 clear()函数
清空字典
d.clear()
只要取key
d.keys()
dict_keys(['cat', 'dog'])
只要取value
通过这些例子可以知道字典其实就很多对 key:value关系的集合
我们看一下其他语言是怎么实现的
Java语言
import java.util.HashMap;
import java.util.Map;
public class MapMain {
public static void main(String[] args) {
Map dict = new HashMap();
dict.put("1","one");
dict.put("2","two");
dict.put("3","three");
System.out.println(dict.keySet());
System.out.println(dict.values());
}
}
[1, 2, 3]
[one, two, three]
JavaScript语言的dict
var dict = {"1":"one" , "2":"two"}
undefined
dict
{1: 'one', 2: 'two'}
字典dict的本质 是什么?
是建立一种映射关系