【问题标题】:How do I create a list like the one below?如何创建如下列表?
【发布时间】:2017-04-17 16:47:38
【问题描述】:

我有一个关于 URL 的系列,我需要复制如下。

https://wipp.edmundsassoc.com/Wipp/?wippid=*1205*

1205 是可变的,最终输出需要看起来像

"https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1"
................................................#taxpage2"
................................................#taxpage3
................................................#taxpage4

等等。我有一个没有"#taxpage" 部分的 URL 列表以及每个应该有多少个税页的列表。我想为每个 URL 生成所有可能页面的列表。感谢您的帮助....对编码完全陌生,非常感谢您提供任何帮助。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您可以使用str.format 在列表推导中添加#taxpage 数字

    >>> s = r'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage{}'
    >>> [s.format(i) for i in range(1, 5)]
        ['https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1',
         'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage2',
         'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage3',
         'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage4']
    

    【讨论】:

      【解决方案2】:

      您可以使用列表推导来做到这一点:

      In [1]: urls = ['https://wipp.edmundsassoc.com/Wipp/?wippid=1205',
                      'https://wipp.edmundsassoc.com/Wipp/?wippid=1206']
      
      In [2]: ["{}#taxpage{}".format(url, page_num) for page_num in xrange(1, 4) for url in urls]
      Out[2]: 
      ['https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1',
       'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage1',
       'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage2',
       'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage2',
       'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage3',
       'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage3']
      

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 2018-04-07
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 2019-10-20
        • 1970-01-01
        相关资源
        最近更新 更多