gpt4 book ai didi

How do I access dictionary values with nested for loop?(如何使用嵌套的for循环访问字典值?)

转载 作者:bug小助手 更新时间:2023-10-25 23:34:09 24 4
gpt4 key购买 nike



I have the following dictionary, and I'd like to output all the items into the terminal sequentially (e.g. Other, Waxing, 20, Manicure, 30, etc...)

我有以下词典,我想按顺序将所有条目输出到终端(例如,其他、打蜡、20、美甲、30等)。


{'Other': [('Waxing', 20), ('Manicure', 30), ('Extensions', 35)], 'Styling': [('Blow Dry', 15), ('Perm', 25), ('Straightening', 25)], 'Color': [('Full Color', 40), ('Color Retouch', 20), ('Highlights', 30)], 'Haircut': [("Men's Cut", 20), ("Womens's Cut", 20), ("Children's Cut", 10), ('Specialty Cut', 30)]}

I currently have the following code:

我目前有以下代码:


    for key, value in dict.items():
print(key)
for i in range(0, len(value)):
print(value[i])
for j in range(1):
print(value[i][j])

Which outputs the following:

它输出以下内容:


Other
('Waxing', 20)
Waxing
('Manicure', 30)
Manicure
...

I'm just confused as to why my last for loop (i.e for loop 'j') isn't printing both 'Waxing' and '20' in the 3rd (and should be 4th) line. I'm under the impression that for loop 'j' should be printing value[i][0] = 'Waxing' & value[i][1] = 20

我只是搞不懂为什么我的最后一个for循环(即for循环‘j’)没有在第三行(应该是第四行)同时打印‘WAXING’和‘20’。我的印象是for循环‘j’应该是打印值[i][0]=‘打蜡’&值[i][1]=20


更多回答
优秀答案推荐

The range() function in Python returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number.

Python中的range()函数返回一个数字序列,默认情况下从0开始,以1为增量(默认情况下),并在指定的数字之前结束。


So, if you set the range like this for j in range(1), the loop will stop before it reaches index 1 (read: reaches index 0 only). If you want the loop to reach index 1 as well, you need to set the range to 2, like this for j in range(2).

因此,如果像这样为Range(1)中的j设置范围,循环将在达到索引1之前停止(Read:仅达到索引0)。如果希望循环也达到索引1,则需要将范围设置为2,就像范围(2)中的j这样。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com