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. POJ 2685

    #include <iostream> #include <string> #define MAXN 26 using namespace std; int _map[MAXN ...

  2. javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)

    javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...

  3. Entity framework 预热

    Entity framework  预热 对于在应用程序中定义的每个DbContext类型,在首次使用时,Entity Framework都会根据数据库中的信息在内存生成一个映射视图(mapping ...

  4. WTF小程序之<web-view>

    叨叨两句 昨天爬了一下午坑才出来的我向大家问好

  5. php -- 格式化字符串

    ----- 003-output.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  6. Web前后端分离知识整理

    Web研发模式的演变 职责分离(减少扯皮,开发效率),代码分离(可维护性) 简单明快的早期时代 后端为主的 MVC 时代 Ajax 带来的 SPA 时代 前端为主的 MV* 时代 Node 带来的全栈 ...

  7. redis学习(七)redis主从复制

    redis主从复制 1.redis主从复制的作用 redis的定位是一个高可用的数据服务器,可是在实际生产环境下,单机的redis服务器是无法满足真正意义上的高可用性的. 第一,单机的redis服务器 ...

  8. SpringBoot入门 (十二) 定时任务

    本文记录在SpringBoot中使用定时任务. 在我们的项目中,经常需要用到定时任务去帮我们做一些事情,比如服务状态监控,业务数据状态的更改等,SpringBoot中实现定时任务有2中方案,一种是自带 ...

  9. linux编译找不到aprt apr-util

    Linux很多地方编译的时候都会用到apr 如果找不到apr就会报错 configure: WARNING: APR not found The Apache Portable Runtime (AP ...

  10. LVS专题-(3) 虚拟ip理解

    1.虚拟IP是什么? 要是单讲解虚拟 IP,理解起来很困难,所以干脆把 动态 IP .固定 IP .实体 IP 与虚拟 IP都讲解一下,加深理解和知识扩展 实体 IP:在网络的世界里,为了要辨识每一部 ...