Basics of Numpy - Arithmetical operation and arrays handling code samples - 4

This post is in continuation to where we left in

Basics of Numpy - Arithmetical operation and arrays handling code samples

Basics of Numpy - Arithmetical operation and arrays handling code samples - 2

Basics of Numpy - Arithmetical operation and arrays handling code samples - 3

Lets explore more on array operation and cover string , artihmetical operations that we perform on ndarray object.

String functions

ADD - element-wise string concatenation for two arrays of str or Unicode

print np.char.add(['hello'],[' xyz']) 

print np.char.add(['hello', 'hi'],[' abc', ' xyz'])

MULTIPLY - Returns the string with multiple concatenation, element-wise

print np.char.multiply('Hello ',3)

CENTER - copy of the given string with elements centered in a string of specified length

# np.char.center(arr, width,fillchar) 

print np.char.center('hello', 20,fillchar = '*')

CAPITALISE - only the first character capitalized

print np.char.capitalize('hello world')

TITLE - element-wise title cased version of the string or unicode

print np.char.title('hello how are you?')

LOWER - elements converted to lowercase

print np.char.lower(['HELLO','WORLD']) 

print np.char.lower('HELLO')

UPPER - elements converted to uppercase

print np.char.upper('hello') 

print np.char.upper(['hello','world'])

SPLIT -  list of the words in the string, using separatordelimiter

print np.char.split ('hello how do you do ?') 

print np.char.split ('Viplav,Anand,check', sep = ',')

SPLITLINES - list of the lines in the element, breaking at the line boundaries

print np.char.splitlines('hello\nhow are you?') 

print np.char.splitlines('hello\rhow are you?')

STRIP - the leading and trailing characters removed

print np.char.strip('viplav van','v') 

print np.char.strip(['viplav','anand','vov'],'v')

JOIN - the concatenation of the strings in the sequence

print np.char.join(':','hms') 

print np.char.join([':','-'],['hms','dmy'])

REPLACE -  all occurrences of substring replaced by the new string

print np.char.replace ('He is a good boy', 'is', 'was')

DECODE , ENCODE - encoded text

a = np.char.encode('hello', 'cp500') 

print a 

print np.char.decode(a,'cp500') 

Post a Comment

Previous Post Next Post