len(object) -> integer
Return the number of items of a sequence or mapping.
根据用户输入生成列表:
针对你的评论,我想我会加上这一点。这是一种方法:
user = raw_input("Enter a string: ").split() # with python3.x you'd be using input instead of raw_input
Enter a string: Hello there sir and madam # user enters this string
print user
['Hello', 'there', 'sir', 'and', 'madam']
现在要使用max:
print max(user, key=len)
'Hello' # since 'Hello' and 'madam' are both of length 5, max just returns the first one
如果要在列表中搜索最长的字符串,可以使用内置的
max()
函数:max
接受一个“key”参数,您可以将一个应用于myList中每个项的函数(在本例中是len
,另一个内置函数)。在在这种情况下,
myList
中每个字符串的len(string)
返回的最大结果(长度)是最长的字符串,并由max
返回。在来自
^{pr2}$max
文档字符串:来自
len
文档字符串:根据用户输入生成列表:
针对你的评论,我想我会加上这一点。这是一种方法:
现在要使用max:
相关问题 更多 >
编程相关推荐