簡體   English   中英

如何用python中的文件中的數據替換列表項

[英]How to replace list items with data from a file in python

我正在嘗試使用列表。 我想用文件中的數據替換每個列表項。 我想知道如何正確地做到這一點。

目前,我正在使用mylist = mylist[:0-1]並且進行單獨替換的時間很長。 我想知道另一種方法來做到這一點。

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

mydata.txt #--用於替換列表項

admin
staff
reach
train

需要的輸出:#-- 僅打印到屏幕上

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 

一個非常簡單的( 在線嘗試! ):

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

嘗試這個。 itertools.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

如果我誤解了你的問題,請告訴我:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM