+ 1

Im confused. Explain it for me plz. Step by step

Ex1: list=["car","plane","train","bike","rocket"] new=sorted(list,key=lambda x:x[-2]) print(new) ---》['car','rocket','train','bike','plain'] Ex2: x=100.1205 y=str(x)[6] print("{:.{}f}.format(x,y)) --》100

21st Aug 2019, 11:20 AM
Gin
Gin - avatar
5 odpowiedzi
+ 2
For Ex1, sorted(list,key=lambda x:x[-2]) means to sort the elements(represented by x) in the list. The key=lambda x:x[-2] means that instead of using the default way,arranging the elements by the first letter and giving ['bike', 'car', 'plane', 'rocket', 'train'],the sequence depends on the [-2] element in each string,which is a,n,i,k,e. Car is the first element as a is the 1st alphabet. For Ex2,x is a float number while y=0.(You missed the " )In("{:.{}f}".format(x,y)),x is placed in the outside curly brackets while y is placed in the inside one. So you can interpret as "100.1205:.{0}". Number after :. is the precision. As it is zero,the numbers behind the decimal point are removed so output is only 100.
21st Aug 2019, 1:48 PM
Ketchup🍅
Ketchup🍅 - avatar
+ 1
Ex1 wanted to sort the elements by the second last characters of the strings: ["car", "plane", "train", "bike", "plain"] | "a" | "n" | "i " | "k" | "i" | By those characters, program sorted the strings: | "a" | "i" | "i" | "k" | "n" | ["car", "train", "plain", "bike", "plane"] Which was returned by the sorted function and printed. Im not sure about Ex2, format method has so many ways to format strings.
21st Aug 2019, 1:08 PM
Seb TheS
Seb TheS - avatar
0
I realize that i forget a lot of something. Hazz. Its so helful for me. Thank you kindly
21st Aug 2019, 2:06 PM
Gin
Gin - avatar
0
Gin By the way,where did you see these codes? Not easy stuff.
21st Aug 2019, 2:09 PM
Ketchup🍅
Ketchup🍅 - avatar
0
It's a random question. I play it in challenge(python).
21st Aug 2019, 2:14 PM
Gin
Gin - avatar