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

 

posted @ 2021-01-01 12:28  小鲨鱼2018  阅读(1466)  评论(0编辑  收藏  举报