How can I print out words from a list that contains a particular letter? I know what to do with words starting with specific letter but not in BETWEEN
This is One liner approach to condition
a = [print(word) for word in greekAlpha if "a" in word]
Find how many words start with certain letter in a list
You are using the a_words
as the value of the word in each iteration and missing a counter. If we change the for loop to have words
as the value and reserved a_words
for the counter, we can increment the counter each time the criteria is passed. You could change a_words
to wordCount
or something generic to make it more portable and friendly for other letters.
a_words = 0
for words in wordList:
if words[0]=='a':
a_words += 1
print(a_words, "start with the letter 'a'.")
How would I get all the words from a list that begins with the specified letter
You could loop through all the possible indices, check if the element at that index starts with the letter, and print it if it does.
ALTERNATIVE (and probably better) code (I was going to put this after, but since its better it deserves to be first. Taken form @larsmans's answer here.
//given wordList as the word list
//given startChar as the character to search for in the form of a *String* not char
for (String element : wordList){
if (element.startsWith(startChar)){
System.out.println(element);
}
}
DISCLAIMER: This code is untested, I don't have much experience with ArrayList
, and Java is more of a quaternary programming language for me. Hope it works :)
//given same variables as before
for (int i = 0; i < wordList.size(); i++){
String element = wordList.get(i);
//you could remove the temporary variable and replace element with
// wordList.get(i)
if (element.startsWith(startChar){
System.out.println(element);
}
}
How to create a statement that will print out words that start with particular word?
Use str.startswith
Ex:
St= "where is my mobile"
for i in St.split():
if i.startswith("m"):
print(i)
Output:
my
mobile
Using filter
Ex:
L = ['mobile',"pencil","Pen","eraser","Book"]
print( list(filter(lambda x: x.lower().startswith("p"), L)) )
Output:
['pencil', 'Pen']
Finding the words that start with a given letter from a list
You use filter
,
foo string = filter startsWithP (words string)
then you need to define
startsWithP :: String -> Bool
More useful will be the generic variant
startsWith :: String -> Char -> Bool
to be used like "foo" `startsWith` 'f'
.
How do I extract words from a list that start with A in PYTHON I?
You forgot to get the word out of the list and instead were trying to get the first character of the index (i), which obviously doesn't work.
w = ["Algorithm", "Logic", "Filter", "Software", "Network", "Parameters", "Analyze", "Algorithm", "Functionality", "Viruses"]
for i in range(len(w)):
if (w[i][0] == "a" or w[i][0] == "A"):
print(w[i])
However a more "pythonic" way of iterating over a list is as follows:
for word in w:
if (word[0] == "a" or word[0] == "A"):
print(word)
Related Topics
In Dictionary, Converting the Value from String to Integer
Using Continue in a Try and Except Inside While-Loop
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Tensorflow:Attributeerror: 'Module' Object Has No Attribute 'Mul'
How to Flatten a Hierarchical Index in Columns
Pandas: Sum Dataframe Rows for Given Columns
Python Ssl.Sslerror: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed (_Ssl.C:748)
How to Ignore Null Byte When Reading a CSV File
How to Convert a Float into Hex
Putting Multiple Conditions Using Np.Where on Python Pandas
Why Am I Getting Ioerror: [Errno 13] Permission Denied
Convert Np.Array of Type Float64 to Type Uint8 Scaling Values
Interactive Matplotlib Figures in Google Colab
Pyqt: Getting Widgets to Resize Automatically in a Qdialog
Read Merged Cells in Excel With Python