2

I am taking a class to learn Morse code. At the beginning of the class, we are given a list of words in .txt format. I take this list and convert it to a Morse code .mp3 file. (LCWO.net) Each word is played once, however, it would be better to play it multiple times so I can learn the pattern.

My goal is to take the original text file and duplicate each word a number of times based on user input. I have been typing this manually, but reasoned that a computer could do it much easier. So, I chose Python to try and create the program. So far, I have figured out how to open the .txt file, create a list, strip out the newline character after each word, then print the list to the screen.

How can I loop through this list and create a copy of each word based on the user input? For example, a user would enter a '3' for 3 copies of each word. To illustrate, if the word list is ['cat', 'dog', 'chicken'] , how do I create a list that is: ['cat', 'cat', 'cat', 'dog', 'dog', 'dog', 'chicken','chicken', 'chicken'], then write this list to a text file so each word is on one line?

filename is 'words.txt'

cat cat cat
dog dog dog
chicken chicken chicken

I think I can figure out how to get the user input and assign it to a variable, then use this variable as part of a loop for generating the new list. I also think I can figure out how to write the new list to a file. The part I need help with is figuring out how to create the list based on the input from the user (number of iterations for the word).

I realize that I'm asking for you to do the work, but I've read through the Python docs and I am struggling with a solution.

Thanks for your help!

Scott

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Sierra_Nov
  • 23
  • 3
  • No fear. Make an attempt. Do what you can. When you actually stick on the problem, post your code that you will achieve so far. – Askold Ilvento May 18 '20 at 19:05

5 Answers5

1

Supposing you have l = ['cat', 'dog'] and you want each of the elements repeated 3 times you can use itertools library to do it as following

import itertools

l = ['cat', 'dog']
duplicate = list(itertools.chain.from_iterable(itertools.repeat(x, 3) for x in l))

This way you keep the order of the elements

Iteeeh
  • 67
  • 6
  • I will give this a try. I don't understand the itertools function but will read up on it in the documentation. Thank you. – Sierra_Nov May 18 '20 at 20:12
1

Given:

>>> li
['cat', 'dog', 'chicken']

You can do:

>>> [e for s in li for e in [s,s,s]]
['cat', 'cat', 'cat', 'dog', 'dog', 'dog', 'chicken', 'chicken', 'chicken']

Or, since strings are interned and immutable, you won't get surprises if you also do:

>>> [e for s in li for e in [s]*3]
['cat', 'cat', 'cat', 'dog', 'dog', 'dog', 'chicken', 'chicken', 'chicken']

This is a modification of Alex Martelli's answer on how to flatten a list of lists. In this case, [s, s, s] or your string 3 times.

If you are reading from a file, you can do:

n=3
with open(ur_file) as f_in:
    flat_list=[word for line in f for word in line.split()]
    duped_words=[mult for word in flat_list for mult in [s]*n]
dawg
  • 98,345
  • 23
  • 131
  • 206
  • I read up a little bit on list comprehensions and this seems similar to what little I understand about them. This seems like a simple solution. I'll give it a try as well. Thank you for your help! – Sierra_Nov May 18 '20 at 20:14
0

You do not need any lib for it (even standard itertools). Just mulptiply your list to integer.

In[11]: x = ["cat", "dog", "chicken"]
In[12]: x * 3
Out[12]: ['cat', 'dog', 'chicken', 'cat', 'dog', 'chicken', 'cat', 'dog', 'chicken']
In[13]: sorted(x * 3)
Out[13]: ['cat', 'cat', 'cat', 'chicken', 'chicken', 'chicken', 'dog', 'dog', 'dog']  
ncopiy
  • 1,515
  • 14
  • 30
  • Let me see if I understand this. The first list In[11] is created from the original list 'x'. Then a newlist In[12] takes x (the original list) and multiplies the list by 3. So, now we have one list with three sets of words, each the same (appended). Now, sort the list so each word is the same three times, then move onto the next word in the list and do the same. Then output the list. Is that it? Thank you for your help! I – Sierra_Nov May 18 '20 at 20:22
0

One simple way is to put another loop inside your loop;

with open('words.txt') as f:
  for line in f.readlines():
    for x in range(3):
      print(word_to_morse(line))
silicontrip
  • 906
  • 7
  • 23
  • Does the 'for x in range(3):' multiply each word 3 times? Is there a way to substitute '3' with a variable x, where x=3 from the user input? Thanks for your help! – Sierra_Nov May 18 '20 at 20:16
  • No, the for x in range, executes the following block 3 times and that block outputs the word. Yes you can use a variable with a numeric value anywhere you see a number in the code. – silicontrip May 18 '20 at 20:35
0

This script should do the trick:

count = int(input('Input you number: '))
with open('text.txt','r') as f:
    l = [word for word in f.read().split('\n')]
with open('text2.txt','w') as f:
    for n in range(count):
        f.write(' '.join(l))
        f.write('\n')
Community
  • 1
  • 1
Red
  • 26,798
  • 7
  • 36
  • 58
  • This solution makes sense to me because I can understand the logic. Thank you very much for your help! – Sierra_Nov May 18 '20 at 20:17
  • I'm just glad I can help. – Red May 18 '20 at 20:22
  • I tried your solution, substituting 'testfile.txt' for 'text.txt'. However, I got an error that 'word' was not defined. Does with open() open the file and read it? Or do I need to open the file first? – Sierra_Nov May 18 '20 at 23:04