python3 將兩個列表生成一個字典


需求:

  存在兩個list如下

  list1 = ["one", "two", "three"]

  list2 = ["1", "2", "3"]

  需要生成一個字典如下

  dict1 = {"one" : "1", "two" : 2, "three" : "3"}

 

解決方法:

  

dict4 = {m + ":" + n for m in list1 for n in list2}
print(dict4)

{'one:1', 'three:1', 'three:2', 'one:3', 'two:2', 'two:3', 'one:2', 'two:1', 'three:3'}

 

注意:

  拼接只對string有效,對int無效


免責聲明!

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



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