Neculai Fântânaru

Everything Depends On The Leader

Python Shuffle Method (): How To Mix Words Randomly

On May 05, 2021
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/QR0Wky4j

Install Python. Then install the following two libraries using the Command Prompt (cmd) interpreter in Windows10: Usually these libraries are already included in Python.

py -m pip install re
py -m pip install random

The "Python Random" code will automatically mix the words in the sentences you want, using the shuffle () method.

Option 1 - Simple Version

import random
import re # import for regular expressions

listOfStrings = ["Ana are mere", "Ana are pere", "Eu am mure"]
for s in listOfStrings: # for each s in the list I go through, I do something, and that something is in the body of the forum
    print(re.split(r' ', s)) # what is obtained after dividing a string by " " (space)
    result = re.split(r' ', s) # we save the result in a variable (the variable is a memory area where something is stored)
    random.shuffle(result) #
    print(result)

Option 2 - Saved Version

# extract the words directly from all the sentences with the help of a regular expression
mix = r"[a-zA-Z]+"
listOfWords = re.findall(mix, "To provide you with the best experiences, we and our partners use technologies. Such as cookies to store and / or access information. It is all about the device used.")
print("Result: ", listOfWords)

random.Random(4).shuffle(listOfWords)
print("Shuffled stuff: ", listOfWords)


# The first method to combine values from a list
finalString = "" # we initialize the variable in which the final text will be put with the string empty => does not contain anything
for cuvant in listOfWords: # FOR repeats what is in his body (what is in the bottom line)
    finalString = finalString + cuvant + " "
finalString = finalString.strip()
print(finalString)

#SAve

with open("shuffle-words.txt", "w") as some_file_handle:
    some_file_handle.write(finalString)

Option 3 - Saved Version (+ input pop-up window)

import re
import random

def shuffle_Bebe(text):
    regex = r"[a-zA-Z]+"
    listOfWords = re.findall(regex, text)
    random.shuffle(listOfWords)
    return listOfWords

def concatenate_Bebe(listOfWords): # concatenates a list of words and the returned result is a text
    finalString = "" # we initialize the variable in which the final text will be put with the string empty => does not contain anything
    for cuvant in listOfWords: # FOR repeats what is in his body (what is in the bottom line)
        finalString = finalString + cuvant + " "
    finalString = finalString.strip()
    return finalString

def save_text(cale_fisier, text):
    with open(cale_fisier, "w", encoding = 'utf-8') as file:
        file.write(text)

if __name__ == "__main__":

    print("Enter a word: ")
    text = str(input()) # you see the text box that is started at the input () command, and what you type in the box will be converted to a string with the str () command
    shuffled_list = shuffle_Bebe(text) # save the list of words that were shuffled
    print("Lista de cuvinte amestecate este: ", shuffled_list)
    text_final = concatenate_Bebe(shuffled_list)
    print("Textul obtinut din lista de mai sus este: ", text_final)

    save_text("c:\\Folder2\\test_final.txt", text_final)

Option 3 - Saved Version (+ input pop-up window) + Open File and Save File

import re
import random

def shuffle(text): 
    regex = r"[a-zA-Z]+" 
    listOfWords = re.findall(regex, text) 
    random.shuffle(listOfWords) 
    return listOfWords 

def concatenate(listOfWords): 
    finalString = "" 
    for cuvant in listOfWords: 
        finalString = finalString + cuvant + " "
    finalString = finalString.strip() 
    return finalString 

def read_text_from_file(cale_fisier):
    f = open(cale_fisier, "r") 
    text = f.read() 
    print("ce am citit: ", text)
    return text 

def save_text_into_file(cale_fisier, text):
    f = open(cale_fisier, "w", encoding = 'utf-8') 
    print("Ce am scris: ", text)
    f.write(text) 

text = read_text_from_file("test_final.txt")

lista_cuvinte_amestecate = shuffle(text)

text_final = concatenate(lista_cuvinte_amestecate)

save_text_into_file("test_final.txt", text_final)

You can easily do the same word mix with PowerShell:< ! -- HTML generated using hilite.me -->

 ((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" | 
     Sort-Object {Get-Random} ) -join ' ' |
         Out-File -FilePath C:\Folder1\NewFile.txt

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.