简体   繁体   中英

Replace element of list with string from another list

So I wrote this but it doesn't accomplish what I want to do. Basically, I want to replace the number in the second index with whatever the word is at that index in the content_list 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)

This is the output that i get:

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

But this is what i want:

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

First of all, max is a built-in function I would highly recommend you to check how to name variables for the future, It may cause some big problems for you :). You can brute-force your way out here also something like this:

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)

I would do something like this, I think must be better options but it works... so it's better than nothing

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

Here is the solution:

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)

The output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM