Capitalize the first string in list using python - python-2.7

I have a list containing strings and I want capitalize the first letter of first string using Python and not the entire list.
I have attempted the following but every first letter in the list is capitalized:
L = ("hello", "what", "is", "your", "name")
LCaps = [str.capitalize(element) for element in L)
print LCaps

So, you want to capitalize the first string and only the first string of a tuple. Use:
>>> L = ("hello", "what", "is", "your", "name")
>>> (L[0].capitalize(),) + L[1:]
('Hello', 'what', 'is', 'your', 'name')
Key points:
Strings have methods. There is no need to use the string module: just use the strings capitalize method.
By running L[0].capitalize(), we capitalize the first string but none of the others.
Because L is a tuple, we can't change the first string in-place. We can however capitalize the first string and concatenate it with the rest.

(L[0].title(),) + L[1:]
You can use title() too.

Related

how to print list all first name

I had a list with string first name and last name
val dataList = List("Narendra MODI","Amit SHA","Donald TRUMP","Ratan TATA","Abdul KALAM")
I want to print all the first from the list like Narendra,Amit,Donald,Ratan,Abdul
could you please help me on this in scala
The simplest option is to take the initial non-space characters from each string:
dataList.map(_.takeWhile(!_.isSpaceChar))
you can map over your list and use split on space and select the 1st index.
scala> val dataList = List("Narendra MODI","Amit SHA","Donald TRUMP","Ratan TATA","Abdul KALAM")
dataList: List[String] = List(Narendra MODI, Amit SHA, Donald TRUMP, Ratan TATA, Abdul KALAM)
scala> dataList.map( _.split(" ").headOption.getOrElse(None))
res2: List[java.io.Serializable] = List(Narendra, Amit, Donald, Ratan, Abdul)

How to use "\w+" to find words in a string?

I need to write a function that takes a string as input. This function will return a List[String]. I have to use the regular expression "\w+" in this function as a requirement for this task. So when given a line string of random text with a few actual words dotted around inside it, I need to add all of these 'proper' words and add them to the list to be returned. I must also use ".findAllIn". I have tried the following
def foo(stringIn: String) : List[String] = {
val regEx = """\w+""".r
val match = regEx.findAllIn(s).toList
match
}
But it just returns the string that I pass into the function.
match is a reserved keyword in scala. So you just need to replace that.
def foo(stringIn: String) : List[String] = {
val regEx = """\w+""".r
regEx.findAllIn(stringIn).toList
}
scala> foo("hey. how are you?")
res17: List[String] = List(hey, how, are, you)
\\w is the pattern for a word character, in the current regex context equal to [a-zA-Z_0-9], that matches a lower- and uppercase letters, digits and an underscore.
\\w+ is for one ore more occurrences of the above.
scala> foo("hey")
res18: List[String] = List(hey)
In above case, there is nothing for the regex to split by. Hence returns the original string.
scala> foo("hey-hey")
res20: List[String] = List(hey, hey)
- is not part of \\w. Hence it splits by -

Extracting Current line if some sub-string match first time only?

I have a string like this
str = ["asap subject ssfs sfdsf sdfsdfsdefs sdfssdf","nsubject qwerty
swqt","dsfsdf sdfsdf sdfsfs sfsdf er:subject adsdsd dsdfs
sdfsdfsdfsds"]
What i Want
str = ["ssfs sfdsf sdfsdfsdefs sdfssdf","qwerty
swqt","adsdsd dsdfs sdfsdfsdfsds"]
I using
for i in range(0,len(str)):
list_i.append(str[i].strip("subject*)[1])
But problem is when i have long text after subject and i want value of current line only.
Seems like you should be using the str.split function.
Instead of
str[i].strip("subject")[1]
replace that with
str[i].split("subject ",1)[-1]
This splits the string at "subject", then takes the last element of that result.

converting a list to string and printing it out python

I am trying to convert the first letter of each word of a string to uppercase in python. But i keep getting a generator object at 0x10315b8> no post before this seems to answer my question.
def capitalize(str):
newstr = str.split(' ')
newlist = []
for word in newstr:
if word[0][0] == word[0][0].upper():
newlist.append(word[0][0].upper())
newlist.append(word[0][1:])
newlist.append(" ")
convert_first = (str(w) for w in newlist)
print(convert_first)
capitalize(input("enter some string"))#calling the function
Your problem lies in how you are trying to make a string out of a list of strings. The opposite of "splitting" a string into a list is "joining" a list into a string.
def capitalize(str):
newstr = str.split(' ')
newlist = []
for word in newstr:
newlist.append(word[0].upper() + word[1:])
convert_first = ' '.join(newlist)
print(convert_first)
capitalize(input("enter some string"))#calling the function
Note: I made an attempt to have my code be as close as possible to that in the question.
Also, why is there an if statement in your code? With that in place you're really just capitalizing all the words that are already capitalized and discarding the rest since they never make it into newlist.
There are a few issues with your code:
The error message you got is for trying to print convert_first, which is a generator, not a string.
newstr is a list of words, so word is a string and word[0] is already the first character. Meaningless for word[0][0] or word[0][1:].
if word[0][0] == word[0][0].upper(): just filters all the words whose first character is not uppercase...
So simply some code will do what you described:
def capitalize(str):
newstr = str.split(' ')
newlist = []
for word in newstr:
newlist.append(word[0].upper())
newlist.append(word[1:])
newlist.append(" ")
convert_first = ''.join(w for w in newlist)
print(convert_first)
capitalize(input("enter some string"))
Or those who favors short code and generator expressions:
def capitalize(str):
print(' '.join(word[0].upper() + word[1:] for word in str.split(' ')))
capitalize(input("enter some string"))
This also removes the tailing space of the generated string, which may (not) be what you intended.

Python 2.7 : Remove elements from a multidimensional list

Basically, I have a 3dimensional list (it is a list of tokens, where the first dimension is for the text, second for the sentence, and third for the word).
Addressing an element in the list (lets call it mat) can be done for example:
mat[2][3][4]. That would give us the fifth word or the fourth sentence in the third text.
But, some of the words are just symbols like '.' or ',' or '?'. I need to remove all of them. I thought to do that with a procedure:
def removePunc(mat):
newMat = []
newText = []
newSentence = []
for text in mat:
for sentence in text:
for word in sentence:
if word not in " !##$%^&*()-_+={}[]|\\:;'<>?,./\"":
newSentence.append(word)
newText.append(newSentence)
newMat.append(newText)
return newMat
Now, when I try to use that:
finalMat = removePunc(mat)
it is giving me the same list (mat is a 3 dimensional list). My idea was to iterate over the list and remove only the 'words' which are actually punctuation symbols.
I don't know what I am doing wrong but surely there is a simple logical mistake.
Edit: I need to keep the structure of the array. So, words of the same sentence should still be in the same sentence (just without the 'punctuation symbol' words). Example:
a = [[['as', '.'], ['w', '?', '?']], [['asas', '23', '!'], ['h', ',', ',']]]
after the changes should be:
a = [[['as'], ['w']], [['asas', '23'], ['h']]]
Thanks for reading and/or giving me a reply.
I would suspect that your data are not organized as you think they are. And although I am usually not the one to propose regular expressions, I think in your case they may be among the best solutions.
I would also suggest that instead of eliminating non-alphabetic characters from words, you process sentences
>>> import re
>>> non_word = re.compile(r'\W+') # If your sentences may
>>> sentence = '''The formatting sucks, but the only change that I've made to your code was shortening the "symbols" string to one character. The only issue that I can identify is either with the "symbols" string (though it looks like all chars in it are properly escaped) that you used, or the punctuation is not actually separate words'''
>>> words = re.split(non_word, sentence)
>>> words
['The', 'formatting', 'sucks', 'but', 'the', 'only', 'change', 'that', 'I', 've', 'made', 'to', 'your', 'code', 'was', 'shortening', 'the', 'symbols', 'string', 'to', 'one', 'character', 'The', 'only', 'issue', 'that', 'I', 'can', 'identify', 'is', 'either', 'with', 'the', 'symbols', 'string', 'though', 'it', 'looks', 'like', 'all', 'chars', 'in', 'it', 'are', 'properly', 'escaped', 'that', 'you', 'used', 'or', 'the', 'punctuation', 'is', 'not', 'actually', 'separate', 'words']
>>>
The code you wrote seems solid and it looks like "it should work", but only if this:
But, some of the words are just symbols like '.' or ',' or '?'
is actually fulfilled.
I would actually expect the symbols to not be separate from words, so instead of:
["Are", "you", "sure", "?"] #example sentence
you would rather have:
["Are", "you", "sure?"] #example sentence
If this is the case, you would need to go along the lines of:
def removePunc(mat):
newMat = []
newText = []
newSentence = []
newWord = ""
for text in mat:
for sentence in text:
for word in sentence:
for char in word:
if char not in " !##$%^&*()-_+={}[]|\\:;'<>?,./\"":
newWord += char
newSentence.append(newWord)
newText.append(newSentence)
newMat.append(newText)
return newMat
Finally, found it. As expected, it was a very small logical mistake that was always there but couldn't see it. Here is the working solution:
def removePunc(mat):
newMat = []
for text in mat:
newText = []
for sentence in text:
newSentence = []
for word in sentence:
if word not in " !##$%^&*()-_+={}[]|\\:;'<>?,./\"":
newSentence.append(word)
newText.append(newSentence)
newMat.append(newText)
return newMat