A cleaner way to generate a list of running IDs - python

I just made a function to generate a list of running ids between a given range. IDs begin with an alphabet and follow with 5 numbers (e.g. A00002). The function below works, but I was wondering if there was a cleaner way to do this. Thanks!
def running_ids(start, end):
list = []
start = int(start[1:])
end = int(end[1:])
steps = end - start
def zeros(n):
zeros = 5 - len(str(n))
return zeros
while start <= end:
string = "A" + "0"*zeros(start) + str(start)
list.append(string)
start += 1
return list
print running_ids('A00001', 'A00005')
['A00001', 'A00002', 'A00003', 'A00004', 'A00005']

Use a generator. This way you can generate the numbers as needed and not store them all at once. It also maintains the state of your counter, useful if you start building large projects and you forget to add one to your index. It's a very powerful way of programming in Python:
def running_id():
n = 1
while True:
yield 'A{0:05d}'.format(n)
n += 1
C = running_id()
for n in xrange(5):
print next(C)
Giving:
A00001
A00002
A00003
A00004
A00005

You could just use simple builtin string formatting:
>>> 'A%05d'%1
'A00001'
>>> 'A{0:05d}'.format(1)
'A00001'

You can use the builtin format method
print "A" + format(1, "05d") # A00001
print "A" + format(100, "05d") # A00100
Or you can use str.zfill method like this
print "A" + str(1).zfill(5) # A00001
print "A" + str(100).zfill(5) # A00100

def running_ids(start, end):
t = start[0]
low = int(start[1:])
high = int(end[1:]) + 1
res = []
for x in range(low, high):
res.append(t + '{0:05d}'.format(x))
return res
print(running_ids('A00001', 'A00005'))

Related

Separating thousands

I have been coding for around half a year in uni and have done some side projects. This is one of them and although my code works for integers, I would like to know how it could be optimised using less lines of code. Coding at uni has taught me how to create many programs but not really how to optimise code and so any further tips would be greatly appreciated! <3
num = int(1230124013502)
def rem(num):
"""
Rem function separates the thousands in an intiger and converts to
a string. Function takes one input (num) which must be of intiger
form. Rem converts to string with commas separating the thousands
"""
num = str(num)
l = len(num)
remain = l%3
sum = ''
if remain == 0:
remain = 3
new = num[remain:]
pre = num[:remain]
#print(pre,new,remain)
l_new = len(new)
zeros = []
for i in range(3,l_new+3,3):
j = i - 3
post = new[j:i] + ','
zeros.append(post)
for i in range(len(zeros)):
sum += zeros[i]
tot = pre + ',' + sum
endpoint = len(tot) - 1
tot = tot[0:endpoint]
if l < 4:
print(num)
return num
else:
print(tot)
return tot
rem(num)

How to increment and decrement with a string Python?

