python基础技巧综合训练题2
1,判断一个字符串中的每一个字母是否都在另一个字符串中,可以利用集合的特性来解,集合的元素如果存在,再次更新(update) 是添加不进集合的,那么集合的长度还是跟原来一样,如果添加进去,集合长度就会增加
>>> a = 'ghost'
>>> b = 'hello, can you help me install ghost windows xp system'
>>> b_set = set( b )
>>> b_set.update( list( a ) )
>>> print len( b_set ) == len( set( b ) )
True
>>> a = 'abcostg'
>>> b_set.update( list( a ) )
>>> print len( b_set ) == len( set( b ) )
False
>>>
2,如果是多个字符呢?
#!/usr/bin/python
#coding:utf-8 #str_list = [ 'abc', 'ghost', 'hello' ]
str_list = [ 'abc', 'ghost', 'hellox' ]
target_str = "abcdefghijklopqrst"
target_str_set = set( target_str ) for val in str_list:
target_str_set.update( val ) print len( target_str_set ) == len( set( target_str ) )
3,统计出现次数最多的字符
ghostwu@ghostwu:~/python/tmp$ python str3.py
[('f', 7), ('s', 5), ('a', 4), ('j', 4), ('k', 3), ('h', 2), ('', 2), ('', 2), ('', 2), ('d', 1), ('l', 1), ('', 1), (';', 1)]
ghostwu@ghostwu:~/python/tmp$ cat str3.py
#!/usr/bin/python
#coding:utf-8 str = 'askfjkjasf1234fasdfasfsh;lkjfhjf123' l = ( [ ( key, str.count( key ) ) for key in set( str ) ] )
l.sort( key = lambda item : item[1], reverse = True )
print l ghostwu@ghostwu:~/python/tmp$
这里有个lambda表达式, key指定按哪个键排序, item是形参,代表当前的元组,item[1],那就是取元组中第2项,这里就是字符串的次数,reverse = True,从高到低排序 .
4,统计this模块中, be, is, than,三个单词的出现次数
ghostwu@ghostwu:~/python/tmp$ !p
python statics.py
[('be', 3), ('is', 10), ('than', 8)]
ghostwu@ghostwu:~/python/tmp$ cat statics.py
#!/usr/bin/python
#coding:utf-8 import os
this_str = os.popen( "python -m this" ).read()
this_str = this_str.replace( '\n', '' )
l = this_str.split( ' ' ) print [ ( x, l.count( x ) ) for x in ['be', 'is', 'than' ] ]
ghostwu@ghostwu:~/python/tmp$
os.popen( "python -m this" ).read 读出命令行python -m this 模块的执行结果到一个字符串中
5,用位移运算符,换算b, kb, mb之间的转换关系
ghostwu@ghostwu:~/software$ ls -l sogoupinyin_2.2.0.0102_amd64.deb
-rw-rw-r-- 1 ghostwu ghostwu 22852956 2月 2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
ghostwu@ghostwu:~/software$ ls -lh sogoupinyin_2.2.0.0102_amd64.deb
-rw-rw-r-- 1 ghostwu ghostwu 22M 2月 2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
ghostwu@ghostwu:~/software$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> size = 22852956
>>> print "%s kb" % ( size >> 10 )
22317 kb
>>> print "%s MB" % ( size >> 20 )
21 MB
>>>
6,把列表中的值,连接成字符串
>>> a = [10, 20, 30, 1, 2, 3]
>>> s = str( a )
>>> s
'[10, 20, 30, 1, 2, 3]'
>>> type( s )
<type 'str'>
>>> s[1:-1]
'10, 20, 30, 1, 2, 3'
>>> s.replace( ', ', '', s[1:-1] )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> s[1:-1].replace( ', ', '' )
''
>>>
python基础技巧综合训练题2的更多相关文章
- python基础技巧综合训练题1
1,大小写翻转 >>> str='hello,GhostWU' >>> str.swapcase() 'HELLO,gHOSTwu' 2,从一串字符串中,提取纯数字 ...
- python基础之逻辑题(3)
Python基础之逻辑题(3) 1.编写一个函数实现将IP地址转换成一个整数 2.求结果:---lambda 3.求a的结果 4.求下面nums的输出 5.求下面片段的输出 6.写出程序的结果:--- ...
- python基础之逻辑题(2)
python基础之逻辑题(2) 1.若k为整数,下列while循环执行的次数为? 2.请输出正确结果-----numbers? 3.求结果-----math? 4.求结果-----sum? 5.输 ...
- python基础之逻辑题(1)
python基础之逻辑题(1) 1.用一行代码实现数值交换? 2.求结果--fromkeys? 3.1,2,3,4,5能组成多少个互不相同且无重复的三位数? 4.有两个字符串列表a和b,每个字符串是逗 ...
- 面试题之第一部分(Python基础篇) 80题
第一部分(python基础篇)80题 为什么学习Python?==*== # 1. python应用于很多领域,比如后端,前端,爬虫,机器学习(人工智能)等方面,几乎能涵盖各个开发语言的领域,同时它相 ...
- python面试题解析(python基础篇80题)
1. 答:出于编程的喜爱,以及行业本身的前瞻性,创造性,优越性,越是综合的科目越能检验一个人的能力,喜欢这种有挑战的事情. 2. 答:跟随老师学习,以及自己查询资料,结合实战,进行输入输出以及 ...
- python(9)- python基础知识刷题
1. 执行 Python 脚本的两种方式 交互方式:命令行 Windows操作系统下,快捷键cmd,输入“python”启动交互式python解释器. 文件方式:python文件 2. 简述位.字 ...
- python基础面试常见题
1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途径学习的Python? 通过自学,包括网上查看一些视频,购买一 ...
- python 基础技巧
多个字典合并 >>> d1 = {'name' : 'revotu', 'age' : 99} >>> d2 = {'age' : 24, 'sex' : 'mal ...
随机推荐
- [学习笔记]树形dp
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...
- Javascript如何避免连续调用中取到不存在的属性而导致报TypeError错?
背景: 在最近的 NODEJS 项目中,涉及到数据库的查询,回调函数里返回了查询结果,我这样做处理然后返回给前端: return results.collect_coupon[0].count 但是这 ...
- 好用的在线工具汇总:Iconfont图标,数据mock,时间函数库,颜色查询 等
一 时间函数库 ———http://momentjs.com/ 非常全的时间处理函数库,引入使用非常方便. 二 Iconfont———http://www.iconfont.cn/ 各种小图标 ...
- 剑指offer十二之数值的整数次方
一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...
- 剑指offer五之用两个栈实现队列
一.题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 二.思路 1.Push操作:将数据直接压入stack1即可 2.Pop操作:将stack1中的数据全部弹 ...
- 在MVC3中修改KindEditor实现上传图片到指定文件夹
KindEditor编辑器默认上传的图片文件夹,是根据系统时间自动生成的,图片是自动上传到这些文件夹里面,无法选择.如果要上传图片到指定文件夹,像相册一样管理图片,则需要扩展KindEditor编辑器 ...
- 使用 Redis 共享 Session 会话
储存模式 1.InProc模式 这是ASP.NET默认的Session管理模式,在应用进程内维护Session. 2.StateServer模式 这是在服务器装了.NET环境后自带的一个StateSe ...
- java Queue的用法
https://www.cnblogs.com/caozengling/p/5307992.html https://blog.csdn.net/a724888/article/details/802 ...
- 老司机的应用级监控——spring actuator(转)
转自:https://www.jianshu.com/p/c043d3c71f47 什么是spring actuator? 这是一个研发老司机与运维同学都会非常喜欢的东西,随着点融集团的扩张,点融网的 ...
- BEA-290074 <Deployment service servlet received file download request for file "security/SerializedSystemIni.dat". The file may exist, but download of this file is not allowed.>
Bug 19766239 - WLS 12.1.3 - MS NOT STARTING - 'DOWNLOAD OF THIS FILE IS NOT ALLOWED' Issue is fixed ...