Joining results in Python
Question:
Im new to Python, like around an hour and a half into it new.. ive crawled my website using cewl to get a bespoke wordlist for password audits, i also want to combine randomly 3 of these words together.
IE Cewl wordlist ;
word1
word2
word3
word4
using a python script i want to further create another wordlist randomly joining 3 words together IE
word4word2word1
word1word3word4
word3word4word2
so far all ive come up with is;
import random
print(random.choice(open("test.txt").read().split()))
print (random.choice(open("test.txt").read().split()))
print(random.choice(open("test.txt").read().split()))
Whilst this is clearly wrong, it will give me 3 random words from my list i just want to join them without delimiter, any help for a complete novice would be massively appreciated
Answers:
Using your code/style:
import random
wordlist = open("test.txt").read().split()
randomword = ''.join([random.choice(wordlist), random.choice(wordlist), random.choice(wordlist)])
print(randomword)
join
is a method of the string type and it will join the elements of a list using the string as a delimiter. In this case we use an empty string ''
and join a list made up of random choices from your test.txt
file.
First thing to do is only read the words once and using a context manager so the file gets closed properly.
with open("test.txt") as f:
lines = f.readlines()
Then use random.sample
to pick three words.
words = random.sample(lines, 3)
Of course, you probably want to strip newlines and other extraneous whitespace for each word.
words = random.sample([x.strip() for x in lines], 3)
Now you just need to join those together.
Here’s an example of how you can use it to combine three words together:
import random
with open("test.txt") as f:
words = f.read().split()
combined_words = [random.sample(words, 3) for _ in range(len(words)//3)]
for cw in combined_words:
print("".join(cw))
This code is reading in the contents of a text file called "test.txt" and storing it in the words
variable. It’s using the split()
function to split the contents of the file into a list of individual words.
Next, it’s using list comprehension to create a new list of combined words. The random.sample()
function is being used within the list comprehension to randomly select 3 words from the words
list, join them together as a single string, and assign them as one element of the combined_words
list.
Finally, it’s using a for loop to iterate through the combined_words
list and print out each word in the list.
Im new to Python, like around an hour and a half into it new.. ive crawled my website using cewl to get a bespoke wordlist for password audits, i also want to combine randomly 3 of these words together.
IE Cewl wordlist ;
word1
word2
word3
word4
using a python script i want to further create another wordlist randomly joining 3 words together IE
word4word2word1
word1word3word4
word3word4word2
so far all ive come up with is;
import random
print(random.choice(open("test.txt").read().split()))
print (random.choice(open("test.txt").read().split()))
print(random.choice(open("test.txt").read().split()))
Whilst this is clearly wrong, it will give me 3 random words from my list i just want to join them without delimiter, any help for a complete novice would be massively appreciated
Using your code/style:
import random
wordlist = open("test.txt").read().split()
randomword = ''.join([random.choice(wordlist), random.choice(wordlist), random.choice(wordlist)])
print(randomword)
join
is a method of the string type and it will join the elements of a list using the string as a delimiter. In this case we use an empty string ''
and join a list made up of random choices from your test.txt
file.
First thing to do is only read the words once and using a context manager so the file gets closed properly.
with open("test.txt") as f:
lines = f.readlines()
Then use random.sample
to pick three words.
words = random.sample(lines, 3)
Of course, you probably want to strip newlines and other extraneous whitespace for each word.
words = random.sample([x.strip() for x in lines], 3)
Now you just need to join those together.
Here’s an example of how you can use it to combine three words together:
import random
with open("test.txt") as f:
words = f.read().split()
combined_words = [random.sample(words, 3) for _ in range(len(words)//3)]
for cw in combined_words:
print("".join(cw))
This code is reading in the contents of a text file called "test.txt" and storing it in the words
variable. It’s using the split()
function to split the contents of the file into a list of individual words.
Next, it’s using list comprehension to create a new list of combined words. The random.sample()
function is being used within the list comprehension to randomly select 3 words from the words
list, join them together as a single string, and assign them as one element of the combined_words
list.
Finally, it’s using a for loop to iterate through the combined_words
list and print out each word in the list.