+ 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
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.
+ 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.
0
I realize that i forget a lot of something. Hazz. Its so helful for me. Thank you kindly
0
Gin By the way,where did you see these codes? Not easy stuff.
0
It's a random question. I play it in challenge(python).