程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How can dictionaries with the same value be printed when dictionaries are nested in the python list

編輯:Python
The phenomenon and background of the problem

Want to take out two ordinary world , But with for Loop traversal can only take out one , And I want to take out two books of liucixin

 print('************** The book management system **************')library = [{
'name':' Trisomy ','author':' liu ','price':24,'number':5}, {
'name':' Wandering the earth ','author':' liu ','price':26,'number':5} ,{
'name':' Ordinary world ','author':' Lu yao ','price':108,'number':5}, {
'name':' Ordinary world ','author':' Luyao ','price':108,'number':5} ,{
'name':' Alive ','author':' Yuhua ','price':20,'number':5}]name_list = []author_list = []for i in library: name_list.append(i.get('name')) author_list.append(i.get('author'))print(name_list)print(author_list)while 1: print('1. Borrow books \n2. Return books \n3. Inquire about \n4. Library \n5. sign out ') choice = int(input(' Please select the function :')) if choice == 1: i_name = input(' Please enter the book you want to borrow :') n_number = name_list.count(i_name) for i in range(0,n_number): for book in library: if book.get('name') == i_name: print(' The relevant information in this book is :\n Title :{}\t author :{}\t Price :{} element \t Inventory :{} Ben '.format(book.get('name'), book.get('author'), book.get('price'),book.get('number'))) break else: print(' The book is not included in the library ')
Operation results and error reporting contents
My solution ideas and tried methods
What I want to achieve

You want to realize the selection of books according to the book title and the selection of books according to the author's name , And take them all out




Take the answer :

Then put your code if The branch break Take it out



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved