我想知道你是否可以在google python类上给我一些问题,它们基于列表。
# A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.
def match_ends(words):
print words
# B. front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with 'x' first.
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
def front_x(words):
return
这些是问题,我有几个尝试,从来没有真正到过任何地方,我想知道是否有人可以这样做,所以我可以看到答案并向后工作。
谢谢! 乔治
答案 0 :(得分:2)
def match_ends(words):
count = 0
for item in words:
if len(item) >= 2 and item[0] == item[-1]:
count += 1
return count
def front_x(words):
x_list = []
other_list = []
for item in words:
if item[0].lower() == "x":
x_list.append(item)
else:
other_list.append(item)
x_list.sort()
other_list.sort()
return x_list + other_list
答案 1 :(得分:0)
你不会从被告知答案中学到很多东西,但也许这可以帮助你自己实现目标:
def match_ends(words):
count = 0
for word in words:
# (do something)
return count
def front_x(words):
with_x = []
without_x = []
for i in words:
# (do something)
# (sort the lists)
return with_x + without_x