使用列表从字典中检索值
Retrieve value from dictionary with list
我看到了很多问题,但没有找到像这样的字典:
我有这个dict
:
my_dict = {
'og_min': 'https://www.example.com/assets/images/empreendimentos/listing-varanda-vila-ema.png?_=1000',
'images_sobre_o_produto': ['https://www.example.com/assets/images/enterprises/varanda-vila-ema/fachada.png?_=1000', 'https://www.example.com/assets/images/enterprises/retrato-by-dialogo/living.png?_=1000']}
如何遍历该字典以一次获取所有 urls
个?
如果我使用类似的东西:
for values in my_dict.values():
print(values)
for value in values:
print(value)
og_min
键的第一个值被拆分,如何避免?
事实上你有一本字典,有两个键。
一个键是字符串,另一个是列表。
所以你可以这样做:
for key, val in my_dict.items():
if type(val) is list:
for url in val:
print(url)
if type(val) is str:
print(val)
这将为您提供您正在寻找的 url 的输出。
当然这段代码假设你必须输入值的类型,一个字符串和一个列表类型,字符串值包含一个 url 并且列表类型包含一个列表 url的。
我看到了很多问题,但没有找到像这样的字典:
我有这个dict
:
my_dict = {
'og_min': 'https://www.example.com/assets/images/empreendimentos/listing-varanda-vila-ema.png?_=1000',
'images_sobre_o_produto': ['https://www.example.com/assets/images/enterprises/varanda-vila-ema/fachada.png?_=1000', 'https://www.example.com/assets/images/enterprises/retrato-by-dialogo/living.png?_=1000']}
如何遍历该字典以一次获取所有 urls
个?
如果我使用类似的东西:
for values in my_dict.values():
print(values)
for value in values:
print(value)
og_min
键的第一个值被拆分,如何避免?
事实上你有一本字典,有两个键。 一个键是字符串,另一个是列表。
所以你可以这样做:
for key, val in my_dict.items():
if type(val) is list:
for url in val:
print(url)
if type(val) is str:
print(val)
这将为您提供您正在寻找的 url 的输出。
当然这段代码假设你必须输入值的类型,一个字符串和一个列表类型,字符串值包含一个 url 并且列表类型包含一个列表 url的。