使用列表理解生成字典列表的问题

时间:2019-03-25 12:22:38

标签: python python-3.x list-comprehension

{
    "realm": "...",
    "auth-server-url": "http://...",
    "ssl-required": "none",
    "resource": "...",
    "public-client": true,
    "confidential-port": 0
} 

通过上面的列表理解,我可以生成字典列表。如果<form id="fileupload" class="d-none"> <div class="form-row"> <div class="form-group col-md-8"> <input type="text" id="num_param" class="form-control" name="num_param" > </div> <div class="form-group col-md-8"> <input type="file" id="resume" class="form-control" name="resume" accept=".doc,.docx,.pdf" required multiple/> </div> </div> <input type="submit" class="btn btn-primary mt-10" id="file-submit" value="submit"> </form> 的值相同,我需要将这些值附加到feed_mapping = {'BC': 11, 'HA':12, 'AB':16,'GR':18} x = ['AB-16007891', 'HA-4625798','GR-4444545','BC-4447764','HA-46257854'] feed = [{"feed": feed_mapping[i.split('-')[0]],"id":[i]} for i in x] print(feed)

当前输出:

id

预期输出:

feed

3 个答案:

答案 0 :(得分:5)

使用itertools.groupby

例如:

from itertools import groupby
feed_mapping = {'BC': 11, 'HA':12, 'AB':16,'GR':18}
x = ['AB-16007891', 'HA-4625798','GR-4444545','BC-4447764','HA-46257854']
x.sort()

feed = [{"feed": feed_mapping[k], "id": list(v)} for k, v in groupby(x, lambda x: x.split('-')[0])]
print(feed)

输出:

[{'feed': 16, 'id': ['AB-16007891']},
 {'feed': 11, 'id': ['BC-4447764']},
 {'feed': 18, 'id': ['GR-4444545']},
 {'feed': 12, 'id': ['HA-46257854', 'HA-4625798']}]

答案 1 :(得分:1)

不使用lambda的解决方案,请注意,此解决方案的顺序与您的问题不相同:

feed_mapping = {'BC': 11, 'HA': 12, 'AB': 16, 'GR': 18}
x = ['AB-16007891', 'HA-4625798', 'GR-4444545', 'BC-4447764', 'HA-46257854']
feed = [{"feed": feed_value, "id": [i for i in x if feed_key in i]} for
        feed_key, feed_value in feed_mapping.items()]
print(feed)

>>> [{'feed': 11, 'id': ['BC-4447764']}, {'feed': 12, 'id': ['HA-4625798', 'HA-46257854']}, {'feed': 16, 'id': ['AB-16007891']}, {'feed': 18, 'id': ['GR-4444545']}]

我使用以下for循环从feed_mapping中拆分键和值:

for feed_key, feed_value in feed_mapping.items()]

然后我有第二个(嵌套循环,用于将x中的项目添加到id字段中:

i for i in x

我注意到,仅当feed_mapping键位于i的字符串中时,您才想添加一个id,我在嵌套的for循环中使用以下if语句来做到这一点:

[i for i in x if feed_key in i]

答案 2 :(得分:1)

您可以使用 <app-search-list (onSearchInputUpdate)="onSearchFieldUpdate($event)"> </app-search-list> <app-test-selection-category-list (onCategorySelect)="updateTestPanelView($event)"></app-test-selection- category-list> 将列表 onSearchFieldUpdate($event) { this.searchField = $event; this.updateTestPanelView(this.selectedCategoryId); } updateTestPanelView(categoryId: string) { this.selectedCategoryId = categoryId; switch (this.selectedCategoryId) { case '-1': this.fetchAllTests(); break; case "0": this.fetchFavoritesForCategories(); break; default: this.fetchTestsForCategory(); } } fetchAllTests() { this.testOrderService.getAllTests(this.searchField).subscribe(response => { const {panels, tests} = this.extractPanelsAndTests(response); this.testSelectionSession = { ...this.testSelectionSession, PanelsForAll: panels, IndividualTestPanelsForAll: tests }; this.store.dispatch( new SetTestOrderTestSelectionSession(this.testSelectionSession) ); }) } fetchFavoritesForCategories() { this.testOrderService .getAllFavorites(this.searchField) .subscribe(favorites => { this.testSelectionSession = Object.assign( {}, this.testSelectionSession, { FavoritesByCategory: _.groupBy(favorites, 'CategoryName') } ); this.store.dispatch( new SetTestOrderTestSelectionSession(this.testSelectionSession) ); }); } fetchTestsForCategory() { this.testOrderService .getTestsByCategoryId(this.selectedCategoryId, this.searchField) .subscribe(categoryResponse => { const {panels, tests} = this.extractPanelsAndTests(categoryResponse); this.testSelectionSession = Object.assign( {}, this.testSelectionSession, { PanelsForCategory: panels.map(panel => { panel.CategoryId = this.selectedCategoryId; return panel; }), IndividualTestPanelsForCategory: tests.map( test => { test.CategoryId = this.selectedCategoryId; return test; } ) } ); this.store.dispatch( new SetTestOrderTestSelectionSession(this.testSelectionSession) ); }); } 转换成字典:

x