a = "1234567"
list_1 = []
odd_num = ["1","3","5","7","9"]
for i in range(0,len(a)):
if a[i] in odd_num:
list_1.append(i)
I want to make sure that each string a is an odd number (for example, to find 5791 and 753 and exclude 135798 with an even number), so even if the first digit is an odd number, it will be added to the list.
So the way I thought about it is to slice the strings, put them in the list one by one, put them together, and then put them together Even if you change the odd list to a set and add up the sets, they are still all odd if they are the same as the odd list set I think it's been too long since I tried to implemented too long Is there any simpler way to do it?
Thank you.
python
To give you one presumptuous opinion, please list 1,3,5,7,9 to determine odd numbers Drink. If you accidentally omit any of the five numbers, it will lead to a critical error, and if this three-year-old habit reaches 80 years old, you will never be a code monkey that hardcodes the server password or VIP customer member ID to the source. Please define the object, identify its properties, and derive a deductively true answer.It's called computer engineering.
Fortunately, odd/even numbers have an easy definition.
An odd number is n ZZ in which k ZZ with n=2k+1 exists. Source
In Python, when foo%2 == 1
, foo
is odd.
The rest of the description will be replaced by the operating code below.
def is_all_odd(num) :
for digit in str(num) :
if int(digit) % 2 == 0 :
return False
return True
def has_no_even(num) :
return len(filter(lambda digit: int(digit) % 2 == 0, str(num).split())) == 0
print(is_all_odd(5791))
print(is_all_odd(135798))
print(has_no_even(5791))
print(has_no_even(135798))
Check numeric character string ="1357"
All Odd Numbers = "13579"
set (checked numeric character string) <= set (all odd numbers)
It's possible like this. In the set type, <
and >
determine whether it is a subset.
Simple regular expression version.
import re
odd = re.compile("^\d[13579]*$")
print(odd.match('17536'))
print(odd.match('5364'))
print(odd.match('9753'))
print(odd.match('a9753'))
print(odd.match('993575'))
None
None
<re.Match object; span=(0, 4), match='9753'>
None
<re.Match object; span=(0, 6), match='993575'>
© 2024 OneMinuteCode. All rights reserved.