Python Forum
Generate a string of words for multiple lists of words in txt files in order. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generate a string of words for multiple lists of words in txt files in order. (/thread-34587.html)



Generate a string of words for multiple lists of words in txt files in order. - AnicraftPlayz - Aug-11-2021

Ok, so I want to make something that takes some text files with lists of words in them and outputs every combination of those words in order. For example:
If I had a txt file with the English dictionary, one with the names of the days and months, one with every number 1-99 and finally a list of symbols, I want the program to output a string of words in that order, ex: liveTuesday10@, wouldDecember7*, softNovember85#, etc etc. But it would only give words in that order (dictionary, day/month, number, symbol), not like 65Friday£hippo. Is it possible? If so please show me how.


RE: Generate a string of words for multiple lists of words in txt files in order. - Larz60+ - Aug-11-2021

what have you tried so far?


RE: Generate a string of words for multiple lists of words in txt files in order. - jamesaarr - Aug-11-2021

Hello mate,

Import the random library and assign a random number to a variable for each of the text files, read in the text files and then use the random number as an index point for each list.

It would be easier to do it from a csv using numpy - EG:
import numpy as np
list_of_words = np.genfromtxt('words.csv',delimiter = ",")
You can then use the random library to give a variable a random number:

import random
random_number = random.randint(1, 10)
And then you can use that to choose the item from the list:
print(list_of_words[random_number])
If you have a varied number of items on each list you could also do:
import numpy as np
import random
list_of_words = np.genfromtxt('words.csv',delimiter = ",")
x = int(len(list_of_words))
random_number = random.randint(1, x)
You could then make a random order generator by using random number generation and a series of if statements:
#random number made x
#random number made y
#random number made z

if x > y and x > z:
    if y > z:
        print (list[x] + list[y] + list[z])
    if z > y:
        print(list[x] + list[z] + list[y])
#and so on