簡體   English   中英

用另一個列表中的字符串替換列表元素

[英]Replace element of list with string from another list

所以我寫了這個,但它沒有完成我想做的事情。 基本上,我想用 content_list 列表中該索引處的任何單詞替換第二個索引中的數字。

content_list= ['abstract', 'bow', 'button', 'chiffon', 'collar', 'cotton', 'crepe', 'crochet', 'crop', 'embroidered', 'floral', 'floralprint', 'knit', 'lace', 'longsleeve', 'peasant', 'pink', 'pintuck', 'plaid', 'pleated', 'polkadot', 'printed', 'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless', 'split', 'striped', 'summer', 'trim', 'tunic', 'v-neck', 'woven', '']

max=[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]

for l in max:
  e=l[1]
  f=e.split(",")
  for s in f:
    intt=int(s)
    rep=content_list[intt]
    #print(rep)
    e.replace(s,rep)
    #print(z)

print(max)

這是我得到的輸出:

[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'], ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]

但這就是我想要的:

[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', 'sheer'], ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', 'pleated,peasant,sheer']]

首先, max 是一個內置函數,我強烈建議您檢查如何為將來的變量命名,它可能會給您帶來一些大問題:)。 你也可以用蠻力方法離開這里,就像這樣:

arr = [
    ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],
    ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24'],
]

for inner in arr:
    indexes=inner[1]
    inner[1] = ""
    for number in indexes.split(","):
        inner[1] += content_list[int(number)]
    print(inner)

我會做這樣的事情,我認為必須是更好的選擇,但它有效......所以總比沒有好

content_list = ['abstract', 'bow', 'button', 'chiffon', 
               'collar', 'cotton', 'crepe', 'crochet', 
               'crop', 'embroidered', 'floral', 'floralprint', 
               'knit', 'lace', 'longsleeve', 'peasant', 'pink', 
               'pintuck', 'plaid', 'pleated', 'polkadot', 'printed', 
               'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless', 
               'split', 'striped', 'summer', 'trim', 'tunic', 
               'v-neck', 'woven', '']

target_array = [['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],
                ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]

for id_target, el in enumerate(target_array):
    for num in el[1].split(','):
        target_array[id_target][1] = target_array[id_target][1].replace(num,
                                                        content_list[int(num)])

這是解決方案:

import numpy as np
c= ['abstract', 'bow', 'button', 'chiffon', 'collar', 'cotton', 'crepe', 'crochet', 'crop', 'embroidered', 'floral', 'floralprint', 'knit', 'lace', 'longsleeve', 'peasant', 'pink', 'pintuck', 'plaid', 'pleated', 'polkadot', 'printed', 'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless', 'split', 'striped', 'summer', 'trim', 'tunic', 'v-neck', 'woven', '']   
m=[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]

n = np.copy(m)

for i in range(np.size(m,1)):
    for j in range(np.size(m[i][1].split(','))):
        idx = m[i][1].split(',')[j]
        if (j==0):
            n[i][1] = c[int(idx)]
        else:
            n[i][1] += ',' + c[int(idx)]
            
print(n)

輸出:

[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg' 'sheer']
 ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg'
  'pleated,peasant,sheer']]

暫無
暫無

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

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