||
Python Village - INI3: Strings and Lists
Python除了strings和numbers数据类型,还有更加复杂一点的数据类型,比如说lists。
list的赋值方式是list_name = [item_1, item_2, ..., item_n],比如:
>>>
注意,在Python中,list内部item计数是从0开始的,因此tea_party[2]对应的item是'Dormouse',而不是'Hatter'。
可以直接赋予list新的值已取代旧的item:
>>>
第2个item从'Hatter'变成了'Cheshire Cat'。
另外,往已有的list中添加item可以用append()函数:
>>>
可见tea_party最后新增了一个item,'Jabberwocky’。
如果想获取某个list中的某几个元素,可以使用list_name[a:b]函数:
>>>
可见list_name[a:b]获取的是从a到b,但不包括b的items。
另外,a或者b一方为空值的话,表示取a到最后一个值或者第一个值到b:
>>>
此外,也可以使用负数,代表从后往前数:
>>>
因为tea_party一共有5个item,因此tea_party[3:]与tea_party[-2:]得到的结果是一样的。
同样,我们可以用这种方法去应用于strings上:
>>>
b[0:1]返回的是m,a[2:]返回的是imsy,加起来就是mimsy。
Given: A string s of length at most 200 letters and four integers a, b, c and d.
Return: The slice of this string from indices a through b and c through d (with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.
22 27 97 102
Humpty Dumpty
这个需要看清楚题目,第一个是输出的值中间有空格,第二个是s[b]与s[d]包括在内:
Rosalind is a platform for learning bioinformatics and programming through problem solving. Take a tour to get the hang of how Rosalind works.
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 08:16
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社