[Tutor] Location of found item in list.

Kent Johnson kent37 at tds.net
Thu Oct 19 03:38:35 CEST 2006


Chris Hengge wrote:
> Ok the example I gave here wasn't written as I had it in my head.. you 
> are right, the example only had 1 sentence.
> 
> Inputs:
> List1 ['a.exe','b.exe',c.exe']
> List2 ['A.exe',B.eXe',c.EXE']
> 
> for item in List1:
>      if item in List2:
>            print item + " " + list2thing that matched.

But the list2thing that matched is the same as item! In this example 
nothing will match because nothing in list1 is also in list2. I still 
don't have a clue what you want.
> 
> I can't force to upper or lower, that would break the already working 
> code..
> 
> Its just this darn output for list2 that isn't working.
> 
> I tried the suggested list.index(item) but that wont work if there isn't 
> a match.

It raises an exception if there is no match, but you want to print only 
if there is a match. John Fouhy showed how to catch the exception.

I'm done guessing what you want. If you can't state the problem so I can 
understand it I give up.

Kent

> Right now my code works when there is a match, and if there isnt'...
> It also works for renaming the actual file to match the file call from 
> the document.
> 
> I'm about to take the display out, since I dont honestly care that it 
> works, but now that I've been working with it I'm being stubborn and 
> want the darn thing to show me =P
> 
> On 10/18/06, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> wrote:
> 
>     Chris Hengge wrote:
>      > Tried your first suggestion.
>      > AttributeError: 'list' object has no attribute 'find'
> 
>     Sorry, it's index() not find(). Strings have both but lists only have
>     index()
>      >
>      > Perhaps a better explanation...
>      >
>      >
>      > for word in paragraph:
>      >      if word in sentence:
>      >           print word + sentence
>      >
>      > Assume that the word is only used once per paragraph.
> 
>     Still not clear - the above looks like it would actually run.
>      >
>      > I can't figure out how to tell it to print the right sentence (using
>      > this example) because python does the search internally and
>     doesn't seem
>      > to have a way to return the list location where the match occurred.
> 
>     There is only one sentence in the above example.
> 
>     I think you want index(). If not, maybe you could show a small sample of
>     the data and the result you want.
>      >
>      >
>      > On 10/18/06, *Kent Johnson* < kent37 at tds.net
>     <mailto:kent37 at tds.net> <mailto:kent37 at tds.net
>     <mailto:kent37 at tds.net>>> wrote:
>      >
>      >     Chris Hengge wrote:
>      >      > Still no progress with this myself.
>      >      >
>      >      > For clarification if I didn't provide enough earlier,
>      >      > for item in list1:
>      >      >     if item in list2:
>      >      >          print item and list[object at location where matched
>      >     item] <--
>      >      > need this location.
>      >
>      >     I still don't understand your question. If you want the index
>     in list2
>      >     of the item that matches, use list2.find(item).
>      >
>      >     If you want to enumerate over a list and have the list indices
>      >     available
>      >     as well as the list values, use enumerate() e.g.
>      >     for i, item in enumerate(list1):
>      >        # i is the index of item in list1
>      >
>      >     Kent
>      >
>      >      >
>      >      > On 10/18/06, *Chris Hengge* < pyro9219 at gmail.com
>     <mailto:pyro9219 at gmail.com>
>      >     <mailto: pyro9219 at gmail.com <mailto:pyro9219 at gmail.com>>
>      >      > <mailto:pyro9219 at gmail.com <mailto:pyro9219 at gmail.com>
>     <mailto:pyro9219 at gmail.com <mailto:pyro9219 at gmail.com>>>> wrote:
>      >      >
>      >      >     I'm looking for a way to do the following.
>      >      >
>      >      >     for item in limitedLineList:
>      >      >             if item in directoryList:
>      >      >                 print match.ljust(20) +
>      >     limitedLineList[count].ljust(20)
>      >      >     + directoryList[ count].ljust(20)
>      >      >             else:
>      >      >                 print fail.ljust(20) +
>      >     limitedLineList[count].ljust(20)
>      >      >     + directoryList[count].ljust(20)
>      >      >                 os.rename(pathName + directoryList[ count],
>      >     pathName +
>      >      >     limitedLineList[count])
>      >      >             count = count + 1
>      >      >
>      >      >     Where I have underlined, needs to be the item from the
>      >      >     directoryList, and I'm unable to find a way to return
>     that.
>      >      >
>      >      >     The code is actually doing what I want correctly,
>     (cheated a
>      >     test by
>      >      >     hand changing variables), but I need to find the directory
>      >     location.
>      >      >
>      >      >     Thanks.
>      >      >
>      >      >
>      >      >
>      >      >
>      >    
>     ------------------------------------------------------------------------
>      >      >
>      >      > _______________________________________________
>      >      > Tutor maillist  -   Tutor at python.org
>     <mailto:Tutor at python.org> <mailto:Tutor at python.org
>     <mailto:Tutor at python.org>>
>      >      > http://mail.python.org/mailman/listinfo/tutor
>     <http://mail.python.org/mailman/listinfo/tutor>
>      >     <http://mail.python.org/mailman/listinfo/tutor>
>      >
>      >
>      >     _______________________________________________
>      >     Tutor maillist  -   Tutor at python.org
>     <mailto:Tutor at python.org> <mailto:Tutor at python.org
>     <mailto:Tutor at python.org>>
>      >     http://mail.python.org/mailman/listinfo/tutor
>     <http://mail.python.org/mailman/listinfo/tutor>
>      >     <http://mail.python.org/mailman/listinfo/tutor>
>      >
>      >
> 
> 
>     _______________________________________________
>     Tutor maillist  -   Tutor at python.org <mailto:Tutor at python.org>
>     http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list