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!
- If you return before increment, you'll never have an increment.
- 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:
split_string = xyz.split('-')
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
_Corrupt_Record Error When Reading a Json File into Spark
Index Out of Bounds Error:Python
Get Span Inside a Class Using Webdriver and Selenium
Replacing Special Characters in a List in Python
Check If List Is Ascending or Descending (Using For)
How to Deal With Certificates Using Selenium
Converting Text File into Json in a Specific Format ( Python )
Create New Column Based on String
How to Normalize a 2-Dimensional Numpy Array in Python Less Verbose
How to Iterate Over Dates in a Dataframe
Best Practices for Adding .Gitignore File for Python Projects
Convert Timedelta to Floating-Point
How to Transform Floats to Integers in a List
Python - Returning Nan When Trying to Predict With Keras
How to Force Pip to Reinstall the Current Version
Using Regex to Find All Phrases That Are Completely Capitalized
How to Share Single Sqlite Connection in Multi-Threaded Python Application