3376| 0
|
[大脑洞] python for循环实现小功能以及推导式的分享 |
For 循环. 一、使用for循环实现简单功能 '''乘法表1--9乘法表for循环实现数字遍历'''for i in range(1,10): # 行中 for s in range(1,i+1): # 列中 print('%s X %s=%s '%(s,i,s*i),end ='') print()''' 星星实现星星以三角形排列'''for i in range(1,9): # 行 for s in range(len(range(1,9))-i): print(' ',end ='') for n in range(1,2*i): # 列 print('*',end = '') print() '''金额小写=>大写对循环数字进行拆分对数组合'''list_num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']list_U = ['园','拾','佰','仟','萬']input_num = input('输入数字:')len_U = len(input_num)for i in input_num: len_U -=1 print("{0}{1}".format(list_num[int(i)],list_U[len_U]),end='')print('整') 运行: E:\python_VS_code\directory[目录]>D://py3.6//python.exe e:/python_VS_code/directory[目录]/demo0802/py_for_mult.py1 X 1=11 X 2=2 2 X 2=41 X 3=3 2 X 3=6 3 X 3=91 X 4=4 2 X 4=8 3 X 4=12 4 X 4=161 X 5=5 2 X 5=10 3 X 5=15 4 X 5=20 5 X 5=251 X 6=6 2 X 6=12 3 X 6=18 4 X 6=24 5 X 6=30 6 X 6=361 X 7=7 2 X 7=14 3 X 7=21 4 X 7=28 5 X 7=35 6 X 7=42 7 X 7=491 X 8=8 2 X 8=16 3 X 8=24 4 X 8=32 5 X 8=40 6 X 8=48 7 X 8=56 8 X 8=641 X 9=9 2 X 9=18 3 X 9=27 4 X 9=36 5 X 9=45 6 X 9=54 7 X 9=63 8 X 9=72 9 X 9=81 * *** ***** ******* ********* *********** ****************************输入数字:1234壹仟贰佰叁拾肆园整二、推导式: 列表推导式:va=[rxp for out in list if (out 条件)]获得两个列表交集va3=[10,20,30,40]va4=[20,30,50,60,70]Va5=[ num for num in va3 if num in va4 ]获得并集va6=[ num for num in va3 if num not in va4 ]print(va6)print(va4+va6)获得差集va7=[ num for num in va4 if num not in va3 ]print(va7)-------------------------------------------------------------- # 获得0-99之间的 奇数的二倍值 va2=[i*2 for i in range(100) if i%2!=0 ] print('va2,',va2) 生成器 对象(元组推导式)gen=( i for i in range(5)) print(gen) # for i in gen: # print(i)------------------------------------------------------------ 字典推导式mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3,'B':20,'b':50}var={keyexcept:vaExcept for key in 字典对象 }# if k.lower() in ['a','b','z'] 判断条件mcase_frequency = { k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) #第三部执行 for k in mcase.keys()#第一步 if k.lower() in ['a','b','z'] #第二部判断执行 true 执行第三部-------------------------------------------------------集合推导式mcase_key={key for key in mcase if key== key.lower()}三,同样要求的三种不同解决方式 '''统计字符串中数字和字母的个数1.使用异常处理非数字 try...except2.使用内置函数判定非数字 isdigit()3.使用字符转换判断数字字母范围。ord() 字符转ascii码 chr() ascii码转字符'''str_for = 'adashgdkahfqa223354564'num = 0str_num = 0'''第一种'''for i in str_for: try: int(i) num +=1 except ValueError: str_num +=1print('数字:',num,'\n''字母:',str_num)'''第二种'''num1 = 0str_num1 = 0for i in str_for: if i.isdigit() == True: num1 +=1 else: str_num1 +=1print('数字:',num1,'\n''字母:',str_num1)'''第三种'''num2 = 0str_num2 = 0for i in str_for: if ord('0') < ord(i) <ord('9'): num2 +=1 elif ord('a') <= ord(i) <=ord('z') or ord('A') <= ord(i) <= ord('Z'): str_num2 += 1print('数字:',num2,'\n''字母:',str_num2)四、推导式解决【金额大小写转换,字典推导式练习】 list_num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']list_U = ['园','拾','佰','仟','萬']input_num = input('输入数字:')len_U = len(input_num)num_info = [int(input_num)//10**i%10 for i in range(len_U-1,-1,-1)]for i in num_info: len_U -=1 print('%s%s'%(list_num,list_U[len_U]),end = '')print('整')'''字典对象初始化定义数据把 Key是 str类型长度大于等于5的信息获取用推导式'''dict_1 = {'a':12,123:'abcd','12345':True,'sadasdds':'18895','asd':'d'}dict_infer = {k:v for k,v in dict_1.items() if type(k) ==str and len(k) >= 5}print(dict_infer)文章转载自python教程网:www.99kao.com 转载请注明出处 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed