943,654 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 874
  • Python RSS
Jun 8th, 2008
0

python list woes :(

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pukebag is offline Offline
2 posts
since Jun 2008
Jun 8th, 2008
0

Re: python list woes :(

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.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Jun 8th, 2008
0

Re: python list woes :(

Here was my solution to the problem:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Dunganb is offline Offline
10 posts
since Jun 2008
Jun 8th, 2008
0

Re: python list woes :(

ah nice one dude!

Im sooo stupid
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pukebag is offline Offline
2 posts
since Jun 2008
Jun 8th, 2008
0

Re: python list woes :(

Mine is:
python Syntax (Toggle Plain Text)
  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)
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Jun 9th, 2008
0

Re: python list woes :(

Click to Expand / Collapse  Quote originally posted by Dunganb ...
Here was my solution to the problem:

Python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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']
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Can't hear PMIDI
Next Thread in Python Forum Timeline: Install Python 3.0a5 on Windows Vista System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC