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 ...
随机推荐
- C语言Socket-单工通信(客户端向服务器发送数据)
服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...
- Redis---Hash(字典)
1. 概述 Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿). ...
- 采用太平洋AI集装箱箱号识别接口实现集装箱箱号识别
识别 示例图片 1 太平洋AI集装箱箱号识别接口(文档下方有详细操作指南) 1.1 接口一:提交base64格式的图片 地址:http://218.1.125.60:88/container_num_ ...
- [Umbraco] document type里的父节点与子节点的设置
虽然我们不能像做数据库设计那样建立主外键关系.但我们建立xml里父子关系,父子关系其实是指是否允许在一个页面(如频道,分类,栏目等)下创建子页面,这就相当于建立站点的树状结构,对于筛选数据会有很大的作 ...
- Java实现二叉树先序,中序,后序,层次遍历
一.以下是我要解析的一个二叉树的模型形状.本文实现了以下方式的遍历: 1.用递归的方法实现了前序.中序.后序的遍历: 2.利用队列的方法实现层次遍历: 3.用堆栈的方法实现前序.中序.后序的遍历. . ...
- 为 git 设置 http 代理
最近基于 PDFium 项目做一些东西.之前得了代码,今天想要更新到最新的,发现怎么都 pull 不下来.后来想起来,可能是 git 没有使用代理的原因.于是添加代理,果然更新成功. 在 git ba ...
- 跌跌撞撞的看完了《jquery技术内幕》
今年2月20日买的书,今天是5月26,三个月来,除了周末休息一天,如果没有特殊情况,我都会每晚花两个小时看这本书,以及查各种与jquery源码相关的资料.今天总算是跌跌撞撞的看完了,有点小激动,也有点 ...
- j2ee高级开发技术课程第四周
分析hello.java,在hello1项目中.下载链接:https://github.com/javaee/tutorial-examples/tree/master/web/jsf/hello1 ...
- JDK8简化if-else
简化if-else 1234567891011 User user = ...if (user != null) { String userName = user.getUserName(); if ...
- Linux系统修改防火墙配置
防火墙配置文件位置 /etc/sysconfig/iptables 需要开放端口,请在里面添加一条内容即可: 1 -A RH-Firewall-1-INPUT -m state --state NEW ...