| | |
python list woes :(
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 2
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Jun 2008
Posts: 10
Reputation:
Solved Threads: 3
Here was my solution to the problem:
With the output:
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.
Python Syntax (Toggle Plain Text)
word = 'cake' cakeList = [] mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"] for x in mixedList: if word in str(x).lower(): cakeList.append(x) 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.
•
•
Join Date: Jun 2008
Posts: 128
Reputation:
Solved Threads: 31
Mine is:
python Syntax (Toggle Plain Text)
mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"] ffilt=lambda x: str(x).lower().find("cake")<>-1 print filter(ffilt,mixedList)
•
•
•
•
Here was my solution to the problem:
Python Syntax (Toggle Plain Text)
word = 'cake' cakeList = [] mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"] for x in mixedList: if word in str(x).lower(): cakeList.append(x) 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.
python Syntax (Toggle Plain Text)
word = 'cake' mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"] cakeList = [x for x in mixedList if word in str(x).lower()] print cakeList # ['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
May 'the Google' be with you!
![]() |
Other Threads in the Python Forum
- Previous Thread: Can't hear PMIDI
- Next Thread: Install Python 3.0a5 on Windows Vista System
Views: 730 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app bash beginner bits calling chmod cmd code data dictionary directory dynamic edit examples excel external feet file float format ftp function gui halp homework http images import info input ip itunes java keycontrol line linux list lists loan loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects push py-mailer py2exe pygame pyqt python queue random recursion recursive scrolledtext smtp split ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice whileloop windows wxpython xlib






