0

I am trying to play around with a list. I want to replace every list item with data from a file. I would like to get an idea as to how to properly do it.

Currently, I'm using mylist = mylist[:0-1] and doing the individual replace is quite long. I would like to know of an alternative approach to do it.

mylist = ['exit', 'smoke', 'chef', 'toddler', 'dolphin']

mydata.txt #-- to be used to replace the list items

admin
staff
reach
train

Needed Output: #-- Printed to screen only

exit smoke chef toddler admin
exit smoke chef toddler staff
exit smoke chef toddler reach
exit smoke chef toddler train

exit smoke chef admin dolphin
exit smoke chef staff dolphin
exit smoke chef reach dolphin
exit smoke chef train dolphin

exit smoke admin toddler dolphin
exit smoke staff toddler dolphin
exit smoke reach toddler dolphin
exit smoke train toddler dolphin

exit admin chef toddler dolphin
exit staff chef toddler dolphin
exit reach chef toddler dolphin
exit train chef toddler dolphin

admin smoke chef toddler dolphin 
staff smoke chef toddler dolphin 
reach smoke chef toddler dolphin 
train smoke chef toddler dolphin 
2
  • do you want to create new list for each line your printed or you just want to print? Commented Jun 11, 2022 at 1:52
  • just a print will do Commented Jun 11, 2022 at 1:53

4 Answers 4

2

A pretty simple one (Try it online!):

mylist = ['exit', 'smoke', 'chef', 'toddler', 'dolphin']
mydata = ['admin', 'staff', 'reach', 'train']

for i in reversed(range(len(mylist))):
    copy = mylist.copy()
    for copy[i] in mydata:
        print(*copy)
    print()
Sign up to request clarification or add additional context in comments.

2 Comments

wanted to ask which is faster, using itertools or this one
@rbutrnz Probably this one. Although both could be optimized, and both might spend most of their time on the printing anyway. Besides, this is very little data, so it's very fast anyway. If you have much larger data and speed actually becomes an issue, that would be a different question.
1

Try this. itertools.combinations will help you get combinations.

mylist = ['exit', 'smoke', 'chef', 'toddler', 'dolphin']
mydata = ['admin', 'staff', 'reach', 'train']

from itertools import combinations
size = len(mylist)-1
# start enumerate from the back of mylist
for i, x in enumerate(combinations(mylist, size), -size):
    for y in mydata:
        # replace element in position -i by y
        z = [*x[:-i], y, *x[-i:]]
        print(*z)
    print()

    
#exit smoke chef toddler admin
#exit smoke chef toddler staff
#exit smoke chef toddler reach
#exit smoke chef toddler train

#exit smoke chef admin dolphin
#exit smoke chef staff dolphin
#exit smoke chef reach dolphin
#exit smoke chef train dolphin

#exit smoke admin toddler dolphin
#exit smoke staff toddler dolphin
#exit smoke reach toddler dolphin
#exit smoke train toddler dolphin

#exit admin chef toddler dolphin
#exit staff chef toddler dolphin
#exit reach chef toddler dolphin
#exit train chef toddler dolphin

#admin smoke chef toddler dolphin
#staff smoke chef toddler dolphin
#reach smoke chef toddler dolphin
#train smoke chef toddler dolphin

Comments

1

Let me know if I misunderstood your question :)

my_list = ["exit", "smoke", "chef", "toddler", "dolphin"]
replacement_list = ["admin", "staff", "reach", "train"]
n = len(my_list)

for i in range(n-1, -1, -1):
    for replacement in replacement_list:
        for k in range(n):
            if i != k:
                print(my_list[k], end=' ')
            else:
                print(replacement, end=' ')
        print()
    print()

Comments

0
# set or get words array here
# read your file to lines and set to filewords here (will probably need to trim \n)
ridx = len(words) - 1
sets = []
while ridx >= 0:
    for fw in filewords:
        tmp = []
        for w in words:
            if len(tmp) == ridx: tmp.append(fw)
            else: tmp.append(w)
        print(' '.join(tmp)) # if you want to print
        sets.append(tmp) # if you want to keep arrays
    print('') # seperate sets like your example
    ridx -= 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.