当前位置: 代码迷 >> python >> 有没有比每次循环后清空列表更好的方法了?
  详细解决方案

有没有比每次循环后清空列表更好的方法了?

热度:35   发布时间:2023-06-13 16:49:00.0

我是Python的新手。 我刚刚编写了一个新脚本,用于从多个Google Analytics(分析)配置文件中导出一些数据。 它工作得很好,但是我确定它写得很差。

我真的不知道从哪里开始改进它,所以这是我的第一个问题。

我正在遍历个人资料ID的列表。 对于每个配置文件ID,我有几个操作,其中使用了append方法。 因此,我正在逐步构建一些列表,但是最后我需要重置这些列表。 所以我在代码的开头和结尾都有这样的内容:

fullurllist = []
urllist = []
share = []
sharelist = []
sharelist1 = []
end_list = []

我想我应该避免这种情况。 我是否需要更改代码的所有逻辑。 我还有其他事情可以改善这个方面。

这是代码:

  # Loop through the profiles_list and get the best pages for each profile 
  for profile in profiles_list:
    response = service.data().ga().get(
      ids='ga:' + profile,
      start_date='1daysAgo',
      end_date='today',
      metrics='ga:sessions',
      dimensions='ga:pagePath',
      sort='-ga:sessions',
      filters='ga:sessions>400').execute()

    # Catch response.
    rawdata = response.get('rows', [])

    # Flatten response (which is a list of lists)
    for row in rawdata:
      urllist.append(row[0])

    # Building a list of full url (Hostname + Page path)
    fullurllist = [urljoin(base, h) for h in urllist]

    # Scraping some data from the url list
    for url in fullurllist:  

      try:
          page = urllib2.urlopen(url)
      except urllib2.HTTPError as e:
              if e.getcode() == 404: # eheck the return code
                  continue
      soup = BeautifulSoup(page, 'html.parser')

      # Take out the <div> of name and get its value
      name_box = soup.find(attrs={'class': 'nb-shares'})
      if name_box is None:
        continue
      share = name_box.text.strip() # strip() is used to remove starting and trailing

      # save the data in tuple
      sharelist.append(url)
      sharelist1.append(share)

      # Format the data scraped
      end_list = [int(1000*float(x.replace('k', ''))) if 'k' in x else int(x) for x in sharelist1]

    #export in csv
    csv_out = open(response.get('profileInfo').get('profileName') + '.csv', 'wb')
    mywriter = csv.writer(csv_out)
    for row in zip(sharelist, end_list):
      mywriter.writerow([row])
    csv_out.close()

    #reset list
    fullurllist = []
    urllist = []
    share = []
    sharelist = []
    sharelist1 = []
    end_list = []

非常感谢 !

更合适的方法是在for循环的顶部而不是外部进行声明( fullurlllist = [] )。 他们应该只生活在循环中

  相关解决方案