使用 lapply 时如何维护行号?

问题描述 投票:0回答:1

这是我正在处理的 JSON 示例输入数据:

 {"results":[{"address_components":[{"long_name":"16","short_name":"16","types":["street_number"]},{"long_name":"Bhagwan Tatyasaheb Kawade Road","short_name":"BT Kawde Road","types":["route"]},{"long_name":"Palmgrove Society","short_name":"Palmgrove Society","types":["neighborhood","political"]},{"long_name":"Uday Baug","short_name":"Uday Baug","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Ghorpadi","short_name":"Ghorpadi","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]},{"long_name":"411001","short_name":"411001","types":["postal_code"]}],"formatted_address":"16, BT Kawade Road, Palmgrove Society, Uday Baug, Ghorpadi, Pune, Maharashtra 411001, India","geometry":{"location":{"lat":18.5132611,"lng":73.907346},"location_type":"ROOFTOP","viewport":{"northeast":{"lat":18.5146100802915,"lng":73.90869498029151},"southwest":{"lat":18.51191211970849,"lng":73.90599701970851}}},"place_id":"ChIJo1QsU7nBwjsRiewRdiNc2i4","types":["street_address"]},{"address_components":[{"long_name":"Jambhulkar Mala","short_name":"Jambhulkar Mala","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Jambhulkar Mala, Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}},"location":{"lat":18.508659,"lng":73.9029138},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}}},"place_id":"ChIJJ0lCDMfBwjsRrPmUq2ZOxd8","types":["political","sublocality","sublocality_level_2"]},{"address_components":[{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}},"location":{"lat":18.4828904,"lng":73.9016832},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}}},"place_id":"ChIJ8TQ7l8LBwjsROUGpMh25HMw","types":["political","sublocality","sublocality_level_1"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Prabhag 36","short_name":"Prabhag 36","types":["administrative_area_level_3","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}},"location":{"lat":18.5204303,"lng":73.8567437},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}}},"place_id":"ChIJARFGZy6_wjsRQ-Oenb9DjYI","types":["locality","political"]},{"address_components":[{"long_name":"411040","short_name":"411040","types":["postal_code"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra 411040, India","geometry":{"bounds":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}},"location":{"lat":18.492095,"lng":73.90017759999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}}},"place_id":"ChIJ-64DzdvBwjsRKzv08MBrn18","types":["postal_code"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":19.38404,"lng":75.16309},"southwest":{"lat":17.89324,"lng":73.32352}},"location":{"lat":18.6832564,"lng":74.0300122},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":19.38404,"lng":75.1447465},"southwest":{"lat":17.89324,"lng":73.32352}}},"place_id":"ChIJQ97RPE_AwjsR5zbDDbo3wHI","types":["administrative_area_level_2","political"]},{"address_components":[{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":22.028441,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}},"location":{"lat":19.7514798,"lng":75.7138884},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":22.0279091,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}}},"place_id":"ChIJ-dacnB7EzzsRtk_gS5IiLxs","types":["administrative_area_level_1","political"]},{"address_components":[{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"India","geometry":{"bounds":{"northeast":{"lat":35.5087008,"lng":97.39535869999999},"southwest":{"lat":6.4626999,"lng":68.1097}},"location":{"lat":20.593684,"lng":78.96288},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":35.5087008,"lng":97.39498069999999},"southwest":{"lat":6.7535159,"lng":68.16288519999999}}},"place_id":"ChIJkbeSa_BfYzARphNChaFPjNc","types":["country","political"]},{"address_components":[{"long_name":"Uday Baug","short_name":"Uday Baug","types":["bus_station","establishment","point_of_interest","transit_station"]},{"long_name":"Uday Baug","short_name":"Uday Baug","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Ghorpadi","short_name":"Ghorpadi","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]},{"long_name":"411001","short_name":"411001","types":["postal_code"]}],"formatted_address":"Uday Baug, Ghorpadi, Pune, Maharashtra 411001, India","geometry":{"location":{"lat":18.510892,"lng":73.906956},"location_type":"GEOMETRIC_CENTER","viewport":{"northeast":{"lat":18.5122409802915,"lng":73.9083049802915},"southwest":{"lat":18.5095430197085,"lng":73.90560701970848}}},"place_id":"ChIJ5YdfqcDBwjsR5VJutWLCsfA","types":["bus_station","establishment","point_of_interest","transit_station"]}],"status":"OK"}
 {"results":[{"address_components":[{"long_name":"Canal Road","short_name":"Canal Rd","types":["route"]},{"long_name":"Empress Garden View Society","short_name":"Empress Garden View Society","types":["political","sublocality","sublocality_level_3"]},{"long_name":"Uday Baug","short_name":"Uday Baug","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Ghorpadi","short_name":"Ghorpadi","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]},{"long_name":"411001","short_name":"411001","types":["postal_code"]}],"formatted_address":"Canal Rd, Empress Garden View Society, Uday Baug, Ghorpadi, Pune, Maharashtra 411001, India","geometry":{"bounds":{"northeast":{"lat":18.510637,"lng":73.9070137},"southwest":{"lat":18.5102785,"lng":73.9016839}},"location":{"lat":18.510445,"lng":73.90438309999999},"location_type":"GEOMETRIC_CENTER","viewport":{"northeast":{"lat":18.5118067302915,"lng":73.9070137},"southwest":{"lat":18.50910876970849,"lng":73.9016839}}},"place_id":"ChIJ88lKe8fBwjsRCRKukkS6-nM","types":["route"]},{"address_components":[{"long_name":"Jambhulkar Mala","short_name":"Jambhulkar Mala","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Jambhulkar Mala, Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}},"location":{"lat":18.508659,"lng":73.9029138},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}}},"place_id":"ChIJJ0lCDMfBwjsRrPmUq2ZOxd8","types":["political","sublocality","sublocality_level_2"]},{"address_components":[{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}},"location":{"lat":18.4828904,"lng":73.9016832},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}}},"place_id":"ChIJ8TQ7l8LBwjsROUGpMh25HMw","types":["political","sublocality","sublocality_level_1"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Prabhag 36","short_name":"Prabhag 36","types":["administrative_area_level_3","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}},"location":{"lat":18.5204303,"lng":73.8567437},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}}},"place_id":"ChIJARFGZy6_wjsRQ-Oenb9DjYI","types":["locality","political"]},{"address_components":[{"long_name":"411040","short_name":"411040","types":["postal_code"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra 411040, India","geometry":{"bounds":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}},"location":{"lat":18.492095,"lng":73.90017759999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}}},"place_id":"ChIJ-64DzdvBwjsRKzv08MBrn18","types":["postal_code"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":19.38404,"lng":75.16309},"southwest":{"lat":17.89324,"lng":73.32352}},"location":{"lat":18.6832564,"lng":74.0300122},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":19.38404,"lng":75.1447465},"southwest":{"lat":17.89324,"lng":73.32352}}},"place_id":"ChIJQ97RPE_AwjsR5zbDDbo3wHI","types":["administrative_area_level_2","political"]},{"address_components":[{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":22.028441,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}},"location":{"lat":19.7514798,"lng":75.7138884},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":22.0279091,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}}},"place_id":"ChIJ-dacnB7EzzsRtk_gS5IiLxs","types":["administrative_area_level_1","political"]},{"address_components":[{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"India","geometry":{"bounds":{"northeast":{"lat":35.5087008,"lng":97.39535869999999},"southwest":{"lat":6.4626999,"lng":68.1097}},"location":{"lat":20.593684,"lng":78.96288},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":35.5087008,"lng":97.39498069999999},"southwest":{"lat":6.7535159,"lng":68.16288519999999}}},"place_id":"ChIJkbeSa_BfYzARphNChaFPjNc","types":["country","political"]}],"status":"OK"}
 {"results":[{"address_components":[{"long_name":"Canal Road","short_name":"Canal Rd","types":["route"]},{"long_name":"Empress Garden View Society","short_name":"Empress Garden View Society","types":["political","sublocality","sublocality_level_3"]},{"long_name":"Uday Baug","short_name":"Uday Baug","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Ghorpadi","short_name":"Ghorpadi","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]},{"long_name":"411001","short_name":"411001","types":["postal_code"]}],"formatted_address":"Canal Rd, Empress Garden View Society, Uday Baug, Ghorpadi, Pune, Maharashtra 411001, India","geometry":{"bounds":{"northeast":{"lat":18.510637,"lng":73.9070137},"southwest":{"lat":18.5102785,"lng":73.9016839}},"location":{"lat":18.510445,"lng":73.90438309999999},"location_type":"GEOMETRIC_CENTER","viewport":{"northeast":{"lat":18.5118067302915,"lng":73.9070137},"southwest":{"lat":18.50910876970849,"lng":73.9016839}}},"place_id":"ChIJ88lKe8fBwjsRCRKukkS6-nM","types":["route"]},{"address_components":[{"long_name":"Jambhulkar Mala","short_name":"Jambhulkar Mala","types":["political","sublocality","sublocality_level_2"]},{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Jambhulkar Mala, Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}},"location":{"lat":18.508659,"lng":73.9029138},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.510584,"lng":73.9071699},"southwest":{"lat":18.5064841,"lng":73.89949709999999}}},"place_id":"ChIJJ0lCDMfBwjsRrPmUq2ZOxd8","types":["political","sublocality","sublocality_level_2"]},{"address_components":[{"long_name":"Wanowrie","short_name":"Wanowrie","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Wanowrie, Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}},"location":{"lat":18.4828904,"lng":73.9016832},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.512962,"lng":73.9174169},"southwest":{"lat":18.480897,"lng":73.8890401}}},"place_id":"ChIJ8TQ7l8LBwjsROUGpMh25HMw","types":["political","sublocality","sublocality_level_1"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Prabhag 36","short_name":"Prabhag 36","types":["administrative_area_level_3","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}},"location":{"lat":18.5204303,"lng":73.8567437},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.6357545,"lng":73.9864569},"southwest":{"lat":18.4134784,"lng":73.7394779}}},"place_id":"ChIJARFGZy6_wjsRQ-Oenb9DjYI","types":["locality","political"]},{"address_components":[{"long_name":"411040","short_name":"411040","types":["postal_code"]},{"long_name":"Pune","short_name":"Pune","types":["locality","political"]},{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra 411040, India","geometry":{"bounds":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}},"location":{"lat":18.492095,"lng":73.90017759999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":18.5105018,"lng":73.9287002},"southwest":{"lat":18.4785059,"lng":73.87890030000001}}},"place_id":"ChIJ-64DzdvBwjsRKzv08MBrn18","types":["postal_code"]},{"address_components":[{"long_name":"Pune","short_name":"Pune","types":["administrative_area_level_2","political"]},{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Pune, Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":19.38404,"lng":75.16309},"southwest":{"lat":17.89324,"lng":73.32352}},"location":{"lat":18.6832564,"lng":74.0300122},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":19.38404,"lng":75.1447465},"southwest":{"lat":17.89324,"lng":73.32352}}},"place_id":"ChIJQ97RPE_AwjsR5zbDDbo3wHI","types":["administrative_area_level_2","political"]},{"address_components":[{"long_name":"Maharashtra","short_name":"MH","types":["administrative_area_level_1","political"]},{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"Maharashtra, India","geometry":{"bounds":{"northeast":{"lat":22.028441,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}},"location":{"lat":19.7514798,"lng":75.7138884},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":22.0279091,"lng":80.890924},"southwest":{"lat":15.6024121,"lng":72.659363}}},"place_id":"ChIJ-dacnB7EzzsRtk_gS5IiLxs","types":["administrative_area_level_1","political"]},{"address_components":[{"long_name":"India","short_name":"IN","types":["country","political"]}],"formatted_address":"India","geometry":{"bounds":{"northeast":{"lat":35.5087008,"lng":97.39535869999999},"southwest":{"lat":6.4626999,"lng":68.1097}},"location":{"lat":20.593684,"lng":78.96288},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":35.5087008,"lng":97.39498069999999},"southwest":{"lat":6.7535159,"lng":68.16288519999999}}},"place_id":"ChIJkbeSa_BfYzARphNChaFPjNc","types":["country","political"]}],"status":"OK"}

