473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Number combinations

Hi all,

Just wondering if there is a better way of generating a 4 digit number
(that gets converted to a string), ive got the following code which
generates strings between 0000-9999.

<code>

for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
print "%s%s%s%s" %(str(a), str(b), str(c),str(d)

</code>
--Cheers

http://bulkan.googlepages.com/python

Jul 19 '06 #1
5 2389
Just wondering if there is a better way of generating a 4 digit number
(that gets converted to a string), ive got the following code which
generates strings between 0000-9999.

<code>

for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
print "%s%s%s%s" %(str(a), str(b), str(c),str(d)

</code>
Is there something wrong with

for i in xrange(0,10000): print "%04i" % i

If you need the individual digits for something, you can use

for i in xrange(0,10000):
d1,d2,d3,d4 = list("%04i" % i)
# do something with d1,d2,d3,d4

somewhat indelicate, but it works for me. :)

-tkc


Jul 19 '06 #2
How about

print ["%04d" % x for x in xrange(10000)]

Jul 19 '06 #3
placid wrote:
Hi all,

Just wondering if there is a better way of generating a 4 digit number
(that gets converted to a string), ive got the following code which
generates strings between 0000-9999.

<code>

for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
You could reuse the same range...
print "%s%s%s%s" %(str(a), str(b), str(c),str(d)
And there's no need to convert to string here.

def parrot():
r = range(10)
return ["%s%s%s%s" % (a, b, c, d) \
for a in r \
for b in r \
for c in r \
for d in r]

But there's certainly better solutions...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #4
placid <Bu****@gmail.comwrote:
Hi all,

Just wondering if there is a better way of generating a 4 digit number
(that gets converted to a string), ive got the following code which
generates strings between 0000-9999.

<code>

for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
print "%s%s%s%s" %(str(a), str(b), str(c),str(d)
for n in xrange(10000):
print "%4.4d" % n
Alex
Jul 19 '06 #5
Tim Chase wrote:
....
If you need the individual digits for something, you can use

for i in xrange(0,10000):
d1,d2,d3,d4 = list("%04i" % i)
strings are sequences too, and you need only write

d1,d2,d3,d4 = "%04i" % i
Jul 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
20
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
2
by: GrantMagic | last post by:
I have found that some strange combinations of characters in a URL can cause an error in my ASP.NET application. This is regarding URL Paramters For example: if i have the URL:...
4
by: Suzie1986 | last post by:
Hiya, I am a newcomer to programming and really stuck!!! Any help would be gratefully received!! I have written a program that gives me all possible combinations for 1 and 2 for a length of...
22
by: MLH | last post by:
If 3 things can be in one of 2 states, the number of possible combinations is equal to 2^3. But if I have 3 things, 2 of which can be in 2 states and the other in 3 states, what's the simplest...
0
by: John Doe77 | last post by:
Hi, Given a phone number I need to print out all the word representation combinations possible from that phone number. Digits translate into chars like the following: 1 = 1 2 = A B C 3 = D E...
5
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all...
1
by: sotirios | last post by:
I have a Table (Table01)in Access with one number field name Num (double) I want a routine to create a new table example Table02 with 2 fields the first with combinations of the numbers of Table01...
6
by: nullgraph | last post by:
Hi everyone, I'm new to Python and the notion of lambda, and I'm trying to write a function that would have a varying number of nested for loops depending on parameter n. This just smells like a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.