python
暮一白
这个作者很懒,什么都没留下…
展开
-
python set集合的用法
set是一个无需不重复的元素组合去重list2 = ["a","b","c","d","a"]print(set(list2))结果:{‘d’, ‘b’, ‘a’, ‘c’}交集(intersection),并集(union),差集(difference),对称差集(symmetric_difference)list1 = [1,2,3,1,4,"a"]list2 = ["a","b","c","d","a"]print(set(list1).union(set(list2)))原创 2020-08-07 11:07:12 · 128 阅读 · 0 评论 -
python unittest测试
unittest定义示例注意点定义unittest是python标准库自带的测试模块示例测试文件为test_example.py注意点测试的方法名称必须以字符串test开始,否则不会执行;测试代码模块(文件)名称默认需要满足test*.py,但也可以从命令行中提供–pattern关键字参数来指定任意模式。运行命令运行整个文件python -m unittest test_example运行测试文件里的某一个类python -m unittest test_example.M原创 2020-08-01 15:24:24 · 169 阅读 · 0 评论 -
python循环
计算1+2+3+4+…+99+100的和while方法count = 1result = 0while count < 101: result = result+count count=count+1else: print(result)结果: 5050for in方法result=0for i in range(1,101): result +=iprint(result)结果: 5050continue,break区别:continue原创 2020-07-31 18:15:50 · 181 阅读 · 0 评论 -
python --双重循环
for i in range (1,4): print(" ") print("外层第%s遍循环: "%i) for j in range(1, 4): print("内层第%s遍循环: "%j,end=" ") print(" ") 结果其实就是如下的遍历:(1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3)原创 2020-07-31 17:51:24 · 6424 阅读 · 0 评论 -
python 中字符串String和列表List的转换
-字符串转化成列表:split:以什么为分割a = 'dsfjdoe'print(list(a))c= a.split(' ')print(c)结果:[‘d’, ‘s’, ‘f’, ‘j’, ‘d’, ‘o’, ‘e’][‘dsfjdoe’]列表转化成字符串join:以什么字符加入a = ['d', 's', 'f', 'j', 'd', 'o', 'e']b= '-'.join(a)c=''.join(a)print(b)print(c)结果:d-s-f-j-d-原创 2020-07-30 17:25:13 · 198 阅读 · 0 评论 -
python: list, tuple, dict,set ,string的区别
Difference:是否能改变值的不可改变值的:tuple可以改变值的 : list ,dict,set特征tuple:圆括号,以‘,’逗号分隔,任意类型的值list:方括号[],任意类型的值set:{},建立空的值为set(),任意类型值dict:{},键值对的形式存在,创建空的直接为{},任意类型值将tuple,set,set转化成listsetName = {1,2,3,4}tuple2 = (5,6,7,8)dict2 ={'a':1,'b':2}resu原创 2020-07-30 16:43:49 · 279 阅读 · 0 评论 -
python set集合基础
Python Set 基础集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典基础hello = set()hello.add('b')print(hello)结果: {‘b’}hello = {'a','b'}hello.update({1,3})print(hello)hello.remove(3) #删除指定的元素print(hello原创 2020-07-30 16:24:46 · 104 阅读 · 0 评论 -
python元组基础
python的元组是不可变的,方法与列表类似,采用(),最主要的特征,是以“,”分隔。元组里的值是不可以被改变的元组被删除后,输出变量会有异常信息tup1=('abc','edf', 333, 777)print(tup1[0])tup2 =('22','kk')tup3 = tup1 + tup2print(tup3)print(tup1[:])tup1[0]= 666结果:abc(‘abc’, ‘edf’, 333, 777, ‘22’, ‘kk’)(‘abc’, ‘ed原创 2020-07-30 15:37:35 · 136 阅读 · 0 评论 -
python 字典的处理
字典字典的键是不变的,可以使“字符串,数字,元组”举例:dict1={'alice':'2341',98.6:37} #创建一个字典print(dict1['alice']) #取值dict1['alice']=9876 #更新字典的某个值print(dict1['alice']) dict1['school']='hello' #给该字典加入一个新值print(dict1)del dict1['school'] #删除schoolprint(dict1)dict1.clear()原创 2020-07-30 15:17:00 · 265 阅读 · 0 评论 -
python机试算法题
求两个数的最小公倍数# 定义函数def lcm(x, y): # 获取最大的数 if x > y: greater = x else: greater = y while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 retu.原创 2020-07-27 17:14:42 · 309 阅读 · 0 评论 -
python列表List的处理
1. 列表去重使用setduplicateList = ['1','aa','3','aa','23','1','bb']newList= set(duplicateList)print(newList)结果:{‘3’, ‘aa’, ‘23’, ‘1’, ‘bb’}2. 获取列表中的值单个列表根据索引取值a = ['1','aa','3','aa','23','1','bb']print(a[0])print(a[0:4])print(a[::-1])结果:1[‘1’,原创 2020-07-27 13:39:47 · 469 阅读 · 0 评论 -
python的字符串处理
参考 https://www.runoob.com/python/python-strings.html1.将一个字符串变成列表(每一个字母分开)str='adwee'newStr=list(str)print(newStr)结果:[‘a’, ‘d’, ‘w’, ‘e’, ‘e’]split方法str.split(str="", num=string.count(str)).str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。num – 分割次数。默认为原创 2020-07-25 23:24:46 · 148 阅读 · 0 评论 -
python pip upgrade
Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: ‘c:\program files\python37\lib\site-packages\pip-10.0.1.dist-info\entry_points.txt’Consider using the --user opti...原创 2019-03-18 17:13:51 · 2314 阅读 · 0 评论