我正在尝试从第一次出现的地址组件中提取值。

这样我的虚拟代码看起来像:

dat <- lapply(readLines("D:/jsonSample_pune.json"), fromJSON)
res = lapply(dat, function(x) {

  lapply(x[["results"]], function(y) {

    do.call(rbind, lapply(y[['address_components']], function(z) {

      if (is.null(z)) {

        c(rep(NA, 2))}

      else {

        c(z[['types']][[1]], z[['long_name']]
      }
    }))
  })
})

df_pune <- ldply(sapply(res,'[[',1), data.frame)
write.csv(df_pune,"D:/add_component.csv",row.names = F)

这给了我 O/P 作为:

Type                                  Long_Name
route                                 Unnamed Road
Sub-locality                          Lohgaon
locality                              Pune
administrative_area_level_2           Pune
administrative_area_level_1           Maharashtra
country                               India
postal_code                           412105
premise                               T-1
Sub-locality                          V S N L Old Colony
Sub-locality1                         Dighi
locality                              Pimpri-Chinchwad
administrative_area_level_2           Pune
administrative_area_level_1           Maharashtra
country                               India
postal_code                           411015

但我期待这样的O/P:

Type                                  Long_Name                Rec_Numb 
route                                 Unnamed Road             1           
Sub-locality                          Lohgaon                  1 
locality                              Pune                     1
administrative_area_level_2           Pune                     1
administrative_area_level_1           Maharashtra              1
country                               India                    1
postal_code                           412105                   1
premise                               T-1                      2
Sub-locality                          V S N L Old Colony       2
Sub-locality1                         Dighi                    2
locality                              Pimpri-Chinchwad         2
administrative_area_level_2           Pune                     2
administrative_area_level_1           Maharashtra              2
country                               India                    2
postal_code                           411015                   2
.                                      .                       .
.                                      .                       .

应该做什么样的修改才能得到预期的O/P?

r json apply lapply
1个回答
0
投票

我觉得你的做法很复杂

dat %>% 
  lapply(`[[`, "results") %>% 
  lapply(`[[`, "address_components") %>% 
  purrr::map(dplyr::bind_rows) %>% 
  dplyr::bind_rows(.id = "id") 

这是你想要的吗? (除了选择/重新排序列)

© www.soinside.com 2019 - 2024. All rights reserved.