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的更多相关文章

  1. python基础技巧综合训练题1

    1,大小写翻转 >>> str='hello,GhostWU' >>> str.swapcase() 'HELLO,gHOSTwu' 2,从一串字符串中,提取纯数字 ...

  2. python基础之逻辑题(3)

    Python基础之逻辑题(3) 1.编写一个函数实现将IP地址转换成一个整数 2.求结果:---lambda 3.求a的结果 4.求下面nums的输出 5.求下面片段的输出 6.写出程序的结果:--- ...

  3. python基础之逻辑题(2)

    python基础之逻辑题(2) 1.若k为整数,下列while循环执行的次数为? 2.请输出正确结果-----numbers? 3.求结果-----math?   4.求结果-----sum? 5.输 ...

  4. python基础之逻辑题(1)

    python基础之逻辑题(1) 1.用一行代码实现数值交换? 2.求结果--fromkeys? 3.1,2,3,4,5能组成多少个互不相同且无重复的三位数? 4.有两个字符串列表a和b,每个字符串是逗 ...

  5. 面试题之第一部分(Python基础篇) 80题

    第一部分(python基础篇)80题 为什么学习Python?==*== # 1. python应用于很多领域,比如后端,前端,爬虫,机器学习(人工智能)等方面,几乎能涵盖各个开发语言的领域,同时它相 ...

  6. python面试题解析(python基础篇80题)

    1.   答:出于编程的喜爱,以及行业本身的前瞻性,创造性,优越性,越是综合的科目越能检验一个人的能力,喜欢这种有挑战的事情. 2.   答:跟随老师学习,以及自己查询资料,结合实战,进行输入输出以及 ...

  7. python(9)- python基础知识刷题

    1.  执行 Python 脚本的两种方式 交互方式:命令行 Windows操作系统下,快捷键cmd,输入“python”启动交互式python解释器. 文件方式:python文件 2.  简述位.字 ...

  8. python基础面试常见题

    1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途径学习的Python? 通过自学,包括网上查看一些视频,购买一 ...

  9. python 基础技巧

    多个字典合并 >>> d1 = {'name' : 'revotu', 'age' : 99} >>> d2 = {'age' : 24, 'sex' : 'mal ...

随机推荐

  1. Windows安装python3.x后,pip list警告!DEPRECATION: The default format will switch to columns in the future.

    前言(凑字数专用) 这个警告虽然不影响你的正常使用,但是每次都好几行红色警告,总是给人一种怪怪的感觉(当然不是FBI的警告了……),所以咱们还是把他解决掉~ 网上好多解决办法都是Ubuntu的解决办法 ...

  2. C语言Socket-模拟远程CMD(客户端向服务器发送命令,服务器执行该命令)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  3. Akka(20): Stream:异步运算,压力缓冲-Async, batching backpressure and buffering

    akka-stream原则上是一种推式(push-model)的数据流.push-model和pull-model的区别在于它们解决问题倾向性:push模式面向高效的数据流下游(fast-downst ...

  4. Yii2 Apache + Nginx 路由重写

    一.什么是路由重写 原本的HTTP访问地址: www.test.com/index.php?r=post/view&id=100 表示这个请求将由PostController 的 action ...

  5. Linux学习笔记-基本操作4

    1. gdb调试2. makefile的编写3. 系统IO函数 1. gdb调试:        1. 启动gdb + 可执行文件        2. 查看代码:                l== ...

  6. Windows 出现了回声 & 微软账号无法登陆

    Windows 出现了回声,第一反应是杜比音效偷偷背着我开启了客厅模式(后面看了下并没有这个模式,后话了...). 再我尝试打开它发现提示网络无法连接,于是我就直接卸载了,但回声依能没有解决. 后面我 ...

  7. 编译lua-5.3.5时出错解决方法

    问题描述: 执行 make linux test 过程出现错误,错误信息如下: “ lua.c:82:31: fatal error: readline/readline.h: No such fil ...

  8. 13、最新安卓Xamarin绑定相关填坑之旅

    今天群里面有兄弟伙说第三方库用不起.说实话在我觉得第三方库能成功的几率大于90% 除了极少数恶心的库以外. 绝大部分第三方库都还是可以绑定好的 https://github.com/youzan/Yo ...

  9. MySQL命令行登陆,远程登陆MySQL

    注: MySQL图形界面管理工具[navicat 10.1.8中文绿色版] 下载地址:http://www.t00y.com/file/18393836 备用地址:http://ProCircle.q ...

  10. 【从0到1学jQuery】jQuery中each()和$.each()的使用

    引子: 最近遇到一个问题,就是在each()函数中怎么模拟for循环中的break和continue的操作.所以就查看了jQuery关于这个函数的文档,并且总结一下. 演示代码如下: <div& ...