| | |
python list woes :(
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: 122
Reputation:
Solved Threads: 30
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
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd coordinates cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt






