我在使用这段代码时遇到了麻烦。
print("The Overall Winner is,", sorted(data, key=lambda(x,y): sum(n[1] for n in y), reverse=True),sum(event[1] for event in event_data))
这是说语法无效,我不知道它有什么问题。
这是该计划的其余部分
def main():
print("Welcome!\n")
n_events = get_number_of_events()
events_names = get_events_names(n_events)
print()
data = get_data(events_names)
print()
for house_name, event_data in data:
print ("House " + house_name)
for event_name, score in event_data:
# note the use of tuple unpacking
print ("\tEvent: %s Score: %i" % (event_name, score))
print("The Overall Winner is,", sorted(data, key=lambda(x,y): sum(n[1] for n in y), reverse=True),sum(event[1] for event in event_data))
感谢您提供任何帮助。
编辑:
整个计划。
LOWER_BOUND = 0
UPPER_BOUND = 100
def get_score(house_name, event_name):
score = -1
while score < LOWER_BOUND or score > UPPER_BOUND:
try:
score = int(input("Please enter score for %s in the event %s:" % (house_name, event_name)))
if score < LOWER_BOUND :
print ("Score is too low, minimum score is %i.\nPlease try again." % min_score)
if score > UPPER_BOUND:
print ("Score is too high, maximum score is %i\nPlease try again." % max_score)
except ValueError:
print("Invalid Input please enter an integer. Thanks")
return score
def get_number_of_events():
print("Please enter the number of events there is.")
while True:
try:
n_events = int(input(">>"))
except ValueError:
print("Enter only a number, Thanks.")
if n_events > 100:
print("WOW, that's a lot of events, please be reasonable. Thanks.")
elif n_events < 1:
print ("That's too few events! Try Again.")
else:
break
return n_events
def get_events_names(n_events):
print ("Please enter the events names")
events = []
for n in range(1, n_events + 1):
event_name = input("Event %i name: " % n)
events.append(event_name)
return events
def get_data(events):
data = []
while True:
house_name = input("Please enter house names <<<Enter XXX when finished>>> :")
if house_name.upper() == "XXX":
break
def main():
print("Welcome!\n")
n_events = get_number_of_events()
events_names = get_events_names(n_events)
print()
data = get_data(events_names)
print()
for house_name, event_data in data:
print ("House " + house_name)
for event_name, score in event_data:
print ("\tEvent: %s Score: %i" % (event_name, score))
print("The Overall Winner is,", sorted(data, key=lambda(x,y): sum(n[1] for n in y), reverse=True),sum(event[1] for event in event_data))
print ("Please enter the events points in ascending order.")
scores = []
for event_name in events:
score = get_score(house_name, event_name)
scores.append([event_name, score])
data.append([house_name, scores])
return data
if __name__ == '__main__':
main()
答案 0 :(得分:2)
假设您的实际程序中的缩进是正确的,并且这是Python 3.x(因为您似乎使用print作为函数),问题将在您的lambda语句中。一个很小的例子来展示你的问题 -
>>> a = lambda (x,y) : x+y
File "<stdin>", line 1
a = lambda (x,y) : x+y
^
SyntaxError: invalid syntax
在Python 3.x中,tuples
不能成为lambda参数语法的一部分,也就是说你不能直接将元素解包为多个参数(可以在Python 2.x中完成)。相反,您需要使用单个变量,然后使用下标访问元组的每个元素。示例 -
print("The Overall Winner is,", sorted(data, key=lambda x: sum(n[1] for n in x[1]), reverse=True),sum(event[1] for event in event_data))
另外,这对我来说似乎是不可读的,你应该把它分解成多行(也许在打印之前将中间结果保存在不同的变量中,这样会更具可读性)。
正如你在评论中所说的那样 -
例如,在这张图片中,我想要最高的字符串以及要打印的总点数,如picture.
所示
如果您想要具有最高值的元素,则获取第一个元素,并且由于数组的元素是再次列表,请使用下标来获取其名称。示例 -
print("The Overall Winner is,", sorted(data, key=lambda x: sum(n[1] for n in x[1]), reverse=True)[0][0],sum(event[1] for event in event_data))
根据评论的最新要求,做 -
sortedlist = sorted(data, key=lambda x: sum(n[1] for n in x[1]), reverse=True)
print("The Overall Winner is,", sortedlist[0][0], sum(n[1] for n in sortedlist[0][1]))