I’m trying to create a program where if you input a word, it will print out each letter of the word and how many times the letter appears in that word. Eg; when I input “aaaarggh”, the output should be “a 4 r 1 g 2 h 1”. So far it just prints out each letter and position in the
Tag: string
Iterable over raw text documents expected, string object received
I am currently trying to build a naive Bayes classifier as mentioned in this link. Referring to the line under the Training the Classifier subheading, I had a similar line, X_new_counts = count_vect.transform(input.plot_movie) in my code which should take an iterable as an input to the transform function. The input is a record from a DataFrame and is of type
How to check if a string is a palindrome?
I have a code to check whether a word is palindrome or not: If a word is inputted, for example : rotor , I want the program to check whether this word is palindrome and give output as “The given word is a palindrome”. But I’m facing problem that, the program checks first r and r and prints “The given
generating word cloud for items in a list in python
and I am generating a word cloud for this list by using As I am converting all the items into string it is generating word cloud for help me for generating word cloud for items in a list Answer one way of doing, Another way by creating Counter Dictionary,
Python print indentations
In Python 3.x how do you print an indent of white space, the method I am looking for looked something like this should print: repeating answer from below: thanks to @sudo_coffee for t or maybe: Answer sorry guys, think I found the answer, thanks to @sudo_coffee for t or maybe:
How to convert Julian date to standard date?
I have a string as Julian date like “16152” meaning 152’nd day of 2016 or “15234” meaning 234’th day of 2015. How can I convert these Julian dates to format like 20/05/2016 using Python 3 standard library? I can get the year 2016 like this: date = 20 + julian[0:1], where julian is the string containing the Julian date, but
pandas dataframe str.contains() AND operation
I have a df (Pandas Dataframe) with three rows: The function df.col_name.str.contains(“apple|banana”) will catch all of the rows: How do I apply AND operator to the str.contains() method, so that it only grabs strings that contain BOTH “apple” & “banana”? I’d like to grab strings that contains 10-20 different words (grape, watermelon, berry, orange, …, etc.) Answer You can do
How can a string representation of a NumPy array be converted to a NumPy array?
The function numpy.array_repr can be used to create a string representation of a NumPy array. How can a string representation of a NumPy array be converted to a NumPy array? Let’s say the string representation is as follows: How could this be converted to a NumPy array? Answer eval is the easiest, probably. It evaluates a given string as if
Extract digits from string by condition
I want to extract digits from a short string, base on a condition that the digits is in front of a character (S flag). example and result: I can split the string to a list to get the individual element, but how could I just get the 18 and 10? Answer Use re.findall with the regex r'(d+)S’. This matches all
Count consecutive characters
How would I count consecutive characters in Python to see the number of times each unique digit repeats before the next unique digit? At first, I thought I could do something like: So that in this manner I could see the number of times each unique digit repeats. But this, of course, falls out of range when i reaches the