简体   繁体   English

For循环用列表中的名称替换字符串中的单词

[英]For loop to replace word in string with name in list

I need a for loop to replace "name" in html string below with a name from name_lst.我需要一个 for 循环来用 name_lst 中的名称替换下面 html 字符串中的“名称”。 The expected output should be the plotted map with a name at each coordinate from the lists below.预期的 output 应该是绘制的 map,在下面列表中的每个坐标处都有一个名称。

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)

Getting error: TypeError: zip argument #3 must support iteration出现错误:TypeError:zip 参数 #3 必须支持迭代

If anyone could help me create a Tooltip for this, it would be awesome to see the name when hovering over the coordinates.如果有人可以帮助我为此创建一个工具提示,那么在将鼠标悬停在坐标上时看到名称会很棒。

Check indentation for "html.replace("name",name_lst,1) "检查“html.replace("name",name_lst,1) " 的缩进

I do not have problem running the for loop with zip.使用 zip 运行 for 循环没有问题。 Please see below.请看下文。

for lat, lng, name in zip(lat_lst, lng_lst, name_lst): print(lat,lng,name)对于 lat、lng、zip 中的名称(lat_lst、lng_lst、name_lst):打印(lat、lng、名称)

48.6064556 -56.3330408 Nova Scotia 52.9399159 -106.4508639 Saskatchewan 46.510712 -63.4168136 Prince Edward Island 51.253775 -85.3232139 Ontario 48.6064556 -56.3330408 新斯科舍省 52.9399159 -106.4508639 萨斯喀彻温省 46.510712 -63.4168136 爱德华王子岛 51.253775 -85.3232139 安大略省

Update, managed to solve this problem by concatenation:更新,设法通过串联解决了这个问题:

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

Produces the correct name on the map:在 map 上生成正确的名称: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM