Thursday, April 30, 2015

Convert string or strings into a list in python

I always wanted a python cheatsheet where by looking could get an idea.Since i'm a novice programmer,i need to look at these snippets so that it will get into longterm memory
>> myname="cam"
>>> list(myname)
['c', 'a', 'm']
>>> myname.split()
['cam']

>>> "cam".split('c') # split at c and eliminate c
['', 'am']
>>> "cam".split('cam')
['', '']

>>> "cacacacacacccaaaa".split('c')  #split or break at c and remove c 
['', 'a', 'a', 'a', 'a', 'a', '', '', 'aaaa']
#observe the fist character it's empty string
# ""+"cacacacacacccaaa" will give us same string "cacacacacacccaaa"


>>> "cacacacacacccaaaa".split('a')
['c', 'c', 'c', 'c', 'c', 'ccc', '', '', '', '']


"""
Problem: I've a text which have a | seperators.
I need to make a list of numbers 

text = '2,4,6,8|10,12,14,16|18,20,22,24' 

Output:

[[2,4,6,8],[10,12,14,16,18],[18,20,22,24]]

"""

>>> list(text)
['2', ',', '4', ',', '6', ',', '8', '|', '1', '0', ',', '1', '2', ',', '1', '4', ',', '1', '6', '|', '1', '8', ',', '2', '0', ',', '2', '2', ',', '2', '4']

>>> text.split()
['2,4,6,8|10,12,14,16|18,20,22,24']

# but it's just one single string and i still see the seperators '|'

 >>> text.split('|')
['2,4,6,8', '10,12,14,16', '18,20,22,24']

"""wow we got rid of '|'
well this is okay but  it is a list of 3 strings 
'2,4,6,8'==>together one string"""


>>> len(text.split('|'))
3



 #we can use a for loop iterate over the list and add each element to a new list

>>> mynewlist=[]
>>> for i in text.split('|'):
...     mynewlist.append(i.split(','))
... 
>>> mynewlist
[['2', '4', '6', '8'], ['10', '12', '14', '16'], ['18', '20', '22', '24']]


 
#There's more cleaner way to do it.
#So let's use list comprehension to acheive this.


>>> text = '2,4,6,8|10,12,14,16|18,20,22,24' 
>>> my_data = [x.split(',') for x in text.split('|')]
>>> my_data
[['2', '4', '6', '8'], ['10', '12', '14', '16'], ['18', '20', '22', '24']] #but they are still strings

>>> [[int(y) for y in x.split(',')] for x in text.split('|')]
[[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]]


#or alternatively we can use map function
#http://pythonnotesbyajay.blogspot.in/2013/03/imap-and-map-in-python.html
#Simple usage of map (it takes 3 arguments 1.function,2.iterable,3.iterable )

>>> x=[1,2,3]
>>> y=[4,5,6]
>>> map(pow,x,y)

>>> output=list(map(pow,x,y))
>>> output
[1, 32, 729]


>>> text = '2,4,6,8|10,12,14,16|18,20,22,24'
>>> strs1=text.split('|')

>>> [map(int,x.split(',')) for x in strs1] 
[[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]]


#How do you change this string into list 'QH QD JC KD JS' ?

>>> 'QH QD JC KD JS'.split()
['QH', 'QD', 'JC', 'KD', 'JS']



More about Split
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment