您的位置:首页 > 编程语言 > Python开发

Python快速入门(3)列表、练习题

2014-01-24 21:55 567 查看
06

序列:(三种类型)

字符串 不可以修改

列表list [] 可以修改 ex.[1,2.3]

元组tuple () 不可以修改 ex. uinfo = ('well,'male',20,'njupt')

----特点:

1.可以进行索引,索引为负数,则从右边开始计数

2.可以使用切片操作符 [m:n]

----基本序列操作:

1. len()

2. + #拼接

3. *n #重复n次

4. in #判断元素是否在序列中

5. max() #返回最大的值

6. min() #返回最小的值

7. cmp(seq1,seq2) #比较2个序列值是否相同

07

列表list:[] ---- 可变的!

namelist = ['well','tom']

nl = namelist  ##does not copy the list
#Instead, assignment makes the two variables point to the one list in memory.


----专属操作:

1. list.append #追加一个值 namelist.append('lucy')

2. del #del namelist[1] 删除列表索引为1的元素

3. list.remove #删除第一个匹配性 namelist.remove('well')

-------

tuple(seq) #将序列转换为tuple

list(seq) #将列表转换为list

创建列表:

list = []          ## Start as the empty list
list.append('a')   ## Use append() to add elements
list.append('b')


列表切片:同字符串切片一样

list = ['a', 'b', 'c', 'd']
print list[1:-1]   ## ['b', 'c']
list[0:2] = 'z'    ## replace ['a', 'b'] with ['z']
print list         ## ['z', 'c', 'd']


for 和 in :

for var in list  #遍历一个列表
value in collection #测试集合中是否存在一个值


Range:

#The range(n) function yields the numbers 0, 1, ... n-1,

#and range(a, b) returns a, a+1, ... b-1 -- up to but not including the last number.

while循环:

List常用方法:

list.append(elem) #-- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
list.insert(index, elem) #-- inserts the element at the given index, shifting elements to the right.
list.extend(list2) #adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
list.index(elem) -- #searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use "in" to check without a ValueError).
list.remove(elem) -- #searches for the first instance of the given element and removes it (throws ValueError if not present)
list.sort() -- #sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
list.reverse() -- #reverses the list in place (does not return it)
list.pop(index) -- #removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).


相关练习:

# C. sort_last

# Given a list of non-empty tuples, return a list sorted in increasing

# order by the last element in each tuple.

# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields

# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]

# Hint: use a custom key= function to extract the last element form each tuple.

def last(a):
return a[-1]

def sort_last(tuples):
# +++your code here+++
return sorted(tuples,key=last)


# E. Given two lists sorted in increasing order, create and return a merged

# list of all the elements in sorted order. You may modify the passed in lists.

# Ideally, the solution should work in "linear" time, making a single

# pass of both lists.
def linear_merge(list1, list2):
# +++your code here+++
# LAB(begin solution)
result = []
# Look at the two lists so long as both are non-empty.
# Take whichever element [0] is smaller.
while len(list1) and len(list2):
if list1[0] < list2[0]:
result.append(list1.pop(0))
else:
result.append(list2.pop(0))

# Now tack on what's left
result.extend(list1)
result.extend(list2)
return result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: