Given a List L of Video Names and Their Watch Rates, Write a Function That Will Return the Videos With the Top 10 Watch Rates

I am trying to write a function xyz(a) that loops through the no.s 0 through a and returns Jackpot if it finds the number 777 .or else Try again

There are many errors!

  1. If you return before increment, you'll never have an increment.
  2. If you return after increment , no point of while loop.

Solution

Simple if else

def xyz(a):


if a==777:
return "Jackpot"
else:
# i+=1
# return "Try again"
return ("Try Again")

str.split()[] - How it works?

So, here is the explanation of this function, line by line.

pref = xyz.split('-')[1]: This line can be split in the following two lines:

  1. split_string = xyz.split('-')
  2. pref = split_string[1]

The first line means "look at the string and cut it wherever you find the character '-'. For example, for the string "Axx-000.xyz", the character is found once and splitted_string will be a list of 2 strings: ["Axx","000.xyz"].

The second line means "put in pref the second element of the list split_string.

Then the line pref = pref.replace('.xyz', '') call the method replace, which means: "look at the string and wherever you find the string '.xyz', replace it by '' (so nothing).

So the value returned by the function contains the second element of the table ["Axx","000.xyz"] without '.xyz', so simply 000.

How do I sort nested string elements

To get your new sorted list you have to first reverse tuples inside sublists, sort the sublists, and the sort the whole list:

old_x = [(('ABC', '123'), ('XYZ', 'ABC'), ('XYZ', '123')),(('ABC', '123'), ('DEF', 'ABC'), ('DEF', '123'))]

new_x = sorted([sorted(tuple(val[::-1] for val in subl)) for subl in old_x])

print(new_x)

Prints:

[[('123', 'ABC'), ('123', 'DEF'), ('ABC', 'DEF')], [('123', 'ABC'), ('123', 'XYZ'), ('ABC', 'XYZ')]]


Related Topics



Leave a reply



Submit