![](/img/trans.png)
[英]Replacing a value from one array with a value of the same index of another array?
[英]How to push value from an array to another, one by one index
parsed_html = BeautifulSoup(response.text, 'html.parser') frame = parsed_html.find('div', {'id':'product-frame'}) sizes = frame.findAll('div', {'class':'radio-options old-variant'}) # find all sizes sizing = [] for s in sizes: sizing.append(s.label.text.replace('\t', '').replace('\n','').replace('\xa0','')) # format sizes sizes = [{ 'size': sizing, 'in_stock': 'noStock' not in button['class'] } for button in sizes]
您好,我在將“調整大小”數組推入“大小”數組時遇到問題,它會推入數組中的所有值,而不是僅推入索引的單個值,從而使 output 看起來像這樣:
如何將大小索引從大小數組中一一推送到大小數組中?
您面臨的問題是,對於sizes
數組的每個元素,您都將整個sizing
列表推入其中。 相反,您需要做的是使用項目的索引並將該索引處的值推入其中。 例如,這是一種方法:
sizes = [{
'size': sizing[i],
'in_stock': 'noStock' not in sizes[i]['class']
} for i in range(len(sizes))]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.