我是python的新手。我有一个列表:"level_one_links"
:
mylinks = [
{
"type" : "np",
"link_id" : "quotes-first-1",
"link" : "/login"
},
{
"type" : "np",
"link_id" : "quotes-first-2",
"link" : "/author/Albert-Einstein"
},......................
]
现在我想搜索链接,例如/ author / Albert-Einstein。如果该链接不存在,我想将其附加到列表中,否则什么都不做。
答案 0 :(得分:0)
I2
答案 1 :(得分:0)
一种方法是转换为包含所有“链接”字段的列表,然后使用in
,例如:
mylinks = [
{
"type" : "np",
"link_id" : "quotes-first-1",
"link" : "/login"
},
{
"type" : "np",
"link_id" : "quotes-first-2",
"link" : "/author/Albert-Einstein"
}]
if "/author/Albert-Einstein" in [ x["link"] for x in mylinks ]:
print("Found it!")
else:
print("Not there...")
# Append your new object
mylinks.append({...})