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

最新文章

  1. C# Winform中如何让PictureBox的背景透明
  2. Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  3. Tp缓存
  4. ubuntu 终端快捷键
  5. HTML5在VS2010中的智能提示
  6. 无向图最小生成树(prim算法)
  7. Python 的property的实现 .
  8. jquery mobile 栅格化
  9. shell中timeout实现
  10. InputStream和OutputStream
  11. android 项目中使用对话框统一封装
  12. bower 教程
  13. hadoop2.2 datanode 启动不了
  14. redux学习日志:关于异步action
  15. 02Vue2.0+生命周期
  16. socket和webService的区别
  17. Java枚举:小小enum,优雅而干净
  18. 2019-2-14sql server数据库模糊查询语句
  19. 【C#】C#格式化文件大小
  20. BZOJ3207花神的嘲讽计划Ⅰ——主席树+hash

热门文章

  1. Android使用xUtils3上传图片报错解决:java.lang.ArrayIndexOutOfBoundsException: 70918
  2. 02.Mybatis的动态代理方式实现增删改查
  3. 【Kubernetes 系列三】Kubernetes 学习文档推荐
  4. 理解-NumPy
  5. 逆向破解之160个CrackMe —— 014
  6. 蓝桥杯c语言基础题
  7. springboot的log4j配置与logback配置
  8. 如何实现QQ附件在线预览功能
  9. Android进阶之绘制-自定义View完全掌握(五)
  10. java JVM原理讲解和调优和gc