用另一个列表中的字符串替换列表的元素
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']]
我会做这样的事情,我认为一定是更好的选择,但它确实有效...所以总比没有好
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']]
首先,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 列表中该索引处的任何单词替换第二个索引中的数字。
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']]
我会做这样的事情,我认为一定是更好的选择,但它确实有效...所以总比没有好
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']]
首先,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)