Task Two Day

Task2(2day)

  1. 列表

1)标志

2)基本操作(创建,append(),pop(),del(),拷贝)

3)列表相关方法

2.元组

1)标志

2)基本操作(创建及不可变性)

3.string字符串

1)定义及基本操作(+,*,读取方式)

2)字符串相关方法

4.字符串格式化问题

1.创建列表。只要把逗号分隔的不同的数据项使用方括号括起来即可

1 List = [‘wade’,‘james’,‘bosh’,‘haslem’]
与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等

2.添加新的元素

复制代码
1 List.append(‘allen’) #方式一:向list结尾添加 参数object
2 >>> a=[1,2,3,4]
3 >>> a.append(5)
4 >>> print(a)
5 [1, 2, 3, 4, 5]
6
7 List.insert(4,‘lewis’) #方式二:插入一个元素 参数一:index位置 参数二:object
8 >>> a=[1,2,4]
9 >>> a.insert(2,3)
10 >>> print(a)
11 [1, 2, 3, 4]
12
13 List.extend(tableList) #方式三:扩展列表,参数:iterable参数
14 >>> a=[1,2,3]
15 >>> b=[4,5,6]
16 >>> a.extend(b)
17 >>> print(a)
18 [1, 2, 3, 4, 5, 6]
复制代码
3.遍历列表

1 for i in List:
2 print i,
4.访问列表中的值
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:

1 >>> List = [1, 2, 3, 4, 5, 6, 7 ]
2 >>> print(List[3])
3 4
5.从list删除元素

复制代码
1 List.remove() #删除方式一:参数object 如有重复元素,只会删除最靠前的
2 >>> a=[1,2,3]
3 >>> a.remove(2)
4 >>> print(a)
5 [1, 3]
6
7 List.pop() #删除方式二:pop 可选参数index删除指定位置的元素 默认为最后一个元素
8 >>> a=[1, 2, 3, 4, 5, 6]
9 >>> a.pop()
10 6
11 >>> print(a)
12 [1, 2, 3, 4, 5]
13
14
15 del List #删除方式三:可以删除整个列表或指定元素或者列表切片,list删除后无法访问。
16 >>> a=[1, 2, 3, 4, 5, 6]
17 >>> del a[5]
18 >>> print(a)
19 [1, 2, 3, 4, 5]
20 >>> del a
21 >>> print(a)
22 Traceback (most recent call last):
23 File “<pyshell#93>”, line 1, in
24 print(a)
复制代码

6.排序和反转代码

复制代码
1 List.reverse()
2 >>> a=[1, 2, 3, 4, 5, 6]
3 >>> a.reverse()
4 >>> print(a)
5 [6, 5, 4, 3, 2, 1]
6
7
8 List.sort() #sort有三个默认参数 cmp=None,key=None,reverse=False 因此可以制定排序参数
9 >>> a=[2,4,6,7,3,1,5]
10 >>> a.sort()
11 >>> print(a)
12 [1, 2, 3, 4, 5, 6, 7]
13 #python3X中,不能将数字和字符一起排序,会出现此报错
14 >>> a=[2,4,6,7,3,1,5,‘a’]
15 >>> a.sort()
16 Traceback (most recent call last):
17 File “<pyshell#104>”, line 1, in
18 a.sort()
19 TypeError: unorderable types: str() < int()
复制代码
7.Python列表截取

复制代码
1 Python的列表截取与字符串操作类型相同,如下所示:
2 L = [‘spam’, ‘Spam’, ‘SPAM!’]
3 操作:
4 Python 表达式 结果 描述
5 L[2] ‘SPAM!’ 读取列表中第三个元素
6 L[-2] ‘Spam’ 读取列表中倒数第二个元素
7 L[1:] [‘Spam’, ‘SPAM!’] 从第二个元素开始截取列表
复制代码
8.Python列表操作的函数和方法

1 列表操作包含以下函数:
2 1、cmp(list1, list2):比较两个列表的元素 (python3已丢弃)
3 2、len(list):列表元素个数
4 3、max(list):返回列表元素最大值
5 4、min(list):返回列表元素最小值
6 5、list(seq):将元组转换为列表
复制代码
1 列表操作常用操作包含以下方法:
2 1、list.append(obj):在列表末尾添加新的对象
3 2、list.count(obj):统计某个元素在列表中出现的次数
4 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
5 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
6 5、list.insert(index, obj):将对象插入列表
7 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
8 7、list.remove(obj):移除列表中某个值的第一个匹配项
9 8、list.reverse():反向列表中元素
10 9、list.sort([func]):对原列表进行排序
Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套。不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。

创建元组

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可

tup1 = ()          #空元组

tup2 = (‘a’,‘b’,‘c’,‘d’)

tup3 = (1,2,3,‘a’,‘b’,‘c’)

元组操作方法及实例展示

可以使用dir(tuple)查看元组支持的操作

count

1 功能:统计元组中某元素的个数
2 语法:T.count(value) -> integer – return number of occurrences of value
3 T = (‘a’,‘b’,‘c’,‘d’,1,2,2,3,4)
4 T.count(2)
5 结果:2
index

复制代码
1 功能:获取元素在元组中的索引值,对于重复的元素,默认获取从左起第一个元素的索引值
2 语法:T.index(value, [start, [stop]]) -> integer – return first index of value.Raises ValueError if the value is not present.
3 T = (‘a’,‘b’,2,‘c’,‘d’,1,2,3,4)
4 T.index(2)
5 结果:2 #元素2第一次出现在索引为2的位置
6 T.index(2,3,7)
7 结果:6
复制代码
T1 + T2

复制代码
1 功能:合并两个元组,返回一个新的元组,原元组不变
2 语法:T = T1 + T2
3 T1 = (‘a’,‘b’,‘c’)
4 T2 = (1,2,3,4)
5 T = T1 + T 2
6 结果:
7 print T
8 (‘a’,‘b’,‘c’,1,2,3,4)
9 print T1
10 (‘a’,‘b’,‘c’)
11 print T2
12 (1,2,3,4)
复制代码
T1 * N

复制代码
1 功能:重复输出元组N次,返回一个新元组,原元组不变
2 语法:T = T1 * N
3 T1 = (‘a’,‘b’,1,2,3)
4 T = T1 * 3
5 结果:
6 print T
7 (‘a’,‘b’,1,2,3,‘a’,‘b’,1,2,3,‘a’,‘b’,1,2,3)
8 print T1
9 (‘a’,‘b’,1,2,3)
复制代码
元组虽然不可变,但是当元组中嵌套可变元素时,该可变元素是可以修改的,元组本身不变,使用id(tuple)查看。

复制代码
1 T = (‘a’,‘b’,‘c’,[1,2,3,4],1,2,3)
2 id(T)
3 140073510482784
4 print T[3]
5 [1,2,3,4]
6 T[3].append(5)
7 print T[3]
8 [1,2,3,4,5]
9 print T
10 (‘a’,‘b’,‘c’,[1,2,3,4,5],1,2,3)
11 id(T)
12 140073510482784
复制代码
元组支持切片操作

复制代码
1 语法:T[start [, stop[, step]]]
2 实例演示:
3 T = (‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’)
4 print T[:] #取所有元素
5 (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’)
6 print T[2:] #取从索引2开始到末尾的元素
7 (‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’)
8 print T[2:6] #取索引2到6的所有元素,不包含索引6
9 (‘c’, ‘d’, ‘e’, ‘f’)
10 print T[2:6:2] #从索引2到6,每隔一个元素取一个
11 (‘c’, ‘e’)

一、String
 String 是定义一个字符串对象(内存中的字符串都是一个对象。)
 String 一旦被初始化就不能被改变(可以改变变量指向,但是不能改变对象内容)
 
定义方式: String s1 = “abc”; //在内存中存在一个对象。
String s2 = new String(“abc”)//在内存中存在两个对象。
         String s3 = “abc”;

二、字符串的常见方法
1、获取:字符串中包含的字符数(也就是字符串的长度)
int length(); (注意与数组中的length做区别,数组中为属性,字符串中为方法)

2、判断

2.1、判断字符串中是否包含指定字符串
boolean contains(CharSequence s) :CharSequence:为String实现的接口

3、转换
将字符数组转换成字符串
构造函数: String(char[] value)
String(char[] value, int offset, int count): 将数组中从下标offset开始,一共count位字符转换成字符串。

4、替换
 String replace(char oldChar, char newChar) : 返回一个数组,它是用newChar 替换就数组中的oldchar等到的。(一个一个的替换)
String replace(CharSequence target, CharSequence replacement) : 后替前,用新串替换原串中的子串。

注:原字符串没有改变,只是新出现了一个替换后的字符串(字符串一旦初始化,不能改变)
如果要替换的字符没有,还是返回原串,不会生成新的字符串。

5、切割,分割

String[] split(String regex) : 指定其中某一个字符或字符串,以其下刀,切割字符串(其实应当依据正则表达式规则拆分)

6、子串:(获取一个字符串的一部分)

String substring(int beginIndex) //从指定下标位置到结尾。
String substring(int beginIndex, int endIndex) //从指定下标位置到结束下标位置前一个

7、转换、去除空格、比较

7.1:将字符串转换成大写或者小写。
String toUpperCase()
String toLowerCase()
7.2:将字符串两端多余的空额去除。
           String trim()
7.3:将两个字符串进行自然顺序的比较。
         
int compareTo(String anotherString)
int compareToIgnoreCase(String str) :不考虑大小写。

字符串格式化

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值