get the sum of differences between integers in a list
Peter Otten
__peter__ at web.de
Tue Sep 20 12:21:17 EDT 2016
Daiyue Weng wrote:
> Hi, I have a list numbers say,
>
> [1,2,3,4,6,8,9,10,11]
>
> First, I want to calculate the sum of the differences between the numbers
> in the list.
> Second, if a sequence of numbers having a difference of 1, put them in a
> list, i.e. there are two such lists,
>
> [1,2,3]
Why isn't 4 in that list?
> [8,9,10,11]
>
> and also put the rest numbers in another list, i.e. there is only one such
> list in the example,
>
> [6].
>
> Third, get the lists with the max/min sizes from above, i.e. in this
> example, the max list is,
>
> [8,9,10,11]
>
> min list is
>
> [1,2,3].
In addition to Steve's hint: max() takes a key argument, e. g. to get the
string with the highest amount of "a"s:
>>> max(["xyz", "abc", "alpha"], key=lambda s: s.count("a"))
'alpha'
Finding the longest string or list doesn't even require you to define your
own function.
More information about the Python-list
mailing list