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

  1. [python学习笔记] String格式化

    格式化 S % (args...) 方式 特点 str里的占位符同java里的占位符. 优势 这种方式可以限定格式化的时候接受的数据类型. 常见占位符 %d 接收数字,格式化为 十进制 %x 接收数字 ...

  2. Python 学习小结

    python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...

  3. Python 学习文章收藏

    作者 标题 rollenholt Python修饰器的函数式编程 - Rollen Holt - 博客园 rollenholt python操作gmail - Rollen Holt - 博客园 ro ...

  4. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  5. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  6. python学习笔记系列----(八)python常用的标准库

    终于学到了python手册的最后一部分:常用标准库.这部分内容主要就是介绍了一些基础的常用的基础库,可以大概了解下,在以后真正使用的时候也能想起来再拿出来用. 8.1 操作系统接口模块:OS OS模块 ...

  7. Python学习路程day16

    Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...

  8. 记录Python学习中的几个小问题

    记录Python学习中的几个小问题,和C#\JAVA的习惯都不太一样. 1.Django模板中比较两个值是否相等 错误的做法 <option value="{{group.id}}&q ...

  9. 180分钟的python学习之旅

    最近在很多地方都可以看到Python的身影,尤其在人工智能等科学领域,其丰富的科学计算等方面类库无比强大.很多身边的哥们也提到Python非常的简洁方便,比如用Django搭建一个见得网站只需要半天时 ...

随机推荐

  1. 前端插件之Datatables使用--上篇

    工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,前边两篇分别介绍了Duallistbox插件和Select2插件的使用,这一篇开始Databases的征服之旅 D ...

  2. 利用反射,更改string对象的value数组以达到改变string值。

    package test; import java.lang.reflect.Field; import lombok.Value; public class Test1{ public static ...

  3. Kafka面试,看这篇文章就够了

    原文链接:https://mp.weixin.qq.com/s/zxPz_aFEMrshApZQ727h4g** 引言 MQ(消息队列)是跨进程通信的方式之一,可理解为异步rpc,上游系统对调用结果的 ...

  4. 有关element 的一些问题(随时更新)

    <el-input></el-input> input  组件中官方自带的change时间是监听失去焦点之后的value变化,要想一只监听value的值变化的话需要使用  @i ...

  5. Windows下的bat原来可以为我们做很多

    用了windows系统这么多年了,对bat也不是很了解.最近研究了一下bat的用法.这里就大概列举一下自己的用法 参考网址 基本命令 echo echo我们可以理解成程序中的输出,和我们Java的Sy ...

  6. K8S搭建-1 Master 2 Workers(dashboard+ingress)

    本文讲述k8s最新版的搭建(v1.15.2) 分如下几个topic步骤: 各个节点的基本配置 master节点的构建 worker节点的构建 安装dashboard 安装ingress 常见命令 do ...

  7. Yii2 登录报错

    当用数据库登录系统报如下错误时 PHP Recoverable Error – yii\base\ErrorException Argument 1 passed to yii\web\User::l ...

  8. linux command line learn - get the absolute path of a file

    get the absolute path of a file in linux readlink -f filenme [heshuai@login01 3_Variation_calling]$ ...

  9. mysql数据库磁盘空间被撑爆,创建定时任务定期释放资源

    问题描述: 这是我在工作中遇到的一个问题,目前只发现mysql数据库存在该问题,Oracle和gaussDB未发现磁盘空间被占满的情况,部署堆栈服务的时候抛出了写入数据库表失败的问题,经排查,在数据库 ...

  10. Python3 完美解决unittest框架下不生成测试报告

    前提: 1.运行测试用例一切正常,只是没有测试报告显示 2.使用命令行pyhon 脚本名字.py 却可以生成测试报告 3.pycharm 在运行测试用例的时候 默认是以unittest 框架来运行的, ...