遍历Python中的字典
在本文中,我们将学习Python3.x中字典的迭代/遍历。或更早。
字典是键值对的无序序列。索引可以是任何不可变的类型,称为键。在花括号中也指定了该选项。
方法1-直接使用可迭代
示例
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'}
# Iterate over the string
for value in dict_inp:
print(value, end='')输出结果
trason
方法2-使用Iterables作为字典的值
示例
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'}
# Iterate over the string
for value in dict_inp.values():
print(value, end='')输出结果
oilpit
方法3-使用键作为索引
示例
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'}
# Iterate over the string
for value in dict_inp:
print(dict_inp[value], end='')输出结果
oilpit
方法4-使用字典的键和值
示例
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'}
# Iterate over the string
for value,char in dict_inp.items():
print(value,":",char, end=" ")输出结果
t : o r : i a : l s : p o : i n : t
结论
在本文中,我们了解了Python中字典的迭代/遍历。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短