Question

Write a single command line to accomplish the following tasks in Python 1- Count the number...

Write a single command line to accomplish the following tasks in Python
1- Count the number of instances of each item in a list and produce another list of lists containing each item and its count. (Use the list count method)
2- Remove all words containing a capital letter at the end in a given string. (Use the join and ends with methods)
3- Remove every other letter from a string

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

########
###### Below Command Prints List of Lists Containing Each Item and It's Count
a = [1,2,3,2,1,3,3,1,1,3,4,4,2,22] # Initial List
print([[i,a.count(i)] for i in set(a)]) # It using list count method to print list of lists with each item and its count

#########
######### Below command Removes all words containing a capital letter at the end in a given string
string1 = "thiS is ChegG questioN" # Initial String
### It uses join and endswith method to remove all words containing a capital letter at the end in a given string
print(" ".join([(i if True not in [True if i.endswith(chr(j)) else False for j in range(65,91)] else "") for i in string1.split()]))   

######
##### Below command removes every other letter from a string
string2 = "This is Chegg question"
print(string2[::2]) ## It Uses Slicing Method For Removing other letter from string


Code Screenshots:-

Sample Output:-

Here I Attached Code And Sample Output For Given Problems

For Indentation Please Go Through Screenshots

For Any Queries Please Comment

Please Do Like

Add a comment
Know the answer?
Add Answer to:
Write a single command line to accomplish the following tasks in Python 1- Count the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a single line shell command to run the C program above and remove all...

    1. Write a single line shell command to run the C program above and remove all words "a" from the file news.txt 2. Please write a C program remove Word.c to obtain the input from standard input and remove all word "the". 3. Please write a C program countSentence.c to obtain the input from standard input and count the number of sentences. Assume a sentence should end with ., .",?or! 4. Please write a C program listFreq.c to list the...

  • Your Python program should perform the following three tasks: 1. After getting a word of input...

    Your Python program should perform the following three tasks: 1. After getting a word of input from the user (i.e., any string), your program should use a while (or for) loop to print out each of the letters of the word. Just remember that strings in Python start with element 0! 2. Your program should then use another loop to print out each of the letters of the (same) word in reverse order! 3. Ask the user for a letter...

  • Write a single C++ statements to accomplish each of the following tasks: In one statement, assign...

    Write a single C++ statements to accomplish each of the following tasks: In one statement, assign the sum of the current value of x and y to z and postincrement the value of x. Determine whether the value of the variable count is greater than 10. If it is, print “Count is greater than 10.”. Pre-decrement the variable of x by 1, then subtract it from the variable total. Multiply variable power by x and assign the result to power.  ...

  • Letter Count : Write a Python script that reads a file and outputs the number of...

    Letter Count : Write a Python script that reads a file and outputs the number of words that start with each letter. This means that for every letter we want to count the total number of (nonunique) words that begin with that letter. In your implementation you should ignore the letter case, i.e., consider all words as lower case. Output the results in the following format (below values are hypothetical): a or A: 120 b or B: 460 c or...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • 1) Write a single line UNIX command to list all sh files matching the multiple conditions...

    1) Write a single line UNIX command to list all sh files matching the multiple conditions below: • at directory "/home/test" • filename containing "exam" 2) Write a single line UNIX command to check if "/home/exam2" in the PATH variable. 3) How to obtain the value of command line arguments in a shell program? 4) Write a single line UNIX command to run an executable Java program Hello at background and output the number of lines in the result. 5)...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for...

    %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for this assignment. • In the Generator class, write an __init__() method with two parameters: self and the path to a text file containing one word per line. This method should read the words from the file, strip off leading and trailing whitespace, and store them in a dictionary where each key is a lower-case letter and each corresponding value is a list of words...

  • For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me t...

    For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me to not use .join() and write a SINGLE LINE function to join each letter in a list with another string.(See problem below). Using reduce, map, and lambda, write the single-expression function myJoin(L, sep) that takes a non-empty list L and a string sep, and without calling sep.join(L), works roughly the same as that returning a single string with the values in L...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT