假设您有两个字符串列表,第一个是字典的values
,第二个是相应位置键的>>print keys
['key1', 'key2', ..., 'keyN']
>>print values
['value1', 'value2', ..., 'valueN']
。所以它是这样的:
dic = dict.fromkeys(keys)
使用这行代码:
>>print dic
{'key1': None, 'key2':None, ..., 'keyN': None}
我已将密钥分配给我的字典。所以我的输出现在是这样的:
>>print dic
{'key1':'value1', 'key2':'value2', ..., 'keyN':'valueN'}
我可以以某种方式做同样的事情,我用键做了,但也有值?但是这次只将值一个一个地存储到字典中?所以我的目标输出是这样的:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link href="codepen.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="codepen.js"></script>
<script type="text/javascript" src="TweenMax.min.js"></script>
</head>
<body>
<div class="banner crop">
<img id="photo" src="http://bestevents.us/wp-content/uploads/2014/09/1409861249_10536823_10152285336897304_4970023981985043307_o.png"/>
<img id="beer1" class="absolute-left" src="http://www.gooseisland.com/content/gooseisland/en/goose/allBeers/_jcr_content/urbanAles/addbeer/sImage" />
<img id="beer2" class="absolute-center" src="http://www.gooseisland.com/content/gooseisland/en/goose/allBeers/_jcr_content/urbanAles/addbeer_1/sImage" />
<img id="beer3" class="absolute-right" src="http://www.gooseisland.com/content/gooseisland/en/goose/allBeers/_jcr_content/urbanAles/addbeer_0/sImage" />
<img id="logo" class= "absolute" src="http://www.gooseisland.com/assets/images/header-logo.png" </img>
<h2 id="hed1"> Introducing This Year's <br> Beverage Lineup </br> </h2>
<h2 id="hed2"> 312 Urban Block Party </h2>
<h3 id="hed3"> hosted by Goose Island </h3>
<h2 id="cta"> Join the Party </h2>
<h6 id="pale"> 312 Urban Pale Ale </h6>
<h6 id="wheat"> 312 Urban Wheat Ale </h6>
<h6 id="green"> Green Line Pale Ale </h6>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.15.1/TweenMax.min.js"> </script>
</banner>
</body>
</html>
答案 0 :(得分:5)
使用zip
:
>>> keys = ['a', 'b', 'c']
>>> values = ['1', '2', '3']
>>> dict(zip(keys, values))
{'a': '1', 'c': '3', 'b': '2'}