python中的字典列表嵌套循環


 

>>> test1 = {"zhangsan":["java","c","python"],"lisi":["c++","golang"],"wangwu":["c","java","python","javascript"]} >>> type(test1) <class 'dict'>
>>> len(test1) 3
>>> for i,j in test1.items(): print("\n",i) for k in j: print(k) zhangsan java c python lisi c++ golang wangwu c java python javascript

 

>>> test1["zhaoliu"]=["python"] >>> test1 {'zhangsan': ['java', 'c', 'python'], 'lisi': ['c++', 'golang'], 'wangwu': ['c', 'java', 'python', 'javascript'], 'zhaoliu': ['python']} >>> len(test1) 4
>>> for i,j in test1.items(): if len(j) == 1: print(f"\n{i}'s favorite language is {j[0]}") else: print("\n",i) for k in j: print(k) zhangsan java c python lisi c++ golang wangwu c java python javascript zhaoliu's favorite language is python

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM