9 examples of 'how to split a number in python' in Python

Every line of 'how to split a number in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
28def num_split(s: str) -> Tuple[Union[int, str], ...]:
29 """Splits a word into sequences of numbers and non-numbers.
30
31 Creates a tuple from these where the number are converted into int datatype.
32 That way a naturual sort can be implemented.
33 """
34 parts: List[Union[int, str]] = []
35 for part in re.split(r'(\d+)', s):
36 try:
37 parts.append(int(part))
38 except ValueError:
39 parts.append(part)
40
41 return tuple(parts)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

68def splitnum(self, value):
69 for elem in self.cards:
70 if elem > value:
71 continue
72
73 out = []
74 if value == 0:
75 div, mod = 1, 0
76 else:
77 div, mod = divmod(value, elem)
78
79 if div == 1:
80 out.append((self.cards[1], 1))
81 else:
82 if div == value: # The system tallies, eg Roman Numerals
83 return [(div * self.cards[elem], div*elem)]
84 out.append(self.splitnum(div))
85
86 out.append((self.cards[elem], elem))
87
88 if mod:
89 out.append(self.splitnum(mod))
90
91 return out
156def get_numbers(num):
157 card_and_port = str(num)
158 card_and_port = re.split('/|-',card_and_port)
159 card = card_and_port[0]
160 fromPort = card_and_port[1]
161 toPort = fromPort if len(card_and_port) <= 2 else card_and_port[2]
162 return card, fromPort, toPort
143def split_decimal(value):
144 import decimal
145 decimal.getcontext().prec = 2
146 d = decimal.Decimal(value)
147 i = int(d)
148 return (i, d - i)
143def _split_basic(string):
144 """
145 Split a string into a list of tuples of the form (key, modifier_fn,
146 explode) where modifier_fn is a function that applies the appropriate
147 modification to the variable.
148 """
149 tuples = []
150 for word in string.split(','):
151 # Attempt to split on colon
152 parts = word.split(':', 2)
153 key, modifier_fn, explode = parts[0], _identity, False
154 if len(parts) > 1:
155 modifier_fn = functools.partial(
156 _truncate, num_chars=int(parts[1]))
157 if word[len(word) - 1] == '*':
158 key = word[:len(word) - 1]
159 explode = True
160 tuples.append((key, modifier_fn, explode))
161 return tuples
83def test_splitNumber(self):
84 self.assertEqual(splitNumber("0222112222120000"),[0,2,2,2,1,1,2,2,2,2,1,2,0,0,0,0])
59def _split_float_to_numerator_and_denominator(float_data: float) -> tuple:
60 assert 1.0 > float_data >= 0
61 str_float: str = format(decimal.Decimal(str(float_data)), 'f')
62 str_decimal: str = str_float[str_float.find('.') + 1:]
63 numerator = int(str_decimal)
64 denominator = 10 ** len(str_decimal)
65 return numerator, denominator
12def extractnumbers(s):
13 return tuple(map(int,re.findall("(\d+)\.(\d+)\.(\d+)",str(s))[0]))
20def ParseNumber(number):
21 if number.startswith('0x'):
22 return int(number[2:], 16)
23 else:
24 return int(number)

Related snippets