473,395 Members | 1,863 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,395 software developers and data experts.

generate list of strings

6
Hi,

I'm trying to generate a list of random digits with no repeats. So far i have:

a = random.sample(range(0, 1), n)

which generates a list of random digits in a list with no repeat such as [1, 3, 5, 9] where n is 4.

However, i need it to produce a list of string of digits with no repeats, such as ['1', '3', '5', '9']. Is there any simple way to do this?

Thankyou for reading.
Aug 25 '08 #1
3 1702
realin
254 100+
i could get this result

Expand|Select|Wrap|Line Numbers
  1. '[10, 0, 5, 1]'
  2. outta this code 
  3. >>> str(random.sample(range(0,11),4))
  4.  
i know that may be different, but this is what i learnt today cause i just started with python :)

cheers !!
Aug 25 '08 #2
Laharl
849 Expert 512MB
If you want to make each element a string, the simplest way is to generate the list of digits the way you did and then use a list comprehension or loop to convert each to a string.

Expand|Select|Wrap|Line Numbers
  1. a = [str(elem) for elem in a] #list comprehension, I'm not going to type out the loop version, as it's almost identical
  2.  
Aug 25 '08 #3
Thekid
145 100+
This will print out 4 random numbers in the range(1,44). It's easy to adjust as well:

Expand|Select|Wrap|Line Numbers
  1. import random
  2. randlist=[]
  3. while (randlist.__len__() <= 4):
  4.   b =random.randint(1,44)
  5.   if not b in randlist:
  6.      randlist.append(b)
  7. print randlist
  8.  
Credit to kaarthikeyapreyan for that code: http://bytes.com/forum/thread827473.html

Or is it that you need the answer to print out with quotes around each number?
Aug 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Girish Sahani | last post by:
I have a list of strings all of length k. For every pair of k length strings which have k-1 characters in common, i want to generate a k+1 length string(the k-1 common characters + 2 not common...
11
by: Girish Sahani | last post by:
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= l.append(string) string1 = '' for i in...
8
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9...
8
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.