要声明一个空字典,我要做:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Mini App</title>
<style>
body {
background:white;
}
div.user-photo {
margin: 1em auto;
width: 150px;
height: 150px;
border-radius: 50%;
overflow: hidden;
}
.select {
margin-bottom: 2.5em;
}
.details {
color: white;
background-color: #6200ee;
font-size: 1.3em;
margin-top: 4em;
padding: 0.5em 1em;
border-radius: 10px;
}
.details p {
margin: 0.3em
}
#outcome {
position: 2.2em;
bottom: 6.5em;
width: 100px;
text-align: center;
}
#outcome h3 {
padding: 1em;
background-color: white;
border-radius: 10%;
margin: 0;
}
#outcome p {
height: 40px;
color: white;
border-bottom: 5px solid white;
font-size: 2em;
margin: 0;
padding: 0.5em 0;
}
#oracle {
margin-top: 2.5em;
border: 1px solid #333;
width: 100%;
}
</style>
</head>
<body>
<button class="mdc-icon-button materials-icons" id="filter-query">
filter_list
</button>
<div class="select">
<select class="select-text" id="select-text">
<option selected disabled value="0">Select user</option>
</select>
</div>
<div class="user-photo">
<img
src="https://via.placeholder.com/150
C/O https://placeholder.com/"
alt="user-photo"
/>
</div>
<div class="details mdc-elevation--z3">
<p>
<span class="prop" data-age="Age:">Age</span
><span class="value" data-age-value=""> : 40</span>
</p>
<p>
<span class="prop" data-height="Height:">Height</span
><span class="value" data-height-value=""> : 6</span>
</p>
<p>
<span class="prop" data-weight="Weight:">Weight</span
><span class="value" data-weight-value=""> : 75</span>
</p>
<p>
<span class="prop" data-gender="Gender:">Gender</span
><span class="value" data-gender-value=""> : Male</span>
</p>
<p>
<span class="prop" data-country="Counry:">Country</span
><span class="value" data-country-value=""> : Nigeria</span>
</p>
</div>
<button id="oracle" class="mdc-button">
Calculate BMI
</button>
<div id="outcome">
<h3 class="mdc-typography--headline5">BMI</h3>
<p></p>
</div>
<script>
const fetchAndDisplayUsers = () => {
users.push({
age: 40,
weight: 75,
height: 6,
country: 'Nigeria',
name: 'Charles Odili',
id: 'dfhb454768DghtF'
});
displayUsers(users);
};
const startApp = () => {
};
startApp();
</script>
</body>
</html>
要声明一个空字典的空字典,我也可以这样做:
mydict = dict()
然后在需要时添加字典:
mydictofdict = dict()
以及需要的元素:
mydict1 = dict()
mydictofdict.update({1:mydict1})
它是pythonic吗?有更好的执行方法吗?
答案 0 :(得分:3)
您可以将collections.defaultdict用于嵌套字典,在其中定义字典的初始值
from collections import defaultdict
#Use the initial value as a dictionary
dct = defaultdict(dict)
dct['a']['b'] = 'c'
dct['d']['e'] = 'f'
print(dct)
输出将为
defaultdict(<class 'dict'>, {'a': {'b': 'c'}, 'd': {'e': 'f'}})
答案 1 :(得分:2)
您可以使用a
创建空字典:
{}
您可以像第一个那样创建dict(作为值)dict:
d = {}
您可以通过d = {
1: {},
2: {}
}
来修改字典的值:
[]
d = {1: 0} d[1] = 1 d
与dict中的dict类似:
{1: 1}
d = { 1: {}, 2: {} } d[1][4] = 5 d
如果要根据键列表创建字典,则可以使用字典推导:
{1: {4: 5}, 2: {}}
keys = [1,2,3,4,5] d = {key: {} for key in keys} d