For循环用列表中的名称替换字符串中的单词
For loop to replace word in string with name in list
我需要一个 for 循环来用 name_lst 中的名称替换下面 html 字符串中的“名称”。
预期的输出应该是绘制的地图,在下面列表的每个坐标处都有一个名称。
lat_lst = [48.6064556,52.9399159,46.510712,51.253775,53.9332706,53.7266683,53.7608608,53.1355,46.5653163,52.9399159,70.2997711,64.8255441,64.2823274]
lng_lst = [-56.3330408,-106.4508639,-63.4168136,-85.3232139,-116.5765035,-127.6476206,-98.8138763,-57.6604,-66.4619164,-73.5491361,-83.1075769,-124.8457334,-135.00000]
name_lst = ['Nova Scotia','Saskatchewan','Prince Edward Island','Ontario','Alberta','British Columbia','Manitoba','Newfoundland and Labrador','New Brunswick','Quebec','Nunavut','Northwest Terrritories','Yukon']
for lat, lng, name in zip(lat_lst, lng_lst, name_lst):
html.replace("name",name_lst,1)
folium.map.Marker(
location=[lat,lng],
icon=DivIcon(
icon_size=(150,36),
icon_anchor=(0,0),
html='<div style="font-size: 10pt">name</div>',
)).add_to(m)
出现错误:类型错误:zip 参数 #3 必须支持迭代
如果有人可以帮助我为此创建一个工具提示,当鼠标悬停在坐标上时看到名称会很棒。
检查 "html.replace("name",name_lst,1)"
的缩进
我没有问题 运行 带 zip 的 for 循环。请看下面。
对于纬度、经度、zip 中的名称(lat_lst、lng_lst、name_lst):
打印(纬度、经度、名称)
48.6064556 -56.3330408 新斯科舍省
52.9399159 -106.4508639 萨斯喀彻温省
46.510712 -63.4168136 爱德华王子岛
51.253775 -85.3232139 安大略
更新,通过连接成功解决了这个问题:
lat_lst = [48.6064556,52.9399159,46.510712,51.253775,53.9332706,53.7266683,53.7608608,53.1355,46.5653163,52.9399159,70.2997711,64.8255441,64.2823274]
lng_lst = [-56.3330408,-106.4508639,-63.4168136,-85.3232139,-116.5765035,-127.6476206,-98.8138763,-57.6604,-66.4619164,-73.5491361,-83.1075769,-124.8457334,-135.00000]
name_lst = ['Nova Scotia','Saskatchewan','Prince Edward Island','Ontario','Alberta','British Columbia','Manitoba','Newfoundland and Labrador','New Brunswick','Quebec','Nunavut','Northwest Terrritories','Yukon']
for lat, lng, name in zip(lat_lst, lng_lst, name_lst):
folium.map.Marker(
location=[lat,lng],
icon=DivIcon(
icon_size=(150,36),
icon_anchor=(0,0),
html='<div style="font-size: 10pt">'+name+'</div>',
)).add_to(temp_map)
temp_map
在地图上生成正确的名称:
我需要一个 for 循环来用 name_lst 中的名称替换下面 html 字符串中的“名称”。 预期的输出应该是绘制的地图,在下面列表的每个坐标处都有一个名称。
lat_lst = [48.6064556,52.9399159,46.510712,51.253775,53.9332706,53.7266683,53.7608608,53.1355,46.5653163,52.9399159,70.2997711,64.8255441,64.2823274]
lng_lst = [-56.3330408,-106.4508639,-63.4168136,-85.3232139,-116.5765035,-127.6476206,-98.8138763,-57.6604,-66.4619164,-73.5491361,-83.1075769,-124.8457334,-135.00000]
name_lst = ['Nova Scotia','Saskatchewan','Prince Edward Island','Ontario','Alberta','British Columbia','Manitoba','Newfoundland and Labrador','New Brunswick','Quebec','Nunavut','Northwest Terrritories','Yukon']
for lat, lng, name in zip(lat_lst, lng_lst, name_lst):
html.replace("name",name_lst,1)
folium.map.Marker(
location=[lat,lng],
icon=DivIcon(
icon_size=(150,36),
icon_anchor=(0,0),
html='<div style="font-size: 10pt">name</div>',
)).add_to(m)
出现错误:类型错误:zip 参数 #3 必须支持迭代
如果有人可以帮助我为此创建一个工具提示,当鼠标悬停在坐标上时看到名称会很棒。
检查 "html.replace("name",name_lst,1)"
的缩进我没有问题 运行 带 zip 的 for 循环。请看下面。
对于纬度、经度、zip 中的名称(lat_lst、lng_lst、name_lst): 打印(纬度、经度、名称)
48.6064556 -56.3330408 新斯科舍省 52.9399159 -106.4508639 萨斯喀彻温省 46.510712 -63.4168136 爱德华王子岛 51.253775 -85.3232139 安大略
更新,通过连接成功解决了这个问题:
lat_lst = [48.6064556,52.9399159,46.510712,51.253775,53.9332706,53.7266683,53.7608608,53.1355,46.5653163,52.9399159,70.2997711,64.8255441,64.2823274]
lng_lst = [-56.3330408,-106.4508639,-63.4168136,-85.3232139,-116.5765035,-127.6476206,-98.8138763,-57.6604,-66.4619164,-73.5491361,-83.1075769,-124.8457334,-135.00000]
name_lst = ['Nova Scotia','Saskatchewan','Prince Edward Island','Ontario','Alberta','British Columbia','Manitoba','Newfoundland and Labrador','New Brunswick','Quebec','Nunavut','Northwest Terrritories','Yukon']
for lat, lng, name in zip(lat_lst, lng_lst, name_lst):
folium.map.Marker(
location=[lat,lng],
icon=DivIcon(
icon_size=(150,36),
icon_anchor=(0,0),
html='<div style="font-size: 10pt">'+name+'</div>',
)).add_to(temp_map)
temp_map
在地图上生成正确的名称: