d.iteritems()
时间: 2024-05-15 08:12:37 浏览: 14
这是一个 Python 中字典(dictionary)的方法,它返回一个字典迭代器,迭代器中的元素是字典中每个键值对的 (key, value) 二元组。例如:
```
d = {'a': 1, 'b': 2, 'c': 3}
for key, value in d.iteritems():
print(key, value)
```
输出结果为:
```
a 1
b 2
c 3
```
在 Python 3 中,这个方法被改名为 `items()`。
相关问题
下面是旧版本python代码,试改为python3.X版,并执行。 把生成的html代码存为.html,在浏览器中打开看下效果。 d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } def generate_tr(name, score): if score < 60: return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score) return '<tr><td>%s</td><td>%s</td></tr>' % (name, score) tds = [generate_tr(name, score) for name, score in d.iteritems()] print '<table border="1">' print '<tr><th>Name</th><th>Score</th><tr>' print '\n'.join(tds) print '</table>' '\n'.join(tds) 举例说明这个函数的用法与作用?
下面是改为 Python 3.x 版本的代码:
```python
d = {'Adam': 95, 'Lisa': 85, 'Bart': 59}
def generate_tr(name, score):
if score < 60:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
tds = [generate_tr(name, score) for name, score in d.items()]
print('<table border="1">')
print('<tr><th>Name</th><th>Score</th><tr>')
print('\n'.join(tds))
print('</table>')
```
这个程序的作用是生成一个 HTML 表格,其中包含了字典 `d` 中每个人的姓名和分数信息。函数 `generate_tr()` 用于根据输入的姓名和分数生成 HTML 表格的一行,如果分数小于 60,则将分数单元格的文字颜色设为红色。
最后,程序将生成的 HTML 行列表合并为一个字符串,并打印出完整的 HTML 表格。可以将输出保存为 HTML 文件,然后在浏览器中打开来查看效果。
简述遍历Python字典的方法
在Python中,可以使用多种方法遍历字典,包括:
1. for循环遍历键:使用for循环遍历字典的键,然后通过键获取对应的值。示例代码如下:
```
d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
print(key, d[key])
```
2. for循环遍历键值对:使用items()方法获取字典的键值对,然后使用for循环遍历。示例代码如下:
```
d = {'a': 1, 'b': 2, 'c': 3}
for key, value in d.items():
print(key, value)
```
3. 使用iteritems()方法遍历键值对(Python2):与items()方法类似,但返回的是一个迭代器,可以在循环中逐个获取键值对。示例代码如下:
```
d = {'a': 1, 'b': 2, 'c': 3}
for key, value in d.iteritems():
print(key, value)
```
4. 使用keys()方法获取所有键,再遍历键获取值:使用keys()方法获取字典的所有键,然后使用for循环遍历键,通过键获取对应的值。示例代码如下:
```
d = {'a': 1, 'b': 2, 'c': 3}
for key in d.keys():
print(key, d[key])
```
5. 使用values()方法获取所有值,再遍历值获取键:使用values()方法获取字典的所有值,然后使用for循环遍历值,通过值获取对应的键。示例代码如下:
```
d = {'a': 1, 'b': 2, 'c': 3}
for value in d.values():
for key in d.keys():
if d[key] == value:
print(key, value)
```
需要注意的是,以上方法都适用于Python2和Python3。但在Python3中,iteritems()方法被取消,items()方法返回的是一个视图对象,而不是一个列表,需要通过list()函数转换为列表才能进行遍历。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)