学习的时间过的真快,每天都觉得时间不够用。
看到新的知识点,在查询资料的过程中,知识网越铺越大,想一直泡在里面!
目前了解到Pandas库,操作好强大。略兴奋!
这篇是上周末做的练习题笔记,从jupyter notebook中导出。
多读书,惜四时!
1. 一行代码实现1--100之和
# 一行代码实现1--100之和
# print(sum(list(range(1,101))))
# Python中range函数可以生成数字列表,可以直接用在sum函数中。不需要在使用list函数。
print(sum(range(1,101)))
运行结果 :5050
2. 列出5个python标准库
os:提供了不少与操作系统相关联的函数
sys:通常用于命令行参数
re:正则匹配
math:数学运算
datetime:处理日期时间
3. 字典如何删除键和合并两个字典?
删除:使用pop函数或者del删除
# 使用del函数删除字典中键值对
alien_0 = {'color':'green','points':5}
del alien_0['color']
print(alien_0)
运行结果: {'points': 5}
#使用pop函数删除字典键值对
alien_0 = {'color':'green','points':5}
alien_0.pop('color')
print(alien_0)
运行结果:{'points': 5}
合并:
z = dict(x.items() + y.items()) 这种方法,对于python3来说,是无法成功执行的。
因为items()返回是个对象。 你必须明确的将它强制转换成list: z = dict(list(x.items()) + list(y.items()))
#合并方法1:
dict1 = { "name":"owen", "age": 18 }
dict2 = { "birthday": "1999-11-22", "height": 180 }
new_dict = dict(list(dict1.items())+list(dict2.items()))
print(new_dict)
运行结果: {'name': 'owen', 'age': 18, 'birthday': '1999-11-22', 'height': 180}
#合并方法2:
dict1 = { "name":"owen", "age": 18 }
dict2 = { "birthday": "1999-11-22", "height": 180 }
new_dict = {**dict1,**dict2}
print(new_dict)
运行结果: {'name': 'owen', 'age': 18, 'birthday': '1999-11-22', 'height': 180}
#合并方法3:
dict1 = { "name":"owen", "age": 18 }
dict2 = { "birthday": "1999-11-22", "height": 180 }
dict3 = {} #定义空字典
dict3.update(dict1)
dict3.update(dict2)
print(dict3)
运行结果:{'name': 'owen', 'age': 18, 'birthday': '1999-11-22', 'height': 180}
在上述合并方法中,解释下update的用法: Python字典update()函数将参数字典中的键值对更新到dict里。
语法: dict.update(dict2),dict2是指添加到字典dict里面的字典。
前提,是定义字典dict,通常定义一个空字典。
4. Python实现列表去重的方法
集合(set)是一个无序的不重复元素序列。
为剔除重复项,可使用集合(set)。集合set类似于列表,但每个元素必须是独一无二的。
a = [1,2,3,2,3,1]
print(set(a))
运行结果: {1, 2, 3}
5. fun(args,**kwargs)中的args,**kwargs什么意思?
args = argument 参数
args 传入的参数个数是可变的。 *args是用来发送一个键值对可变数量的参数列表给一个函数,没有value值。 一个星号的作用是将tuple或者list中的元素进行unpack,分开传入,作为多个参数。
kwargs是keyword argument的缩写,为关键字参数。 关键字参数可以传入任意个带有参数名的参数,这些参数会在函数内部组装成一个字典格式。 两个星号*的作用是把dict类型的数据作为参数传入。 *kargs将不定长度的键值对,作为参数传递给一个参数,有key-value值。
当函数的参数不确定时,可以使用args 和**kwargs,args 没有key值,**kwargs有key值。
def this_fun(a,b,*args,**kwargs):
print(a)
print(b)
print(args)
print(kwargs)
this_fun(0,1,2,3,4,index1=11,index2=22,index3=33)
运行结果:
0
1
(2, 3, 4)
{'index1': 11, 'index2': 22, 'index3': 33}
6. Python内建数据类型有哪些?
数字 number:整数型int 布尔型bool 浮点型float 复数complex numbers
字符串 string 如: "hello world!"
元祖 tuple 如:(1,2,3,'name')
列表 list 如: [1,2,3,4] 字典 dictionary 如: {'name','color','model','country'}
集合 set
python3以上六个标准数据类型中:
不可变数据(3个): 数字number、字符串string、元祖Tuple
可变数据(3个):列表List、字典Dictionary、集合set
7. 列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25].
# 使用map()函数输出[1,4,9,16,25]
a = [1,2,3,4,5]
b = list(map(lambda x:x*x,a))
print(b)
运行结果: [1, 4, 9, 16, 25]
# 使用列表推导式提取出大于10的数
data_select = [x for x in b if x>10]
print(data_select)
运行结果: [16, 25]
我也尝试用for语句写了段代码:
# 使用for语句
a = [1,2,3,4,5]
b = list(map(lambda x:x*x,a))
c = []
for i in range(0,len(b)):
if b[i] > 10:
c.append(b[i])
print(c)
运行结果: [16, 25]
8. s = "ajldjlajfdljfddd",去重并从小到大排序输出"adfjl"。(提示,.需要用到join函数)
s = "ajldjlajfdljfddd"
ss = "".join(sorted(list(set(s))))
print(ss)
运行结果: adfjl
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串
语法: str.join(sequence)
sequence -- 要连接的元素序列
str-- 指定的字符
返回通过指定字符连接序列中元素后生成的新字符串。