-
浮点数
字符串
整数查看全部 -
d = { 'Alice': [45], 'Bob': [60], 'Candy': [75], }
d["Alice"]=[45,56,89]
d["Bob"]=[85,76,84]
d["Candy"]=[75,86,99]
查看全部 -
例:已知两个集合s1、s2,请判断两个集合是否有重合,如果有,请把重合的元素打印出来。s1 = set([1, 2, 3, 4, 5]),s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])。
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s1.isdisjoint(s2))
for item in s1:
if item in s2:
print(item)
else:
continue
查看全部 -
d = { 'Alice': 45, 'Bob': 60, 'Candy': 75, 'David': 86, 'Ellena': 49 }
print(d.get("ALice"))查看全部 -
例:针对以下set,给定一个list,对于list里面的每个元素,如果set中包含这个元素,就将其删除,否则添加到set里面去。L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],S = set([1, 3, 5, 7, 9, 11])。
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9])
for item in L:
if item in S:
S.remove(item)
else:
S.add(item)
print(S)查看全部 -
删除元素两种方法:
1.remove()方法。特别注意,删除前要确认所删内容是否在set里,否则会报错。
2.discard( )方法(此方法不会报错)
例:针对以下set,给定一个list,对于list里面的每个元素,如果set中包含这个元素,就将其删除,否则添加到set里面去。L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],S = set([1, 3, 5, 7, 9, 11])。
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9])
for item in L:
if item in S:
S.remove(item)
else:
S.add(item)
print(S)查看全部 -
添加set元素:
添加单个元素:add()方法,参数为要添加的内容
批量添加元素:update()方法,参数为要批量添加的内容,可以是list形式。
查看全部 -
例:由于name_set不能识别小写的名字,请改进name_set,是小写的名字也能判断在name_set里面。
解1:
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
name_set = str(set(names)).lower()
print('alice' in name_set)
解2:
names = ['Alice','alice', 'Bob', 'Candy', 'David', 'Ellena']
name_set = set(names)
name1 = 'bob'
if name1 in str(name_set).lower():
print(True)
else:
print(False)
查看全部 -
创建set的方式是使用set(),并传入一个list。list的元素将会被转换成set的元素。
有时,我们只想要 dict 的 key,不关心 key 对应的 value,目的就是保证这个集合的元素不会重复,这时,set就派上用场了。
查看全部 -
字符串format由两个部分组成,字符串模板和模板数据内容组成,通过大括号{},就可以把模板数据内容嵌到字符串模板对应的位置。
# 字符串模板
template = 'Hello {}'
# 模板数据内容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World如果模板中{}比较多,则容易错乱,那么在format的时候也可以指定模板数据内容的顺序。
# 指定顺序
template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.# 调整顺序
template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字对应的模板数据内容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
查看全部 -
在字符串前面加个前缀r,表示这是一个 raw 字符串,里面的字符就不需要转义了。
但是r'...'表示法不能表示多行字符串,也不能表示包含'和 "的字符串。
如果要表示多行字符串,可以用'''...'''表示
还可以在多行字符串前面添加r,把这个多行字符串也变成一个raw字符串:
r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''
查看全部 -
字典的特点:
优点:字典的查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样。而list的查找速度随着元素增加而逐渐下降。
缺点:字典占用内存大,还会浪费很多内容,list正好相反,占用内存小,但是查找速度慢。
tuple可以作为字典的key
例:同学的近三次成绩如下,请把每个同学的每次成绩依次输出。d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key in d.keys():
value=d[key]
for score in value:
print(key,score)
查看全部 -
在Python中,二进制整数使用前缀0b表示查看全部
-
Python可以处理任意大小的整数。查看全部
-
删除字典中的元素:(结合format的用法)
例:在dict中,使用keys()方法,可以返回dict的所有key,在删除某个元素时,可以通过这个方法先判断某个元素是否存在,请改造前面的程序,使得即使key不存在时,删除也不会抛异常。
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
name = 'John'
if name in d.keys():
d.pop(name)
else:
print('{} not in d'.format(name))查看全部
举报