python中 关于 list 和 string 的问题
defmake_numberlist(first,last):"""Returnalistcontainingallofthenumbersfromfirsttolast...
def make_numberlist(first, last):
"""
Return a list containing all of the numbers from first to last inclusive.
>>> make_numberlist(0,3)
[0, 1, 2, 3]
>>> make_numberlist(2,10)
[2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
def common_elements(list1, list2):
"""
Return a list containing the elements which are in both list1 and list2
>>> common_elements([1,2,3,4,5,6], [3,5,7,9])
[3, 5]
>>> common_elements(["this","this","n","that"],["this","not","that","that"])
['this', 'that']
"""
def remove_section(alist, start, end):
"""
Return a copy of alist removing the section from start to end inclusive
>>> inlist = [8,7,6,5,4,3,2,1]
>>> remove_section(inlist, 2, 5)
[8, 7, 2, 1]
>>> inlist == [8,7,6,5,4,3,2,1]
True
>>> inlist = ["bob","sue","jim","mary","tony"]
>>> remove_section(inlist, 0,1)
['jim', 'mary', 'tony']
>>> inlist == ["bob","sue","jim","mary","tony"]
True
"""
def add_lists(lista, listb):
"""
Returns a new list containing the sum of each corresponding pair of items
>>> add_lists([1, 1], [1, 1])
[2, 2]
>>> add_lists([1, 2], [1, 4])
[2, 6]
>>> list1 = [1, 2, 1]
>>> list2 = [1, 4, 3]
>>> add_lists(list1, list2)
[2, 6, 4]
>>> list1 == [1, 2, 1]
True
>>> list2 == [1, 4, 3]
True
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True) 展开
"""
Return a list containing all of the numbers from first to last inclusive.
>>> make_numberlist(0,3)
[0, 1, 2, 3]
>>> make_numberlist(2,10)
[2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
def common_elements(list1, list2):
"""
Return a list containing the elements which are in both list1 and list2
>>> common_elements([1,2,3,4,5,6], [3,5,7,9])
[3, 5]
>>> common_elements(["this","this","n","that"],["this","not","that","that"])
['this', 'that']
"""
def remove_section(alist, start, end):
"""
Return a copy of alist removing the section from start to end inclusive
>>> inlist = [8,7,6,5,4,3,2,1]
>>> remove_section(inlist, 2, 5)
[8, 7, 2, 1]
>>> inlist == [8,7,6,5,4,3,2,1]
True
>>> inlist = ["bob","sue","jim","mary","tony"]
>>> remove_section(inlist, 0,1)
['jim', 'mary', 'tony']
>>> inlist == ["bob","sue","jim","mary","tony"]
True
"""
def add_lists(lista, listb):
"""
Returns a new list containing the sum of each corresponding pair of items
>>> add_lists([1, 1], [1, 1])
[2, 2]
>>> add_lists([1, 2], [1, 4])
[2, 6]
>>> list1 = [1, 2, 1]
>>> list2 = [1, 4, 3]
>>> add_lists(list1, list2)
[2, 6, 4]
>>> list1 == [1, 2, 1]
True
>>> list2 == [1, 4, 3]
True
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True) 展开
展开全部
问题1
def make_numberlist(first,last):
num=[i for i in range(first,last)]
return num
问题2
def common_element(list1,list2):
c=[]
for i in range(len(list1)):
if list1[i] in list2 and list1[i] not in c:
c.append(list1[i])
return c
问题3
def remove_section(alist,start,end):
a=alist[:]
del a[start:end+1]
return a
问题4
def add_lists(lista,listb):
c=[]
for i in range(len(lista)):
c.append(lista[i]+listb[i])
return c
def make_numberlist(first,last):
num=[i for i in range(first,last)]
return num
问题2
def common_element(list1,list2):
c=[]
for i in range(len(list1)):
if list1[i] in list2 and list1[i] not in c:
c.append(list1[i])
return c
问题3
def remove_section(alist,start,end):
a=alist[:]
del a[start:end+1]
return a
问题4
def add_lists(lista,listb):
c=[]
for i in range(len(lista)):
c.append(lista[i]+listb[i])
return c
更多追问追答
追问
第二个函数 end+1,又可能出了 范围。
追答
抱歉,舒服大意了,以下是订正结果
def remove_section(alist,start,end):
a=alist[:]
if end+1>len(alist):
end=len(alist)-1
del a[start:end+1]
return a
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询