May-18-2022, 05:56 PM
like this:
def numunits(s=None): if not isinstance(s,str): raise TypeError('string expected') i = iter(range(len(s))) for p in i: try: v = float(s[:p]) break except: continue else: raise ValueError('no number') for p in i: try: v = float(s[:p]) continue except: break else: raise ValueError('no units') if s[p] != ' ': p -= 1 return v,s[p:] if __name__ == '__main__': a = ['144mHz','432 mHz','1.296GHz','2.304 GHz'] for x in a: print(repr(x)) print(repr(numunits(x)))
Output:lt2a/forums/1 /home/forums 13> py numunits.py
'144mHz'
(144.0, 'mHz')
'432 mHz'
(432.0, 'mHz')
'1.296GHz'
(1.296, 'GHz')
'2.304 GHz'
(2.304, 'GHz')
lt2a/forums/1 /home/forums 14>
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.