generating and mapping two sets of numbers
I have 2 numbers 25798709 and 25716544 representing the minimum and maximum values of some consecutive numbers with each number differing from the preceeding number by one eg. 25798709,25798710, 25798711................25716544 I want a python code that will convert these figures to 1,2,3,4,5,.............. and map the 1,开发者_如何学Python2,3,4,5 to the original figures in two columns: Like:
1 25798709
2 25798710
3 25798711
Oneliner:
print(*['{0}\t{1}'.format(*i) for i in enumerate(range(MIN,MAX+1), 1)], sep='\n')
For python2.6 or 2.7 just add from __future__ import print_function
Here you are:
mapping = dict(enumerate(range(25716544, 25798709)))
for i in iter(mapping):
print i, '->', mapping[i]
I should warn you that this is zero-based. So 0 -> 25716544, 1 -> 25716545, etc.
# make it work in both Python 2 and 3
from __future__ import print_function
try: xrange
except NameError: xrange= range
def my_enumerate(num1, num2):
start= min(num1, num2)
end= max(num1, num2) + 1
for data in enumerate(xrange(start, end), 1):
print("%d\t%d" % data)
This would work and start from 1:
for idx, num in enumerate(range(25716544, 25798709), 1):
print idx, ' ', num
To start from 0:
for idx, num in enumerate(range(25716544, 25798709)):
print idx, ' ', num
精彩评论