Coursera课程《Python Data Structures》 密歇根大学 Charles Severance

Week4 List

8.2 Manipulating Lists

8.2.1 Concatenating Lists Using +

使用“+”可以把存在的两个list加在一起。如:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]

8.2.2 Lists Can Be Sliced Using

list切片和string一样,第二个数字只是到这个数,但却不包括它。

>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41, 12]
>>> t[:4]
[9, 41, 12, 3]
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]

8.2.3 List Methods

list有很多方法,如append, count, extend, index, insert, pop, remove, reverse, sort等。

8.2.4 Building a List from Scratch

我们可以创建一个空list,然后再向里面添加元素,使用"append"方法。使用这个方法的话,新增的元素是放在list的末尾的。

>>> stuff = list()
>>> stuff.append('book')
>>> stuff.append(99)
>>> print(stuff)
['book', 99]

8.2.5 Is Something in a List?

判断一个元素是否在一个list里,可以使用"in""not in",而python则会返回一个逻辑布尔值。而这项操作并不会改变list本身。

>>> some = [1, 9, 21, 10, 16]
>>> 9 in some
True
>>> 15 in some
False
>>> 20 not in some
True

8.2.6 Lists are in Order

使用"sort"可以对list进行排序。

>>> friends = ['Joseph', ' Glenn', 'Sally']
>>> friends.sort()
>>> print(friends)
['Glenn', 'Joseph', 'Sally']

8.2.7 Built-in Functions and Lists

>>> nums = [3, 41, 12, 9, 74, 15]
>>> print(len(nums))
6
>>> print(max(nums))
74
>>> print(min(nums))
3
>>> print(sum(nums))
154
>>> print(sum(nums)/len(nums))
25.6

8.3 Lists and Strings

8.3.1 Best Friends: Strings and Lists

split可以将一个字符串分割成几部分,形成一个字符串的list。这样我们就能取到这个字符串的每一个单词,或者使用一个循环把它都过一遍。

>>> abc = "With three words"
>>> stuff = abc.split()
>>> print(stuff)
['With', 'three', 'words']
>>> print(len(stuff))
3
>>> print(stuff[0])
With
>>> for w in stuff:
... print(w)
...
With
Three
Words

同时split会把很多个空格当成一个来处理,默认把它当成定界符。当然,也可以自己决定用其他符号当作定界符。

>>> line = 'A lot           of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces']
>>> line = 'first;seconed;third'
>>> thing = line.split()
>>> print(thing)
['first;second;third']
>>> print(len(thing))
1
>>> thing = line.split(';')
>>> print(thing)
['first', 'second', 'third']
>>> print(len(thing))
3

8.3.2 The Double Split Pattern

如果我们想得到下面这一行中的加粗部分

From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

那么我们可以这样做

words = line.split()
email = words[1]
pieces = email.split('@')
print(pieces[1])

8.4 Assignment

作业的代码如下

fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
temp = line.split()
for word in temp:
if word not in lst:
lst.append(word)
lst.sort()
print(lst)

8.5 Assignment

作业的代码如下

fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname)
count = 0
for line in fh:
temp = line.split()
if len(temp) < 1 or temp[0] != 'From':
continue
print(temp[1])
count+=1 print("There were", count, "lines in the file with From as the first word")

《Python Data Structures》 Week4 List 课堂笔记的更多相关文章

  1. 【Python学习笔记】Coursera课程《Python Data Structures》 密歇根大学 Charles Severance——Week6 Tuple课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week6 Tuple 10 Tuples 10.1 Tuples A ...

  2. 《Python Data Structures》Week5 Dictionary 课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week5 Dictionary 9.1 Dictionaries 字 ...

  3. 潭州课堂25班:Ph201805201 爬虫基础 第七课 Python与常见加密方式 (课堂笔记)

    打开图形界面  18版 Python与常见加密方式 前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时 ...

  4. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  5. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  6. Python Tutorial 学习(五)--Data Structures

    5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...

  7. [译]The Python Tutorial#5. Data Structures

    [译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...

  8. 《Data Structures and Algorithm Analysis in C》学习与刷题笔记

    <Data Structures and Algorithm Analysis in C>学习与刷题笔记 为什么要学习DSAAC? 某个月黑风高的夜晚,下班的我走在黯淡无光.冷清无人的冲之 ...

  9. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

随机推荐

  1. 【学习总结】快速上手Linux玩转典型应用-第5章-远程连接SSH专题

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 认识SSH 2. 服务器安装SSH服务 3. 客户端安装SSH工具 4. 客户端链接SSH服务 5. SSH config 6. SSH ...

  2. JAVA中自定义properties文件介绍

    Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...

  3. deletefile 与KILL

    1.Kill 语句 从磁盘中删除文件.语法Kill pathname必要的 pathname 参数是用来指定一个文件名的字符串表达式.pathname 可以包含目录或文件夹.以及驱动器.说明在 Mic ...

  4. ID学习一 Basic

    Assignment 作用:定义变量并赋值 变量可以是新定义的也可以是已经存在的: 值可以是另一个变量的值.一个文本值.一个复杂的表达式(利用表达式编辑助手构造): 注意:一旦变量被定义,你不能删除变 ...

  5. 图像描点标注-labelme的安装及使用

    1.直接使用pip安装lebelme pip install labelme 2.labelme的使用 找到labelme的安装路径,先找到python的安装路径如我的,C:\Users\Think\ ...

  6. MacOS Mojave 安装sshpass

    使用sshpass的场景 安装sshpass及各种常见小问题处理 测试 安全提示 使用sshpass的场景 在MacOS下使用ansible命令(inventory文件中使用了密码验证的方式)或者使用 ...

  7. vector存放结构体数据的2种方法

    如果要在Vector容器中存放结构体类型的变量,经常见到两种存放方式. 方式一:放入这个结构体类型变量的副本. 方式二:放入指向这个结构体类型变量的指针. 假设结构体类型变量是这样的, typedef ...

  8. MySQL免安装版 配置

    1. MySQL官方网址:https://dev.mysql.com/downloads/mysql/ 2. 将下载文件解压到一个文件夹:D:\AZ\ 3. 配置环境变量:Path:D:\AZ\mys ...

  9. 开发框架DevExtreme全新发布v19.1.3|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  10. SparkConf源码解读

    ------------恢复内容开始------------ 1.主要功能:SparkConf是Spark的配置类,配置spark的application的应用程序,使用(key,value)来进行存 ...