Hope you all are doing well in these times.
here's my code:
def ab(n):
first = 0
last = -1
endprod = n[first] + n[last]
endprod2 = n[first+1] + n[last-1]
endprod3 = n[first+2] + n[last-2]
endprod4 = n[first+3] + n[last-3]
endprod5 = n[first+4] + n[last-4]
endprod100 = endprod[::-1] + endprod2[::-1] + endprod3[::-1]+ endprod4[::-1]+ endprod5[::-1]
return endprod100
I was able do to it, however mine isn't a loop. Is there a way to convert my code into a for loop. So, increment by 1 and decrement by 1.
Thanks,
Try this:
def ab(n):
result = ''
for j in range(len(n) // 2):
result += n[-j-1] + n[j]
if len(n) % 2 == 1:
result += n[len(n) // 2]
return result
You also need the part
if len(n) % 2 == 1:
result += n[len(n) // 2]
because your input string might have an odd number of characters
Examples:
>>> ab('0123456789')
'9081726354'
>>> ab('01234956789')
'90817263549'
If you want to reuse your original logic:
def ab(n):
result = ''
first = 0
last = -1
for j in range(len(n) // 2):
result += n[last-j] + n[first+j]
if len(n) % 2 == 1:
result += n[len(n) // 2]
return result
You could also recurse it:
def ab(s):
if len(s)>2:
return s[-1]+s[0]+ab(s[1:-1])
else:
return s
But the last part of Riccardo's answer fits your question more closely.
you need to split your string for your loop, means first you broke your string to half then build your string, you could use zip to iterate over multiple iteratable. something like this:
def ab(s):
out = ""
for v0,v1 in zip(s[:len(s)//2 -1 :-1], s[:len(s)//2 ]):
out += v0 + v1
return out
the better version you should write without loop.
like this:
out = "".join(map(lambda x: "".join(x), zip(s[:len(s)//2 -1 :-1], s[:len(s)//2 ])))
There are already a lot of good answers that are probably clearer, easier to read and much better suited for learning purposes than what I'm about to write. BUT... something I wanted to bring up (maybe just telling myself, because I tend to forget it) is that sometimes destroying the original argument can facilitate this sort of things.
In this case, you could keep "poping" left and right, shrinking your string until you exhaust it
def swap_destr(my_str):
result = ""
while len(my_str) > 1:
result += my_str[-1] # Last char
result += my_str[0] # First char
my_str = my_str[1:-1] # Shrink it!
return result + my_str # + my_str just in case there 1 char left
print(swap_destr("0123456789"))
print(swap_destr("123456789"))
# Outputs:
# 9081726354
# 918273645
This is also a good problem to see (and play with) recursion:
def swap_recur(my_str):
if len(my_str) <= 1:
return my_str
return my_str[-1] + my_str[0] + swap_recur(my_str[1:-1])
print(swap_recur("0123456789"))
print(swap_recur("123456789"))
# Outputs:
# 9081726354
# 918273645

Reverse digits in a number

I want to reverse digits in a number in python. Here are my two implementations.
One: convert the number into string and reverse each char in it
number = 2376674032
number_s = str(number)
index = len(number_s) - 1
str_list = []
while index > -1:
str_list.append(number_s[index])
index -= 1
result = int("".join(str_list))
print(result)
Two: using simple mathematics
number = 2376674032
N = 0
K = number
R = number % 10
while K > 0:
N = N*10 + R
K = K // 10
R = K % 10
result = N
print(result)
As I'm pretty new to python programming, so could someone help me with the following questions:
with the first approach, will "".join(str_list) produce a new string with each list element? if so is a better way to concatenate strings in python(something similar to StringBuffer in java)
which of the implementations is better from performance perspective?
You can reverse a string using -1 as the step in a slice. So this works:
number = 2376674032
number_s = str(number)
reverse_s = number_s[::-1]
reversed = int(reverse_s)
you want to reverse a number …..input it as string format , and do this:
number="8374783246837"
revnumber=number[::-1]
Done
a = 1234
a = int("".join(reversed(str(a))))
This will give a = 4321
reversed functions returns an iterable object.
If we do :
a = list(reversed(str(a)))
it will return [“3”,”2″,”1″]. We have then joined it and converted into int.
To make the number an integer type, we have to use the int function, as below:
numbers=str(123456)
#or numbers="123456"
print((int(numbers[::-1])))
print((type(int(numbers[::-1]))))
output:
654321
<class 'int'>
We can do this in a single line as well using [::-1]
print(int(str(int(input()))[::-1]))
#here is my answer . you can do it using simple recursion
# count digits recursively
def reverse_digits(n):
# base case
if n == 0:
pass
#recursive case
else:
print(n%10,end='')
return reverse_digits(n//10)
# reverse 123
reverse_digits(123)
````````````````````````````````````````````````````

Add a start index to a string index generator

I'm currently learning to create generators and to use itertools. So I decided to make a string index generator, but I'd like to add some parameters such as a "start index" allowing to define where to start generating the indexes.
I came up with this ugly solution which can be very long and not efficient with large indexes:
import itertools
import string
class StringIndex(object):
'''
Generator that create string indexes in form:
A, B, C ... Z, AA, AB, AC ... ZZ, AAA, AAB, etc.
Arguments:
- startIndex = string; default = ''; start increment for the generator.
- mode = 'lower' or 'upper'; default = 'upper'; is the output index in
lower or upper case.
'''
def __init__(self, startIndex = '', mode = 'upper'):
if mode == 'lower':
self.letters = string.ascii_lowercase
elif mode == 'upper':
self.letters = string.ascii_uppercase
else:
cmds.error ('Wrong output mode, expected "lower" or "upper", ' +
'got {}'.format(mode))
if startIndex != '':
if not all(i in self.letters for i in startIndex):
cmds.error ('Illegal characters in start index; allowed ' +
'characters are: {}'.format(self.letters))
self.startIndex = startIndex
def getIndex(self):
'''
Returns:
- string; current string index
'''
startIndexOk = False
x = 1
while True:
strIdMaker = itertools.product(self.letters, repeat = x)
for stringList in strIdMaker:
index = ''.join([s for s in stringList])
# Here is the part to simpify
if self.startIndex:
if index == self.startIndex:
startIndexOk = True
if not startIndexOk:
continue
###
yield index
x += 1
Any advice or improvement is welcome. Thank you!
EDIT:
The start index must be a string!
You would have to do the arithmetic (in base 26) yourself to avoid looping over itertools.product. But you can at least set x=len(self.startIndex) or 1!
Old (incorrect) answer
If you would do it without itertools (assuming you start with a single letter), you could do the following:
letters = 'abcdefghijklmnopqrstuvwxyz'
def getIndex(start, case):
lets = list(letters.lower()) if case == 'lower' else list(letters.upper())
# default is 'upper', but can also be an elif
for r in xrange(0,10):
for l in lets[start:]:
if l.lower() == 'z':
start = 0
yield ''.join(lets[:r])+l
I run until max 10 rows of letters are created, but you could ofcourse use an infinite while loop such that it can be called forever.
Correct answer
I found the solution in a different way: I used a base 26 number translator (based on (and fixxed since it didn't work perfectly): http://quora.com/How-do-I-write-a-program-in-Python-that-can-convert-an-integer-from-one-base-to-another)
I uses itertools.count() to count and just loops over all the possibilities.
The code:
import time
from itertools import count
def toAlph(x, letters):
div = 26
r = '' if x > 0 else letters[0]
while x > 0:
r = letters[x % div] + r
if (x // div == 1) and (x % div == 0):
r = letters[0] + r
break
else:
x //= div
return r
def getIndex(start, case='upper'):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
letters = alphabet.upper() if case == 'upper' else alphabet
started = False
for num in count(0,1):
l = toAlph(num, letters)
if l == start:
started = True
if started:
yield l
iterator = getIndex('AA')
for i in iterator:
print(i)
time.sleep(0.1)

Python - Split a string into list after a certain number of special characters

I have a python program which does a SOAP request to a server, and it works fine:
I get the answer from the server, parse it, clean it, and when I am done, I end up with a string like that:
name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|...
Basically, it is a string with values delimited by "|". I also know the structure of the database I am requesting, so I know that it has 6 columns and various rows. I basically need to split the string after every 6th "|" character, to obtain something like:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|...
Can you tell me how to do that in Python? Thank you!
Here's a functional-style solution.
s = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
for row in map('|'.join, zip(*[iter(s.split('|'))] * 6)):
print(row + '|')
output
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|
For info on how zip(*[iter(seq)] * rowsize) works, please see the links at Splitting a list into even chunks.
data = "name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|"
splits = data.split('|')
splits = list(filter(None, splits)) # Filter empty strings
row_len = 6
rows = ['|'.join(splits[i:i + row_len]) + '|' for i in range(0, len(splits), row_len)]
print(rows)
>>> ['name|value|value_name|default|seq|last_modify|', 'record_type|1|Detail|0|0|20150807115904|', 'zero_out|0|No|0|0|20150807115911|', 'out_ind|1|Partially ZeroOut|0|0|20150807115911|']
How about this:
a = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
b = a.split('|')
c = [b[6*i:6*(i+1)] for i in range(len(b)//6)] # this is a very workable form of data storage
print('\n'.join('|'.join(i) for i in c)) # produces your desired output
# prints:
# name|value|value_name|default|seq|last_modify
# record_type|1|Detail|0|0|20150807115904
# zero_out|0|No|0|0|20150807115911
# out_ind|1|Partially ZeroOut|0|0|20150807115911
Here is a flexible generator approach:
def splitOnNth(s,d,n, keep = False):
i = s.find(d)
j = 1
while True:
while i > 0 and j%n != 0:
i = s.find(d,i+1)
j += 1
if i < 0:
yield s
return #end generator
else:
yield s[:i+1] if keep else s[:i]
s = s[i+1:]
i = s.find(d)
j = 1
#test runs, showing `keep` in action:
test = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
for s in splitOnNth(test,'|',6,True): print(s)
print('')
for s in splitOnNth(test,'|',6): print(s)
Output:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|
name|value|value_name|default|seq|last_modify
record_type|1|Detail|0|0|20150807115904
zero_out|0|No|0|0|20150807115911
out_ind|1|Partially ZeroOut|0|0|20150807115911
There are really many ways to do it. Even with a loop:
a = 'name|value|value_name|default|seq|last_modify|record_type|1|Detail|0|0|20150807115904' \
'|zero_out|0|No|0|0|20150807115911|out_ind|1|Partially ZeroOut|0|0|20150807115911|'
new_a = []
ind_start, ind_end = 0, 0
for i in range(a.count('|')// 6):
for i in range(6):
ind_end = a.index('|', ind_end+1)
print(a[ind_start:ind_end + 1])
new_a.append(a[ind_start:ind_end+1])
ind_start = ind_end+1
The print is just to saw the results, you remove it:
name|value|value_name|default|seq|last_modify|
record_type|1|Detail|0|0|20150807115904|
zero_out|0|No|0|0|20150807115911|
out_ind|1|Partially ZeroOut|0|0|20150807115911|

Categories

Resources