Home > Software design >  How to replace list items with data from a file in python
How to replace list items with data from a file in python

Time:06-11

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 

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

# 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

CodePudding user response:

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()
  • Related