User:Mxrwho/Lists: Difference between revisions
(Created page with "== Playing with lists, sorting and randomization in python == === Documentation === https://pad.xpub.nl/p/15424_lists -> https://pad.xpub.nl/p/15424_lists_m export as plain text Python code: import random def sort_and_format_text(text): words = text.split() words = [word.strip('*') for word in words] sorted_words = sorted(words) formatted_lines = [] current_line = [] for word in sorted_words: current_line.append(word) if le...") |
No edit summary |
||
Line 11: | Line 11: | ||
Python code: | Python code: | ||
import random | import random | ||
def sort_and_format_text(text): | def sort_and_format_text(text): | ||
words = text.split() | |||
words = [word.strip('*') for word in words] | |||
sorted_words = sorted(words) | |||
formatted_lines = [] | |||
current_line = [] | |||
for word in sorted_words: | |||
current_line.append(word) | |||
if len(current_line) >= 3 and len(current_line) <= 5: | |||
formatted_lines.append(' '.join(current_line)) | |||
current_line = [] | |||
if current_line: | |||
formatted_lines.append(' '.join(current_line)) | |||
return '\n'.join(formatted_lines) | |||
def randomize_and_format_text(text): | def randomize_and_format_text(text): | ||
words = text.split() | |||
words = [word.strip('*') for word in words] | |||
random.shuffle(words) | |||
formatted_lines = [] | |||
current_line = [] | |||
for word in words: | |||
current_line.append(word) | |||
if len(current_line) >= 3 and len(current_line) <= 5: | |||
formatted_lines.append(' '.join(current_line)) | |||
current_line = [] | |||
if current_line: | |||
formatted_lines.append(' '.join(current_line)) | |||
return '\n'.join(formatted_lines) | |||
with open('lists.txt', 'r') as file: | with open('lists.txt', 'r') as file: | ||
input_text = file.read() | |||
print("Original text:", input_text) | print("Original text:", input_text) | ||
sorted_text = sort_and_format_text(input_text) | sorted_text = sort_and_format_text(input_text) | ||
print("Sorted and formatted text (3 to 5 words per line):") | print("Sorted and formatted text (3 to 5 words per line):") | ||
print(sorted_text) | print(sorted_text) | ||
randomized_text = randomize_and_format_text(input_text) | randomized_text = randomize_and_format_text(input_text) | ||
print("Randomized and formatted text (3 to 5 words per line):") | print("Randomized and formatted text (3 to 5 words per line):") | ||
print(randomized_text) | print(randomized_text) | ||
Combined texts and then: | Combined texts and then: |
Revision as of 13:28, 19 April 2024
Playing with lists, sorting and randomization in python
Documentation
https://pad.xpub.nl/p/15424_lists -> https://pad.xpub.nl/p/15424_lists_m
export as plain text
Python code:
import random
def sort_and_format_text(text): words = text.split() words = [word.strip('*') for word in words] sorted_words = sorted(words) formatted_lines = [] current_line = [] for word in sorted_words: current_line.append(word) if len(current_line) >= 3 and len(current_line) <= 5: formatted_lines.append(' '.join(current_line)) current_line = [] if current_line: formatted_lines.append(' '.join(current_line)) return '\n'.join(formatted_lines)
def randomize_and_format_text(text): words = text.split() words = [word.strip('*') for word in words] random.shuffle(words) formatted_lines = [] current_line = [] for word in words: current_line.append(word) if len(current_line) >= 3 and len(current_line) <= 5: formatted_lines.append(' '.join(current_line)) current_line = [] if current_line: formatted_lines.append(' '.join(current_line)) return '\n'.join(formatted_lines)
with open('lists.txt', 'r') as file: input_text = file.read()
print("Original text:", input_text)
sorted_text = sort_and_format_text(input_text) print("Sorted and formatted text (3 to 5 words per line):") print(sorted_text)
randomized_text = randomize_and_format_text(input_text) print("Randomized and formatted text (3 to 5 words per line):") print(randomized_text)
Combined texts and then:
say -o alllists.aiff -v Whisper -r 130 -f alllists.txt
and then:
ffmpeg -i alllists.aiff -codec:a libmp3lame -qscale:a 2 alllists.mp3