什么是dict

我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字:

['Adam', 'Lisa', 'Bart']

或者考试的成绩列表:

[95, 85, 59]

但是,要根据名字找到对应的成绩,用两个 list 表示就不方便。

如果把名字和分数关联起来,组成类似的查找表:

'Adam' ==> 95
'Lisa' ==> 85
'Bart' ==> 59

给定一个名字,就可以直接查到分数。

Python的 dict 就是专门干这件事的。用 dict 表示“名字”-“成绩”的查找表如下:

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

我们把名字称为key,对应的成绩称为value,dict就是通过 key 来查找 value

花括号 {} 表示这是一个dict,然后按照 key: value, 写出来即可。最后一个 key: value 的逗号可以省略。

由于dict也是集合,len() 函数可以计算任意集合的大小:

>>> len(d)
3

注意: 一个 key-value 算一个,因此,dict大小为3。

任务

新来的Paul同学成绩是 75 分,请编写一个dict,把Paul同学的成绩也加进去。

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM8AAADECAIAAAC3JOLLAAALUElEQVR4nO2dP2sbyxqH8yFucwod4yIQglFxIWCOjgMRAh1ZuHEhuMaSfRNjfCqbKxtMRNBJY8OBNLGEZReujKsI4cK5heULKST0KfYDpHJiXPsW+29mNSNppZ13Z3d/L08hrf450pN3dmZevXr2j19+BYCGZ6H/BSA5wDZAB2wDdMA2QAdsA3Q8u7+/v7+///Hjx8+fP79//56amwdB8QzhCdgG2+gCtsE2uoBtsI0uYBtsowvYBtvoArbBNrqAbbCNLh4eHmAbbCOKx8fHh4cH2AbbKOLr16+Pj4+wDbZRBHIbbKMLzBJgG13ANthGF7ANttEFbINtdAHbYBtdwDbYRhewDbbRBWyDbXRBaVu1+/TUPZj0nk9PT8b5ctjGwLYgQ0/bUnPzqWoXtsUt9B1JYVv8gtK25TPDym3mSNk9sEbMJ6NVtO9WPDee7HBss+/HPdw4X7bG3G6VfyHz9tsz66luq+bxlZbhPK112b4JtpEEmW3LZ4ari331t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tgA7YBuiAbYAO2AbogG2ADtgG6IBtgA7YBuiAbYAO2AbogG2ADtgG6IBtgA7YBuiAbYAO2Abo+D/rkUrPbXdeuwAAAABJRU5ErkJggg==" alt="" />

访问dict

我们已经能创建一个dict,用于表示名字和成绩的对应关系:

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

那么,如何根据名字来查找对应的成绩呢?

可以简单地使用 d[key] 的形式来查找对应的 value,这和 list 很像,不同之处是,list 必须使用索引返回对应的元素,而dict使用key:

>>> print d['Adam']
95
>>> print d['Paul']
Traceback (most recent call last):
File "index.py", line 11, in <module>
print d['Paul']
KeyError: 'Paul'

注意: 通过 key 访问 dict 的value,只要 key 存在,dict就返回对应的value。如果key不存在,会直接报错:KeyError。

要避免 KeyError 发生,有两个办法:

一是先判断一下 key 是否存在,用 in 操作符:

if 'Paul' in d:
print d['Paul']

如果 'Paul' 不存在,if语句判断为False,自然不会执行 print d['Paul'] ,从而避免了错误。

二是使用dict本身提供的一个 get 方法,在Key不存在的时候,返回None:

>>> print d.get('Bart')
59
>>> print d.get('Paul')
None

任务

根据如下dict:

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

请打印出:

Adam: 95
Lisa: 85
Bart: 59

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjUAAADPCAIAAACC4beZAAAYXElEQVR4nO2dQWsb2ZqG9SNmMwuPyaKhaYIXA4Hm6jpNjEHtmN4E2jAmUnJvTPBs2mHshhDT6HZDx3DBm0QeO6HxKnjRRBgHzF3YHuiFhFZ9e+5ltv4Bvev+A55FSVWnpKpSlerU+eqTnpdnIZWkU6WyeF+fU6fOV/mXf/03AACAslERPwIAAIBRyCcAACgj5BMAAJSRym+//fb777//+uuvc/O3wBYVhBCK0Q1KJ/KJfEIIOZW07asR+UQ+IYScStr21Yh8Ip8QQk4lbftqRD6RTwghp5K2fTUin8gnhJBTSdu+GpFP5BNCyKmkbV+NyCfyCSHkVNK2r0bkE/mEEHIqadtXI/KJfE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mb8PbX1Cd2czwSYvweOkLZ9NSrh+hHU18gF9QknP28M7oETpG1fjcinKYL6hLm+NetHgCOkbV+NyCcYhvqEAIUibftqVMJ8AgCYZqRtX43IJwAAp0jbvhqRTwAATpG2fTUinwAAnCJt+2pEPgEAOEXa9tWIfAIAcIq07asR+QQA4BRp21cj8gkAwCnStq9G5BMAgFOkbV+NyCcAAKdI274akU8AAE6Rtn01Ip8AAJwibftqRD4BADhF2vbViHwCAHCKtO2rEfkEAOAUadtXI/IJAMAp0ravRuQTAIBTpG1fjcgnAACnSNu+Gv0/bG1Fi2yal0kAAAAASUVORK5CYII=" alt="" />

dict的特点

dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样。而list的查找速度随着元素增加而逐渐下降。

不过dict的查找速度快不是没有代价的,dict的缺点是占用内存大,还会浪费很多内容,list正好相反,占用内存小,但是查找速度慢。

由于dict是按 key 查找,所以,在一个dict中,key不能重复。

dict的第二个特点就是存储的key-value序对是没有顺序的!这和list不一样:

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

当我们试图打印这个dict时:

>>> print d
{'Lisa': 85, 'Adam': 95, 'Bart': 59}

打印的顺序不一定是我们创建时的顺序,而且,不同的机器打印的顺序都可能不同,这说明dict内部是无序的,不能用dict存储有序的集合。

dict的第三个特点是作为 key 的元素必须不可变,Python的基本类型如字符串、整数、浮点数都是不可变的,都可以作为 key。但是list是可变的,就不能作为 key。

可以试试用list作为key时会报什么样的错误。

不可变这个限制仅作用于key,value是否可变无所谓:

{
'123': [1, 2, 3], # key 是 str,value是list
123: '123', # key 是 int,value 是 str
('a', 'b'): True # key 是 tuple,并且tuple的每个元素都是不可变对象,value是 boolean
}

最常用的key还是字符串,因为用起来最方便。

任务

请设计一个dict,可以根据分数来查找名字,已知成绩如下:

Adam: 95,

Lisa: 85,

Bart: 59.

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASUAAAC4CAIAAAAqim0JAAANEElEQVR4nO2dPWsbWx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LgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByIFvAHLgG4Ac+AYgB74ByPF/gd5gTZOScAMAAAAASUVORK5CYII=" alt="" />

更新dict

dict是可变的,也就是说,我们可以随时往dict中添加新的 key-value。比如已有dict:

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

要把新同学'Paul'的成绩 72 加进去,用赋值语句:

>>> d['Paul'] = 72

再看看dict的内容:

>>> print d
{'Lisa': 85, 'Paul': 72, 'Adam': 95, 'Bart': 59}

如果 key 已经存在,则赋值会用新的 value 替换掉原来的 value:

>>> d['Bart'] = 60
>>> print d
{'Lisa': 85, 'Paul': 72, 'Adam': 95, 'Bart': 60}

任务

请根据Paul的成绩 72 更新下面的dict:

d = {

95: 'Adam',

85: 'Lisa',

59: 'Bart'

}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzgAAAC+CAIAAAB27z2NAAAYnElEQVR4nO2dz28bZ3rHeS/QHlossptqVXVrNJtqlW5SbYRlZFeKWlqS3S2UhEkUUfKuDUOLtithpRSChVTxApGA7aooLAqWjUAnQScLggw4OUjawAcS/Cv4B+TUJPBZPZDzi5wZDjnzzvOQ+nzwOZBDct7hiJrni/edmTfzF9//ISIiIiIm5fd/+KN/zE3ffO/Dm+99+Nb42y+9/PKf/NmfXnntx9fff/8//mvt3z+5/8ulf1v8z7X/frT76f/+z/UP3vnbn742NjVZe/+1f5566Yc/sleVEf8yiIiIiD3m937Q/9rPfj41834tfr09dWPk6ts/eeMffvKz4f5XfvTnL33v714feiP71tjUjX/6l3+tvefGO+///ZvZ773c714PQQ0RERHRiD/o/5s3r47feKce14K88c77b159++W/utK8BoIaIiIiokFf+su//vFrb4xce3vs+s3rv3j3xrsf3Hj3g+u/eHfs+o03r77949feeKlvIOizBDVEREREpRLUEBEREZVKUENERERUKkENERERUak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XQIAQDtmglpu46ij7jTfoOZ7SWZAUPO+3/lIWFDzfKrlVZ9j957Y13sy9ImI8ZQuAQCgHeUzEzSco4aI2FNKlwAA0A5BDRFRTOkSAADaIaghIoopXQIAQDvKgxoiYi8rXQIAQDsENUREMaVLAABoh6CGiCimdAkAAO38P57M4bLu2sQXAAAAAElFTkSuQmCC" alt="" />

遍历dict

由于dict也是一个集合,所以,遍历dict和遍历list类似,都可以通过 for 循环实现。

1、直接使用for循环可以遍历 dict 的 key:

>>> d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
>>> for key in d:
... print key
...
Lisa
Adam
Bart

由于通过 key 可以获取对应的 value,因此,在循环体内,可以获取到value的值。

2、如果要迭代value,可以用for value in d.itervalues()

>>> for value in d.itervalues():
... print value
...
85
95
59
>>>

3、如果要同时迭代key和value,可以用for k, v in d.iteritems()

>>> for k,v in d.iteritems():
... print k,v
...
Lisa 85
Adam 95
Bart 59
>>>

任务

请用 for 循环遍历如下的dict,打印出 name: score 来。

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmsAAADhCAIAAAAYmmUkAAAX0ElEQVR4nO2dzW8bZ36A9Uf00oNi6GDAKBwfChgwzMiBBRWMLPgSwAqqSLS3NgwtUMRGJSOKiUJJurHRRdyDTVUfSHwSdAikEDLg+CB5s0aWDIt0s/nYZJtgQ7hNukGc9cYfUZGbehh+zJAzP86QL+f3DvU8eBBIFDUcGZx58s4M5+37q79+AhEREaPap74GiIiISZSCIiIitiMFRUREbEcKioiI2I4UFBERsR0pKCIiYjtSUERExHakoIiIiO1IQREREduRgiIiIrYjBUVERGxHCoqIiNiOFBQREbEdKSgiImI7UlBERMR2pKCIiIjt2Hf/4aOffvrp66+//vbbb+/fv/+DiwcPHjj/rfHw4cNHjx7du3evf88AmrIPACCAHbAYCqqv9hYKAPai3QiQoKD6am+hAGAv2o0ACQqqr/YWCgD2ot0IkKCg+mpvoQBgL9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/+atbDf58vOv/noef+8Xae0tBM4EHFdTsDOcUFJOldiNAQrGgZ+dLxfkzkX8x3revt3yuadQUC2pwxu+4x6Degjbcgl866GpuhnMKislSuxEgoVbQ9gagugU9cb5+pY/BgnoPCLfQ7IzfXT4Pmrnqvc/+4aee+5f8naCZwOXTlqZmOKegmCy1GwESSgV95uX1tgagKgX1vVA2oKDtzZjt+q0I1+IamPG721cSTcx7DsAefmrw8HO/CJoJvOWFP0ZmOKegmCy1GwES6p9mSUBBXedBE6rajN9HsuveMah0Vz/fghr/eykoJkvtRoAEBZXthYLqzPhdudjHcyg1noLKfy8FxWSp3QiQoKCyyS6obTN+ty5o020WzP293BcXE6l2I0CCgmJ8MjcLYlS1GwESbRZ07779Wqq/oRERY1O7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBRKegHH3ywtra25GV5edn5b4233nrrww8/pKCIiPGo3QiQqBR0bW3tm2+++b8q29vb29vb9+/f//77+/fuff/dd/f+9Kdv7979748++mR9fZ2CIiLGo3YjQKJS0IWFhVo7Hz16/ODhwx9+ePD11/979+7/fFW+++WXX338ye/f+837n3762fy/z1NQRMR41G4ESNQL6ow7Hz9+/ODBw7/85Yc///n+V1/d/eKLP37+hy8+/fTz0n/85507hY8++oSCIiLGpnYjQMJT0B9//PHhw0dOPr/77vsvvvzjZ5/918cf//63v/34/fc/+PWvf/O7331MQRERY1O7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESBBQRER7VW7ESDx/7keEwHtS48hAAAAAElFTkSuQmCC" alt="" />

什么是set

dict的作用是建立一组 key 和一组 value 的映射关系,dict的key是不能重复的。

有的时候,我们只想要 dict 的 key,不关心 key 对应的 value,目的就是保证这个集合的元素不会重复,这时,set就派上用场了。

set 持有一系列元素,这一点和 list 很像,但是set的元素没有重复,而且是无序的,这点和 dict 的 key很像。

创建 set 的方式是调用 set() 并传入一个 list,list的元素将作为set的元素:

>>> s = set(['A', 'B', 'C'])

可以查看 set 的内容:

>>> print s
set(['A', 'C', 'B'])

请注意,上述打印的形式类似 list, 但它不是 list,仔细看还可以发现,打印的顺序和原始 list 的顺序有可能是不同的,因为set内部存储的元素是无序的。

因为set不能包含重复的元素,所以,当我们传入包含重复元素的 list 会怎么样呢?

>>> s = set(['A', 'B', 'C', 'C'])
>>> print s
set(['A', 'C', 'B'])
>>> len(s)
3

结果显示,set会自动去掉重复的元素,原来的list有4个元素,但set只有3个元素。

任务

请用set表示班里的4位同学:

Adam, Lisa, Bart, Paul

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAygAAACACAIAAABvMdq4AAAPkklEQVR4nO2dz2vcZh6H576we1mW/tip8e4W0tK6ZLvphk5tb5yDU7tmF2eTUrcxpW1K9rBNyTgl2HT7A9LAggkEh9gm5FCKc0kwMaR7mEl7GuO/Yv6AnvoDn2cPGo2kkV6NpHmlj+w8D89hLGmkr+TxV5+8kiaV3z75DCIiIiJa8cln/vi36dm5M2/NnXnrtamTTzz99K9+8+tnX3ru1Jtv/vs/Vyry+hAREREPk797auSlV16dmX/TiV8nZ944PnHyxZf/8uIrxwheiIiIiPZ9auRPf52YeuN0N345ErwQERER8/KJ3//huZdePj558sSpuVN//yfBCxEREbEgCV6IiIiIBUnwQkRERCxIghciIiJiQRK8EBEREQuS4IWIiIhYkAQvRERExIIkeCEiIiIWJMELERERsSAJXoiIiIgFSfBCRERELEiCFyIiImJBErwQERERC5LghYiIiFiQBC9ERET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BCxGyq+z8ACLAevLwv8XK99d4QwSvykUND8Aou770lLngF3jXgqcbAI42t7eWJkdEqwQsRs6ru/wAgoMzfXN93j1dJJXghYjbV/R8ABBC8hpXghYjZVPd/ABBA8BpWghciZlPd/wFAQJmD18GQ4IWI2VT3fwAQEBG8nj3ygkp5H0RELEx1/wcAAQQvRESN6v4PAAIIXoiIGtX9HwAEELwQETWq+z8ACCB4ISJqVPd/ABBA8EJE1Kju/wAggOCFiKhR3f8BQADBCxFRo7r/A4AAghciokZ1/wcAAQQvRESN6v4PAAIIXoiIGtX9HwAEELwQETWq+z8ACCB4ISJqVPd/ABBA8EJE1Kju/wAggOCFiKhR3f8BQADBCxFRo7r/A4AAghciokZ1/wcAAQQvRESN6v4PAAIIXoiIGtX9HwAEELwQETWq+z8ACCB4ISJqVPd/ABBA8EJE1Kju/wAggOCFiKhR3f8BQADBCxFRo7r/A4AAghciokZ1/wcAAf8HjEPHc+1NekUAAAAASUVORK5CYII=" alt="" />

访问set

由于set存储的是无序集合,所以我们没法通过索引来访问。

访问 set中的某个元素实际上就是判断一个元素是否在set中。

例如,存储了班里同学名字的set:

>>> s = set(['Adam', 'Lisa', 'Bart', 'Paul'])

我们可以用 in 操作符判断:

Bart是该班的同学吗?

>>> 'Bart' in s
True

Bill是该班的同学吗?

>>> 'Bill' in s
False

bart是该班的同学吗?

>>> 'bart' in s
False

看来大小写很重要,'Bart' 和 'bart'被认为是两个不同的元素。

任务

由于上述set不能识别小写的名字,请改进set,使得 'adam' 和 'bart'都能返回True。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkYAAACCCAIAAAAsWZCwAAANPUlEQVR4nO2d3WsT6R6A80ecm3MRSy4EOawFDwiy2SoWobQiLIrCqbaIH9Bzsy4mfmzLUlbQwsEiSMS2aC8WUVispYWuF4nuVUqv9k/IH+CNu0qucy4m89XMpJ30fec3+eV5eFjyMZnMSXfnOe/MJG/uH/88hIiIqMCc+BYgIiIakaQhIqISSRoiIiqRpCEi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dict很像,唯一区别是不存储value,因此,判断一个元素是否在set中速度很快。

set存储的元素和dict的key类似,必须是不变对象,因此,任何可变对象是不能放入set中的。

最后,set存储的元素也是没有顺序的。

set的这些特点,可以应用在哪些地方呢?

星期一到星期日可以用字符串'MON', 'TUE', ... 'SUN'表示。

假设我们让用户输入星期一至星期日的某天,如何判断用户的输入是否是一个有效的星期呢?

可以用 if 语句判断,但这样做非常繁琐:

x = '???' # 用户输入的字符串
if x!= 'MON' and x!= 'TUE' and x!= 'WED' ... and x!= 'SUN':
print 'input error'
else:
print 'input ok'

注意:if 语句中的...表示没有列出的其它星期名称,测试时,请输入完整。

如果事先创建好一个set,包含'MON' ~ 'SUN':

weekdays = set(['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'])

再判断输入是否有效,只需要判断该字符串是否在set中:

x = '???' # 用户输入的字符串
if x in weekdays:
print 'input ok'
else:
print 'input error'

这样一来,代码就简单多了。

任务

月份也可以用set表示,请设计一个set并判断用户输入的月份是否有效。

月份可以用字符串'Jan', 'Feb', ...表示。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlEAAAE4CAIAAAD0DTGGAAAgAElEQVR4nO2d32sj65nn/UfszV54jS8OHJaDILM0hCi2adMgug9nWBzay3hs4aQ74LlpN6d1grEIZho6hgERMPK2T2N0ERoFQgvhBmcu3E4m7EjRsjuZ/Njdi4FtZm9mmVxtEs5170X9euunVFJVPSU9ny+fC+tX6bWsfj79Vr1Vz9K/+bf/DgAAQANL4iMAAAAoBpwHAABawHkAAKAFnAcAAFrAeQAAoAWcBwAAWsB5AACgBZwHAABawHkAAKAFnAcAAFrAeQAAoIWl/+fkD3/4wx//+Mff//73yyurkBVLhBASkw+k8OA8nEcIkYl0/dcYnIfzCCEyka7/GoPzcB4hRCbS9V9jcB7OI4TIRLr+awzOw3mEEJlI13+NwXk4jxAiE+n6rzFLf/rTn3AeziOEFB/p+q8xS1999ZWlPZyH8wghRUa6/mvM0g9+8ANLezgP5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h3GecUF5wEAyODUYZxXXHAeAIAMTh3GecUF5wEAyODUYZxXXHAeAIAMTh3GecUF5wEAyODUYZxXXHAeAIAMTh3GecXl/wN7RrTrMdMhbwAAAABJRU5ErkJggg==" alt="" />

遍历set

由于 set 也是一个集合,所以,遍历 set 和遍历 list 类似,都可以通过 for 循环实现。

直接使用 for 循环可以遍历 set 的元素:

>>> s = set(['Adam', 'Lisa', 'Bart'])
>>> for name in s:
... print name
...
Lisa
Adam
Bart

注意: 观察 for 循环在遍历set时,元素的顺序和list的顺序很可能是不同的,而且不同的机器上运行的结果也可能不同。

任务

请用 for 循环遍历如下的set,打印出 name: score 来。

s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk4AAACbCAIAAAAbV4MXAAATX0lEQVR4nO2dz2scR5+H54/Yyx4mQodAWBxDFgTindc2Flq0svCyOFjLypbkjRPh9xKLSE4ciRe/ClhmX6INWDKWjeNDEMrFirDB8UFygg8S+ivmkkMgEDaxEeQ276Fn+tdU9XRPV/e3pub58GBGPT39o9xTz1R1dXfln/75LQAAAIepiG8BAABAoaA6AABwHFQHAACOg+oAAMBxUB0AADgOqgMAAMdBdQAA4DioDgAAHAfVAQCA46A6AABwHFQHAACOg+oAAMBxUB0AADgOqgMAAMdBdQAA4DiVP/7446effvr5559//fXX/w/lt99+8/718/vvv79+/fqXX36pDgyCKSqEEKJJgxgKqkN1hBBLIy0Id4LqUB0hxNJIC8KdoDpURwixNNKCcCeoDtURQiyNtCDcCapDdYQQSyMtCHeiVd2bN2+Oj4/fvHmD6lAdIUQk0oJwJ2rVvX79+vj4+N133w3bDtWhOkJImZEWhDtRqM7z3FtvvTU0NHTp0iXfdqgO1RFCyoy0INxJXHWe5wYHB8fHxz///PMvv/xydXXVsx2qQ3WEkDIjLQh3Elfd8fHxxYsXP/vss6+//vq777578eLFjz/++P333x8fH6M6VEcIKTPSgnAn6lZde2jVoTpCSMmRFoQ74WID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992jOtuovzwbARg6qL9oV2Qv+o2E4jMAfn5tOOwCz4XN3M3aPIgBrlfqE6gCqqM5fyVTfx16e9cl1d9FGrIXtpVNfdo1lDn0oegZn4qFjNdXXexWez9XWF56pCw1Iu3zs8CI+pUe3X46Uk1QUXCzaJCw/VgRtIC8KdSHdgZqTc4yx2rs5qdJeQJ5PpujqTmxpr1bWRqlWXbr9QHfQu0oJwJ6guAVRnmrNLT9p7L1WgOoAqqjMXVJdAr6mu2Htglgr3wASoojpzQXVgIzzZAKCK6syFp5CDjfAUcoAqqjMXVAcAYCnSgnAnqA4AwFKkBeFOUB0AgKVIC8KdoDoAAEuRFoQ7QXUAAJYiLQh3guoAACxFWhDuBNUBAFiKtCDcCaoDALAUaUG4E1QHAGAp0oJwJ6gOAMBSpAXhTlAdAIClSAvCnaA6AABLkRaEO0F1AACWIi0Id4LqAAAsRVoQ7gTVAQBYirQg3AmqAwCwFGlBuBNUBwBgKdKCcCeoDgDAUqQF4U5QHQCApUgLwp2gOgAAS5EWhDv5B8CP3inbMX44AAAAAElFTkSuQmCC" alt="" />


更新set

由于set存储的是一组不重复的无序元素,因此,更新set主要做两件事:

一是把新的元素添加到set中,二是把已有元素从set中删除。

添加元素时,用set的add()方法:

>>> s = set([1, 2, 3])
>>> s.add(4)
>>> print s
set([1, 2, 3, 4])

如果添加的元素已经存在于set中,add()不会报错,但是不会加进去了:

>>> s = set([1, 2, 3])
>>> s.add(3)
>>> print s
set([1, 2, 3])

删除set中的元素时,用set的remove()方法:

>>> s = set([1, 2, 3, 4])
>>> s.remove(4)
>>> print s
set([1, 2, 3])

如果删除的元素不存在set中,remove()会报错:

>>> s = set([1, 2, 3])
>>> s.remove(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 4

所以用add()可以直接添加,而remove()前需要判断。

任务

针对下面的set,给定一个list,对list中的每一个元素,如果在set中,就将其删除,如果不在set中,就添加进去。

s = set(['Adam', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', '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pw4hllrtmg9iLFF8LkPvs7bCM28RS612zQcxlig+ERHV1a75IAbxiYjoTu2aD2IQn4iI7tSu+SAG8YmI6E7tmg9iVCM+15887509361r/8NARMxWu+aDGFLx+agdPQ3V/YRl+eLz3rPeW29d/V8RIi6h2jUfxBCJzzvN00Gndcd/mffpfe5/vsQnImqpXfNBDJH4jD8svtHqDI8ezBif60+e915sbr1o987avbP2+yc3Rm/VvfdvvfW69/6s3TtrR/kXLjl7tjVxO9HKobFPpZp7+mtERJvaNR/EkBm8vd3qBmO2j9rDqCc6W3yetXsvNldrftoFQ7Kj5PNf3th9ayTr6N2x+IylpvFurt4nj6NDREG1az6IIXfp0MMjv3+W98Hx8d+W32sMX269MCMwOrsZLfdNjc9oO5uvzDOjueOTJ9IhopDaNR/EkInPB8eD/mjA9lF7OOgfPyokPu0DrcXFZy3v9NeIiHa1az6IIRGfjVbHvNo28XKe+Lyx+7b96t7Kak01PgOnmf4aETFL7ZoPYohdOhSd73x41J/30qHRy3vPokt7BOMzPg6cQ86DIuKcatd8EENyumyJ+z5Hlw6NXxZric/4+tFHsuIz9qkJV95mT3+NiJhP7ZoPYpTtqUOJc5+IiJVSu+aDGMQnIqI7tWs+iEF8IiK6U7vmgxhli09ExCqrXfNBDOITEdGd2jUfxCA+ERHdqV3zQQziExHRndo1H8QgPhER3ald80EM4hMR0Z3aNR/EID4REd2pXfNBDOITEdGd2jUfxCA+ERHdqV3zQQziExHRndo1H8QgPhER3ald80EM4hMR0Z3aNR/EID4REd2pXfNBDOITEdGd2jUfxCA+ERHdqV3zQQziExHRndo1H8T4f8VTW/vVU9LOAAAAAElFTkSuQmCC" alt="" />



 

Python 入门(六)Dict和Set类型的更多相关文章

  1. PYTHON 学习笔记1 PYTHON 入门 搭建环境与基本类型

    简介 Python,当然大家听到这个名词不再是有关于像JAVA 一样的关于后台,我们学习Python 的目的在于对于以后数据分析和机器学习AI 奠定基础,Python 在数据分析这一块,可谓是有较好的 ...

  2. python入门及数字、字符串类型

    目录 python开发框架 开发 1. 开发语言 2. 语言比对 3. python安装 4. Python开发IDE:pycharm ,eclipse python入门 1. 第一句Python 2 ...

  3. python 学习之dict和set类型

    什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的成绩列表: [95, 85, 59] ...

  4. Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)

    Python的变量和数据类型 1 .python的变量是不须要事先定义数据类型的.能够动态的改变 2. Python其中一切皆对象,变量也是一个对象,有自己的属性和方法 我们能够通过 来查看变量的类型 ...

  5. python基础(六)dict字典和文件操作open

    字典dict 使用key来标注value的数据类型,key和value是一一对应的.在字典中key是唯一的,所以字典也是无序的. #定义一个字典 dict = { 'name' : 'sylar', ...

  6. python第六篇文件处理类型

    阅读目录 一 文件操作 二 打开文件的模式 三 操作文件的方法 四 文件内光标移动 五 文件的修改   文件处理                                             ...

  7. Python入门 六、像个 Pythonista

    pickle import pickle test_data = ['Save me!',123.456,True] f = file('test.data','w') pickle.dump(tes ...

  8. Python入门版

    一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...

  9. python 入门快速学习整理

    Python 入门学习 1  : 对象类型 1 1.1 列表 1 1.2 字典 2 1.3 元组 2 1.4 元组 2 1.4 文件 3 2  : 条件和循环语句 3 2.1  if else语句 3 ...

  10. Python之Dict和Set类型(入门5)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407905.html 本文出自:[Edwin博客园] Python之Dict和Set类型 1. Python ...

随机推荐

  1. 检测SqlServer数据库是否能连接的小技巧

    有时候可能需要检测下某台机器的服务是不是起来了,或者某台机器的某个库是不是能被连接又不能打开ssms也不想登陆服务器的话就可以用这个方法. 1.在桌面上右键创建个文本,然后改后缀名为udl以后保存(1 ...

  2. [已修正]安装struts找不到tld文件

    今天安装的struts1.3,但是缺少tld文件,所以无法使用taglib,找了半天 假设你的struts版本为1.3.10 解压后的目录为F:\struts-1.3.10-all\struts-1. ...

  3. registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    问题是tomcat的版本问题,tomcat新检测机制导致的这个问题,换版本可以解决问题,但不建议这么做,租用服务器不是你说换就换的.其实问题根源是BasicDataSource,BasicDataSo ...

  4. Android开发日记(三)

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  5. mysql海量数据处理步骤

    本文转自https://segmentfault.com/a/1190000006158186 当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据 ...

  6. DRAM 各项基本参数记录

    记录一下DRAM的各项基本参数 tCL CAS Latency CAS 潜伏期, 列地址寻找/读写命令执行完毕,准备要读出来,需要一个延时缓一缓 tRAS: RAS Active Timeing 行有 ...

  7. Html5之web workers多线程

    Web Workers 是 HTML5 提供的一个javascript多线程解决方式,我们能够将一些大计算量的代码交由web Worker执行而不冻结用户界面. 1.首先看一个实例: 1)js文件(t ...

  8. Just Cause系列游戏品鉴

    没错, 这又是一个游戏点评, 因为实在没地写了, 想起来我还欠JC系列许多售后评价, 就专门写了这篇blog来总结下JC系列的特色, 以及它最新的游戏引擎apex, JC4月初的时候发布的, 虽然和3 ...

  9. DIV内滚动条滚动到指定位置

    相对浏览器,将指定div滚到到指定位置,其用法如下: $("html,body").animate({scrollTop: $(obj).offset().top},speed); ...

  10. 百万级PHP网站架构工具箱

    在了解过世界最大的PHP站点,Facebook的后台技术后,今天我们来了解一个百万级PHP站点的网站架构:Poppen.de.Poppen.de是德国的一个社交网站,相对Facebook.Flickr ...