python list woes :(

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 2
Reputation: pukebag is an unknown quantity at this point 
Solved Threads: 0
pukebag pukebag is offline Offline
Newbie Poster

python list woes :(

 
0
  #1
Jun 8th, 2008
Hi there,

I have a mixed list containing strings, integers, strings with numbers both prefix and suffix and mixed case strings as follow:

mixedlist = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]

I'm trying to filter the list for any element containing "cake" so I get a new list that looks something like this:

newlist = ["cake", "cakecar", "cAKe", "1cake", "cake1"]



So my approach was to:

convert each element in the list to a string (avoiding any integers) using:
stringlist = [str(x) for x in mixedlist]

and then using x.lower() to ignore case.

then for loop'in through the list looking for any element containing "cake"

and finally using newlist.append(x) to append filtered elements to new list


problem is i cant filter out an element that contains "cake" were its with a number i.e. "1cake" or "cake1" nor an element containing cake with anything else i.e. "cakecar"


Any help would be appreciated, cheers!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 128
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: python list woes :(

 
0
  #2
Jun 8th, 2008
What is the problem with filtering?

I suppose you have a function, that tells if a list member contains cake. Does this function behave unexpectedly?

I think this function should look like:
ffilt=lambda x: str(x).lower().find("cake")<>-1
Last edited by slate; Jun 8th, 2008 at 5:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 10
Reputation: Dunganb is an unknown quantity at this point 
Solved Threads: 3
Dunganb Dunganb is offline Offline
Newbie Poster

Re: python list woes :(

 
0
  #3
Jun 8th, 2008
Here was my solution to the problem:

  1. word = 'cake'
  2. cakeList = []
  3. mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
  4. for x in mixedList:
  5. if word in str(x).lower():
  6. cakeList.append(x)
  7. print cakeList

With the output:
['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
Each element in mixedList is searched for the string 'cake', and if this is True, the element is appended into cakeList.

I am still a n00b at python, so this might not be the most efficient method of solving this problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: pukebag is an unknown quantity at this point 
Solved Threads: 0
pukebag pukebag is offline Offline
Newbie Poster

Re: python list woes :(

 
0
  #4
Jun 8th, 2008
ah nice one dude!

Im sooo stupid
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 128
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: python list woes :(

 
0
  #5
Jun 8th, 2008
Mine is:
  1. mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
  2. ffilt=lambda x: str(x).lower().find("cake")<>-1
  3. print filter(ffilt,mixedList)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,137
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: python list woes :(

 
0
  #6
Jun 9th, 2008
Originally Posted by Dunganb View Post
Here was my solution to the problem:

  1. word = 'cake'
  2. cakeList = []
  3. mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
  4. for x in mixedList:
  5. if word in str(x).lower():
  6. cakeList.append(x)
  7. print cakeList

With the output:
['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
Each element in mixedList is searched for the string 'cake', and if this is True, the element is appended into cakeList.

I am still a n00b at python, so this might not be the most efficient method of solving this problem.
Pretty nice code for a noob! You can simplify it a little with list comprehension ...
  1. word = 'cake'
  2. mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
  3.  
  4. cakeList = [x for x in mixedList if word in str(x).lower()]
  5.  
  6. print cakeList # ['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum


Views: 730 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC