Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31494

Re: Index in a list

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'importing': 0.04; 'indexing': 0.07; 'stops': 0.07; 'index': 0.13; '"user': 0.16; 'beginning.': 0.16; 'conditions;': 0.16; 'constructor.': 0.16; 'entries,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'matching.': 0.16; 'oct': 0.16; 'subject:Index': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'trying': 0.21; 'received:209.85.214.174': 0.21; "i'd": 0.22; 'this:': 0.23; 'second': 0.24; 'specifically': 0.24; 'header :In-Reply-To:1': 0.25; 'looks': 0.26; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'loop,': 0.29; 'search.': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'file': 0.32; 'generally': 0.32; 'to:addr:python-list': 0.33; 'entry': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'or,': 0.34; 'list': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'but': 0.36; 'skip:v 20': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; 'first': 0.61; "you'll": 0.62
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=heJZMmTEpjcR9QXC5wzrG/o3lxu9ZnyFMpZJcE5T1Tc=; b=ygdECHNlXar6RBUG9KG6WBwHeYplmG7q5OyFbVeKFJe6Q5Hy/H18vchTKhm/ngoA9i Vbe4npwGNLFTLIFIwhOotAkProX1pBcoTsT5k9a6ZMeP8IQbAy+McKzbRG/dZmhgo7c0 0mkLc1jGfezJMsmOkFO59ZVaimH3XssncUS/L5F3H0ulnScEA1L7G8F28bL7iiOiZdLG Vgo5QvQWN1ok4a136tyEiB7bkVw0eC1ZXDiGsC0/IdKTnPGWHB23OmCgznqu3D3qsg8c WRiFrjIvatSBUglN1J77sf88Fxxfn6lcPE1Q4+PGc+zyIGgjEx5PInovLqXkZIDoBWUP ZHGQ==
MIME-Version 1.0
In-Reply-To <CAKhY55P2-9GJtj_sZCPdJb2R00OZ3GXjZVixuDasn_Cisb_+yw@mail.gmail.com>
References <CAKhY55P2-9GJtj_sZCPdJb2R00OZ3GXjZVixuDasn_Cisb_+yw@mail.gmail.com>
Date Wed, 17 Oct 2012 21:23:08 +1100
Subject Re: Index in a list
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2339.1350469391.27098.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350469391 news.xs4all.nl 6980 [2001:888:2000:d::a6]:42698
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31494

Show key headers only | View raw


On Wed, Oct 17, 2012 at 9:10 PM, Anatoli Hristov <tolidtm@gmail.com> wrote:
> Hello,
>
> I'm trying to index a text in a list as I'm importing a log file and
> each line is a list.
>
> What I'm trying to do is find the right line which contains the text
> User : and take the username right after the text "User :", but the
> list.index("(User :") is indexing only if all the text matching. How
> can I have the right position of the line which contains the word
> ("(User :")

What you want is a search. Try this:

for idx,val in enumerate(list):
    # if "(User:" in val: break
    # if val.startswith("(User:"): break


Pick one or t'other of those conditions; the first one looks for any
string _containing_ "(User:", while the second will match specifically
on the beginning.

After this loop, list[idx] is the "User" line, and the entry after it
is in list[idx+1] - but be aware that you'll get an exception if
list[idx] is the last entry in the list.

By the way, it's generally considered dodgy to use the name "list" for
a list - it stops you from using the list constructor. I'd recommend
calling it "lst" instead or, better, to name it according to what it
contains (eg if it's a list of log entries, call it 'log_entries' or
something).

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Index in a list Chris Angelico <rosuav@gmail.com> - 2012-10-17 21:23 +1100

csiph-web