【问题标题】:IndexError: string index out of range pythonIndexError:字符串索引超出范围python
【发布时间】:2016-07-26 02:26:14
【问题描述】:

我有以下列表:

url_sims1=[('http://bp.velocityfrequentflyer.com/',
  [(2, 0.90452874),
   (1, 0.83522302),
   (4, 0.77591574),
   (0, 0.72705799),
   (3, 0.52282226)]),
 ('http://cartrawler.virginaustralia.com/',
  [(3, 0.79298556),
   (1, 0.78112978),
   (2, 0.76006395),
   (0, 0.58570701),
   (4, 0.40093967)]),
 ('https://cartrawler.virginaustralia.com/book',
  [(2, 0.9549554),
   (1, 0.71705657),
   (0, 0.58731651),
   (3, 0.43987277),
   (4, 0.38266104)]),
 ('https://fly.virginaustralia.com/SSW2010/VAVA/webqtrip.html',
  [(2, 0.96805269),
   (4, 0.68034023),
   (1, 0.66391909),
   (0, 0.64251828),
   (3, 0.50730866)]),
 ('http://www.magicmillions.com.au/',
  [(2, 0.84748113),
   (4, 0.8338449),
   (1, 0.61795002),
   (0, 0.60271078),
   (3, 0.20899911)])]

我要更换这个订单

(2,...) 
(1,...)
(4,...) 
(0,...) 
(3,...)

使用以下字符串:categories=['艺术和娱乐'、'激情点'、'积极的生活方式'、'消费者习惯'、'旅行精明']。例如,“2”将被类别[2]替换

我写了以下代码:

for i in xrange(0, len(unique_url)): 
    for j in xrange(0, len(sims1)):
        for k in xrange(0,len(categories)):
            url_sims1[i][j][k][1]+=categories[k] 

但我收到此错误:IndexError: string index out of range

unique_url=['http://bp.velocityfrequentflyer.com/',
 'http://cartrawler.virginaustralia.com/',
 'https://cartrawler.virginaustralia.com/book',
 'https://fly.virginaustralia.com/SSW2010/VAVA/webqtrip.html',
 'http://www.magicmillions.com.au/']

sims1=[[(2, 0.90452874),(1, 0.83522302),(4, 0.77591574),(0, 0.72705799),(3, 0.52282226)],
 [(3, 0.79298556),(1, 0.78112978),(2, 0.76006395),(0, 0.58570701),(4, 0.40093967)],
 [(2, 0.9549554),(1, 0.71705657),(0, 0.58731651),(3, 0.43987277),(4, 0.38266104)],
 [(2, 0.96805269),(4, 0.68034023),(1, 0.66391909),(0, 0.64251828),(3, 0.50730866)],
 [(2, 0.84748113),(4, 0.8338449),(1, 0.61795002),(0, 0.60271078),(3, 0.20899911)]]

【问题讨论】:

    标签: python for-loop indexing


    【解决方案1】:

    鉴于您有url_sims1categories,然后尝试:

    In [4]: [(url, [(categories[i], x) for i,x in lst]) for url,lst in url_sims1]
    Out[4]: 
    [('http://bp.velocityfrequentflyer.com/',
      [('active lifestyle', 0.9045),
       ('points of passion', 0.8352),
       ('travel savvy', 0.7759),
       ('arts and entertainment', 0.7271),
       ('consumer habits', 0.5228)]),
     ('http://cartrawler.virginaustralia.com/',
      [('consumer habits', 0.793),
       ('points of passion', 0.7811),
       ('active lifestyle', 0.7601),
       ('arts and entertainment', 0.5857),
       ('travel savvy', 0.4009)]),
     ('https://cartrawler.virginaustralia.com/book',
      [('active lifestyle', 0.955),
       ('points of passion', 0.7171),
       ('arts and entertainment', 0.5873),
       ('consumer habits', 0.4399),
       ('travel savvy', 0.3827)]),
     ('https://fly.virginaustralia.com/SSW2010/VAVA/webqtrip.html',
      [('active lifestyle', 0.9681),
       ('travel savvy', 0.6803),
       ('points of passion', 0.6639),
       ('arts and entertainment', 0.6425),
       ('consumer habits', 0.5073)]),
     ('http://www.magicmillions.com.au/',
      [('active lifestyle', 0.8475),
       ('travel savvy', 0.8338),
       ('points of passion', 0.618),
       ('arts and entertainment', 0.6027),
       ('consumer habits', 0.209)])]
    

    或者,如果您的起点是unique_urlsims1,那么试试:

     [(url, [(categories[i], x) for i,x in lst]) for url,lst in zip(unique_url, sims1)]
    

    【讨论】:

    • 太棒了。非常感谢!
    猜你喜欢
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多