This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I would like to have an if statement where the match is one of several numbers without using a large number of | bars because I have many matching cases
if number == 1 | number == 5 | number == 7
do stuff
In R, there is the operand %in% that works sort of like the following:
if number %in% [1,5,7]
do stuff
Is there a similar operand/ability in python?
Thanks
You can use something like
if number in [1, 5, 7]:
Related
This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
RegEx - Match Numbers of Variable Length
(4 answers)
Closed 4 months ago.
My case is extract number between text (ex: FL-number-$) from string in File names column to Check column. Example:
2022-06-09-FR-Echecks.pdf > Return ''
2022-06-09-FR-FL-3-$797.pdf > Return 3
2022-06-09-FR-TX-20-$35149.91.pdf > Return 20
My case as below
This code I used:
dt_test['File_names_page'] = dt_test['File names'].str.extract('\-([0-99])-\$')
It only return one digit number as below:
So how to extract all number (all digit) in my case?
Tks for all attention!
Your regex pattern is slightly off. Just use \d+ to match any integer number:
dt_test["File_names_page"] = dt_test["File names"].str.extract(r'-(\d+)-\$')
You can't use a 0-99 range, you should use \d{1,2} for one or two digits:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d{1,2})-\$')
Or for any number of digits (at least 1) \d+:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d+)-\$')
NB. - doesn't require an escape
Example:
File names File_names_page
0 ABC-12-$456 12
This question already has answers here:
How to check last digit of number
(10 answers)
Closed 4 years ago.
I was wondering if there was any way to get the ones place of a integer. For example, if I had the number 41, would there be any easy way to get 1 from that? Thanks!
Take modulo by 10
a = float(input('Enter a number : '))
print( a % 10)
Modulo operator return the remainder of number so any number modulo with 10 will return its ones number for more https://docs.python.org/3/reference/expressions.html
This question already has answers here:
multiple excel if statements to produce value 1,2 or 3
(3 answers)
Closed 4 years ago.
Screenshot
I'm trying to use and if statement to return a possible 3 different values. I'm trying to determine if a customer re-ordered this year after ordering last year.
My results should be re-ordered, only 2018 or Q4 not Q1. My formula is : =IF(AND(I15>0,J15<1),"Q4 not Q1","Re-Ordered")
But as you can see I'm not sure if I should be adding an or statement or what the layout of the formula should be. Any help is greatly appreciated. Everything I've found on this keeps returning my statement as false.
An IF statement can only return 2 values, so if you want to return 3, you have to nest it inside another IF like this:
=IF(A=B,"A equals B",IF(A=C, "A equals C", "A does not equal B and A does not equal C"))
Based on your screenshot:
=IF(AND(I12>0,J12<1),"Q4 not Q1",IF(AND(I12>0,J12>0),"Re-Ordered","2018 Only"))
This question already has answers here:
Lexicographic minimum permutation such that all adjacent letters are distinct
(6 answers)
re-arrange items into an array with no similar items next to each other
(3 answers)
Closed 8 years ago.
How to re-arrange string so that same characters are not next to each other and if there are many alternative sorting options we'll choose the one which is alphabetically sorted?
i.e.
AAABBBB -> BABABAB
AAABBB -> ABABAB
BCDDEEEF -> BCEDEDEF
BACHH -> ABHCH
Pseudo code or something would be useful.
A naive solution:
Find all permutations of the string
Find all that don't have repeating characters
Find the first alphabetically
This question already has answers here:
Display number with leading zeros [duplicate]
(19 answers)
Closed 8 years ago.
Apologies if this is a repeat question but,
what formatting commands do i need to use if I want a single digit number to be displayed with a zero in front?
i.e. the number '2' would be displayed as '02'.
[But, I do not want any value above 10 to have extra zeros in front]
cheers
You can use this syntax:
>>> "{:0>2}".format(2)
'02'
>>> "{:0>2}".format(98)
'98'
>>> "{:x>4}".format(2)
'xxx2'
More info: Common string operations
Try:
if 0 < num < 10:
return "0" + str(single_digit)