Sierra_Nov
Sierra_Nov

Reputation: 23

Create a list of duplicate elements from an existing list

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

Upvotes: 2

Views: 377

Answers (5)

Red
Red

Reputation: 27557

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')

Upvotes: 0

dawg
dawg

Reputation: 103774

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]

Upvotes: 1

silicontrip
silicontrip

Reputation: 1026

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))

Upvotes: 0

Iteeeh
Iteeeh

Reputation: 67

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

Upvotes: 1

ncopiy
ncopiy

Reputation: 1604

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']  

Upvotes: 0

Related Questions