《Python Data Structures》 Week4 List 课堂笔记
2024-08-27 08:06:38
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")
最新文章
- socket读写返回值的处理
- Linux内核--内核数据类型
- es6学习笔记2-解构赋值
- ucenter 新增用户后 自动登录discuz 不用激活
- JAVA求解线性方程组-列主元高斯消去法
- python 列表操作
- ORA-32004 参数设置过时的解决办法
- [转]浅谈Python web框架
- 【转自CSDN】深入 Microsoft.VisualBasic.Strings.StrConv 簡繁轉換
- 第一个微信小程序(实现点击一个按钮弹出toast)
- Angular - - $templateCache 和 $templateRequest
- 直方图均衡化CImg实现
- [bzoj1273] [BeiJingWc2008]序列
- CentOS 6.3 64位下MySQL5.1.54源码安装配置详解
- python实现三级菜单
- HTML常用布局---新浪布局
- A1139. First Contact
- js之close()方法
- Faster R-CNN在GPU下的安装、测试经历
- poj3259 Wormholes【最短路-bellman-负环】