Python 一次跳过语句的一行代码

Python 一次跳过语句的一行代码,python,Python,我想第一次跳过 但很多都很重要。我很笨 count = 0 for a in articles: if count == 0: count += 1 continue data = a.b.c() etc = a.abcde(E) # ~~~~.......... 完整代码 for c,l in enumerate(BeautifulSoup(requests.get(NS_URL, timeout=3).text, 'lxml'

我想第一次跳过 但很多都很重要。我很笨

count = 0
for a in articles:
    if count == 0:
        count += 1
        continue
    data = a.b.c()
    etc = a.abcde(E)
    # ~~~~..........
完整代码

for c,l in enumerate(BeautifulSoup(requests.get(NS_URL, timeout=3).text, 'lxml').find_all('li')):    
    if c == 0: continue
    link = l.a.get('href')
    title = l.h1.a.text
    img = l.img.get('src')
新代码

for l in BeautifulSoup(requests.get(NS_URL, timeout=3).text, 'lxml').find_all('li'))[1:]:
    link = l.a.get('href')
    title = l.h1.a.text
    img = l.img.get('src')
我使用冠词[1:]。 thx tdelaney

您可以将“articles”包装为“enumerate”,这也将返回索引(递增顺序)。如果索引为0,则可以调用“continue”:

articles = ['one', 'two', 'three']
for i, a in enumerate(articles):
    if not i: continue
    # do something with a
    print(a)

如果要迭代项目并对其进行计数,请使用
enumerate()
函数:

for count, a in enumerate(articles):
    if count == 0:
        continue
    # now do whatever

您可以确保
articles
是迭代器,并使用
next
放弃迭代器

i_articles = iter(articles)
next(i_articles)
for a in i_articles:
    data = a.b.c()
或者使用
itertools
对序列进行切片,就像
somelist[1::]
一样

for a in itertools.islice(articles, 1, None, 1):
    data = a.b.c.()

对!简单易用。bat进口itertools;您甚至可能不需要
itertools
,这取决于
文章的内容。您可以在文章[1:::]…
中尝试
,看看会发生什么。