Python学习之String
Strings可以想象成一个有序列的数组
#Indexing
planet = 'Pluto'
planet[0]
'P'
#Slicing
planet[-3:]
'uto'
#How long
len(planet)
String methods
#字母大写
claim = 'Pluto is a planet'
claim.upper()
#all lowercase
claim.lower()
#search for the first index of substring
claim.index('plan')
Going between strings and lists : .splits and .join()
words = claim.split()
datestr='1956-01-31'
year,month,day = datestr.split('-')
'/'.join ([month,day,year])
'/'.join([word.upper() for word in words])
如过数值是non-string object,则可以先使用str 转换数值
str(position)
format占位符
This is getting hard to read and annoying to type. str.format() to the rescue
"{}. you'll always be the{}th planet to me".format{planet,position}
检测元组中是否有数值:
seq = ['one', 'two', 'three']
if 'one' in seq:
print True
Dictionraries
numbers={'one':1, 'two':2, 'three': 3}
numbers['one']
1
numbers['eleven']=11
numbers
练习一:
A researcher has gathered thousands of news articles. But she wants to focus her attention on articles including a specific word. Complete the function below to help her filter her list of articles.
Your function should meet the following criteria
- Do not include documents where the keyword string shows up only as a part of a larger word. For example, if she were looking for the keyword “closed”, you would not include the string “enclosed.”
- She does not want you to distinguish upper case from lower case letters. So the phrase “Closed the case.” would be included when the keyword is “closed”
- Do not let periods or commas affect what is matched. “It is closed.” would be included when the keyword is “closed”. But you can assume there are no other types of punctuation.
Answer:
Solution:
def word_search(doc_list, keyword):
indices = []
for i,doc in enumerate(doc_list):
temp=doc.split()
word = [item.strip('.,').lower() for item in temp]
if keyword.lower() in word:
indices.append(i)
return indices
Python学习之String的更多相关文章
- [python学习笔记] String格式化
格式化 S % (args...) 方式 特点 str里的占位符同java里的占位符. 优势 这种方式可以限定格式化的时候接受的数据类型. 常见占位符 %d 接收数字,格式化为 十进制 %x 接收数字 ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- Python 学习文章收藏
作者 标题 rollenholt Python修饰器的函数式编程 - Rollen Holt - 博客园 rollenholt python操作gmail - Rollen Holt - 博客园 ro ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- python学习笔记系列----(八)python常用的标准库
终于学到了python手册的最后一部分:常用标准库.这部分内容主要就是介绍了一些基础的常用的基础库,可以大概了解下,在以后真正使用的时候也能想起来再拿出来用. 8.1 操作系统接口模块:OS OS模块 ...
- Python学习路程day16
Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...
- 记录Python学习中的几个小问题
记录Python学习中的几个小问题,和C#\JAVA的习惯都不太一样. 1.Django模板中比较两个值是否相等 错误的做法 <option value="{{group.id}}&q ...
- 180分钟的python学习之旅
最近在很多地方都可以看到Python的身影,尤其在人工智能等科学领域,其丰富的科学计算等方面类库无比强大.很多身边的哥们也提到Python非常的简洁方便,比如用Django搭建一个见得网站只需要半天时 ...
随机推荐
- 小X的逆袭
[问题描述]毕业于普通本科的小x 一直自称是资深屌丝.谁又能想到,如此不起眼的小x 在历经重重面试环节后,竟然如愿以偿加入了心仪已久的腾讯公司!正所谓野百合也有春天,屌丝也有逆袭的那一天!一段时间以后 ...
- 什么时候使用redis?什么时候使用memcache?
要清楚为什么,redis具有高可用特性,并且可固化,但特性有时候不能成为选择他的理由,一些业务场景中并不需要这样的特性. 什么时候倾向于选择redis? 1.复杂数据结构 value是哈希,列表, ...
- Winform改变Textbox边框颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 最大层内元素和----leetcode周赛150_1002
题目描述: 给你一个二叉树的根节点 root.设根节点位于二叉树的第 1 层,而根节点的子节点位于第 2 层,依此类推. 请你找出层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 ...
- Windows Server 2008配置系统安全策略
下面学习Windows Server 2008配置系统安全策略 在工作组中的计算机本地安全策略有 用户策略,密码策略,密码过期默认42天 服务账户设置成永不过期,帐户锁定策略,本地策略,审核策略,计算 ...
- 谈自由,ASP.NET Core才是未来?
首先我要说一下自己对自由的理解: 自由是我可以选择不干什么,但我要保留我可以干什么的可能性. 比如说我现在只有一个码农的角色,但我仍然要保留我可以扮演其他角色的可能, 比如成为一个作者,当我写下文章的 ...
- static用法总结:
这是我的第一篇随笔! 1.当1.cpp和2.cpp都定义了同一个全局时链接会报错,变量名冲突.需要将其中一个设置为static控制其作用域为只在一个源文件内部. 2.函数名也是全局的,故static的 ...
- Spring Context 你真的懂了吗
今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...
- MySQL多表关联数据同时删除
MySQL多表关联时的多表删除: DELETE t1, t2FROM t1LEFT JOIN t2 ON t1.id = t2.idWHERE t1.id = 25
- 从一道没人能答对的面试题聊聊Java的值传递
这是一道我们公司的面试题,从招第二个Java以来就一直存在了.但是面试了这么长的时间还没有一个人可以全部答对,让我们一度以为是这题出的不对.首先请看面试题. 以下运算的输出分别是多少: ```java ...