This highlights an issue that I'll let you fix (the code somehow runs properly because negative indices are accepted by Python but this is probably not what you intended nor what another programmer would expect by reading quickly the code). Once we have this function, we could take this change to check that idx-1 is in the valid range with assert 0 <= idx - 1 < leng. """Documentation is to be written here.""" Among the various other benefits: we would be able to give it a meaningful name, add some documentation, test it, etc.įor instance, we could have: def get_random_element(lst): In our case, we could implement a function performing this logic. It could be a good idea to avoid having duplicated code which can be harder to read and maintain (see DRY). The 3 first parts of the loop look highly similar. You'll find various tools to check if your code is compliant to it and even tools to reformat your code automatically ( black). There is a Python style guide called PEP 8 which is definitely worth reading. Anyway, various things can be improved in the code without changing the behavior. The simpler and less surprising thing to do is generate a word from any of those characters: import randomĬhoices = ascii_lowercase + ascii_uppercase + digitsĭisclaimer: I'm note 100% sure that your code does what you think it does. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. Word = ''.join(random.choices(choices, k=6))īut your algorithm has some odd properties that, according to your comments, you did not intend. Rather than your manual, unrolled string appending, just use ''.join().Ī strictly equivalent implementation could be import randomįrom string import ascii_lowercase, ascii_uppercase, digitsįill_caps = random.choice(ascii_uppercase)įill_letter = random.choice(ascii_lowercase)Ĭhoices = (fill_letter, fill_caps, fill_number) it shadows an existing type called list, and 2. Replace your length / index / slice with a random.choices.ĭon't call a variable list, since 1. If this is duplicate, please feel free to tell me, because searching for things isn't one of my qualities.ĭon't use caps, numbers and letters those are all constants available from the string module.ĭon't assign j since it isn't used name the iteration variable _ instead. Print(fill1list + fill2list + fill3list + fill4list + fill5list + fill6list) Indexletters=random.randint(0, lengthletters) Indexnumbers=random.randint(0, lengthnumbers) I decided to write this code just for fun, however I feel like the code can be made shorter. I was hoping to make it read each row within the Length column, and within each row take note of how many symbols to randomly select.My code basically picks random letters and numbers from three lists and makes a random combination with them. List1.remove(s) #to ensure random selection without replacement List1 = df #creating a new column for the generated stringįor row in df: #hope this reads each row in that columnįor n in range(1, row): #hope this reads the length value within cell Os.chdir("/Users/etcetc") #change directory This is the code I tried but I do not think it is right. This is an example of the result I want: Word Length String I would like to have the code such that it appends a third column to the existing csv file with the generated string equal in length for each word. I would like to randomly select a string of characters from this list of symbols without replacement: length of the generated string would be equal in length to the word in another column from the csv.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |