在列表dict下方,如果我们有重复的名称,则需要获取具有指定名称的dict。如果名称是单个名称,则需要获取不具有指定名称的字典
<form class="form-style-9"
[formGroup]="productForm"
(ngSubmit)="addProduct(productForm.value)"
enctype="multipart/form-data"
>
<ul>
<li><span>Product Name</span>
<input type="text" name="productname" class="field-style field-full align-none" placeholder="Name" formControlName="productname" />
</li>
<span>Product Category</span><br>
<select formControlName="productcategory">
<option value="Clothing">Clothing</option>
<option value="Electronics">Electronics</option>
<option value="Books">Books</option>
<option value="Toys">Toys</option>
</select>
<li><span>Product Description</span>
<textarea name="productdescription" class="field-style" placeholder="Product Description" formControlName="productdescription"></textarea>
</li>
<li><span>Product Image</span>
<input type="file" name="productimage" class="field-style field-full align-none" placeholder="Image" formControlName="productimage"/>
</li>
<li><span>Product Price</span>
<input type="number" name="productprice" class="field-style field-full align-none" placeholder="Product Price" formControlName="productprice" />
</li>
<li>
<input type="submit" value="Add Product" />
</li>
</ul>
</form>
我的输出应如下所示。
lst = [{'desig': '', 'name': 'William'}, {'desig': 'Chairman of the Board', 'name': 'William'}, {'desig': '', 'name': 'English'}, {'desig': 'Director', 'name': 'English'}, {'desig': '', 'name': 'Charles '}]
答案 0 :(得分:0)
以下应解决您的问题。请注意,如果名称不完全相同,则将它们计为不同的名称。在您的原始帖子中,第二个项目的名称为“ William”,而第一个项目的名称为“ William”,在m后面有一个空格!
data = [{'desig': '', 'name': 'William'}, {'desig': 'Chairman of the Board', 'name': 'William'},
{'desig': '', 'name': 'English'}, {'desig': 'Director', 'name': 'English'},
{'desig': '', 'name': 'Charles '}]
cleaned_data = []
names_added = []
for entry in data:
if entry['name'] in names_added:
if entry['desig'] != '': # the != '' is actually not necessary, but included for clarity
i = names_added.index(entry['name'])
cleaned_data[i]=entry
else:
cleaned_data.append(entry)
names_added.append(entry['name'])
print(cleaned_data)
答案 1 :(得分:0)
from collections import defaultdict
lst = [{'desig': '', 'name': 'William'}, {'desig': 'Chairman of the Board', 'name': 'William'}, {'desig': '', 'name': 'English'}, {'desig': 'Director', 'name': 'English'}, {'desig': '', 'name': 'Charles '}]
d = defaultdict(list)
for i in lst:
d[i['name']].append(i['desig'])
l = [{'desig': sorted(v)[-1], 'name': k} for k, v in d.items()]
print(l)
打印:
[{'desig': 'Chairman of the Board', 'name': 'William'}, {'desig': 'Director', 'name': 'English'}, {'desig': '', 'name': 'Charles '}]
答案 2 :(得分:0)
>>> lst = [{'desig': '', 'name': 'William'}, {'desig': 'Chairman of the Board', 'name': 'William'}, {'desig': '', 'name': 'English'}, {'desig': 'Director', 'name': 'English'}, {'desig': '', 'name': 'Charles '}]
>>> names=set()
>>> list(dict(names.add(d['name']) or (d['name'],d) for d in lst if not d['name'] in names or d['desig']).values())
[{'desig': 'Chairman of the Board', 'name': 'William'}, {'desig': 'Director', 'name': 'English'}, {'desig': '', 'name': 'Charles '}]