题目:
# Define a procedure, add_to_index, # that takes 3 inputs: # - an index: [[<keyword>,[<url>,...]],...] # - a keyword: String # - a url: String # If the keyword is already # in the index, add the url # to the list of urls associated # with that keyword. # If the keyword is not in the index, # add an entry to the index: [keyword,[url]] index = [] def add_to_index(index,keyword,url): #add_to_index(index,'udacity','http://udacity.com') #add_to_index(index,'computing','http://acm.org') #add_to_index(index,'udacity','http://npr.org') #print index #>>> [['udacity', ['http://udacity.com', 'http://npr.org']], #>>> ['computing', ['http://acm.org']]]我的答案(我搞不出来…):
def add_to_index(index,keyword,url): if index. index.append([keyword, [url]]) """ for i in range(len(index)): if keyword not in index[i]: index.append([keyword]) for i in range(len(index)): if len(index[i]) == 1: index[i].append([]) if index[i][0] == keyword: index[i][1].append(url) """ """ for i in range(len(index)): if index[i][0] == keyword: index[i][1].append(url) return index """视频的答案(我看的不是很懂):
index = [] def add_to_index(index,keyword,url): for e in index: if e[0] == keyword: e[1].append(url) return index.append([keyword, [url]]) add_to_index(index,'udacity','http://udacity.com') add_to_index(index,'computing','http://acm.org') add_to_index(index,'udacity','http://npr.org') print index #>>> [['udacity', ['http://udacity.com', 'http://npr.org']], #>>> ['computing', ['http://acm.org']]]( Linux 下的跟踪方式: 1 . 安装 traceroute :
$ sudo apt-get install traceroute 跟踪访问某个网站的路由: $ traceroute www.udacity.com)