Question

In: Computer Science

Python Programming Problem: If I have to separate lists, one contains a large string of paragraphs...

Python Programming Problem:

If I have to separate lists, one contains a large string of paragraphs of words, and one contains just words, how can i iterate the words in my second list and compare it to my paragraph list to see how many times that word has occurred?

List1 = ['paragraph.......']

List2 = ['words', 'words', 'words'......]

these are just minimal examples

how do i approach this problem?

apprently numpy helps with processing time for something like this? cuz the lists could get quite big

Solutions

Expert Solution

You need to use numpy's char.count () function, it returns the number of times a word is present in a paragraph

Below is a naive example just to demonstrate how to use it.

==================================================================================

import numpy as np

paragraphs = np.array(
    ['the fox jumped and the fox drowned and the fox survived', 'the brown fox and the black fox fought in the mud.'])
words = np.array(['and', 'fox', 'the'])

for word in words:
    for paragraph in paragraphs:
        count = np.char.count(paragraph, word)
        print(paragraph, 'contains', count, '\"', word, "\"", 'word(s).')

======================================================================


Related Solutions

Python please A string is one of most powerful data types in programming. A string object...
Python please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception. In Python a string...
Create two lists; one contains 5 elements and the other one is empty. Write a python...
Create two lists; one contains 5 elements and the other one is empty. Write a python program that iterates through the first one, pops the elements from the end of it, then it pushes them one by one to the empty list. As your program iterates through the first list, clean it before processing the data, meaning if any element is like a special character ( , . ; : ) it must be discarded and not gets pushed to...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates. Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23] No-Duplicate List: [14, 24, 42, 25, 23] Same generic method for the name list Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared] No-Duplicate List: [Mike,...
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
Python Programming- Practice Lists & Tuples B #This is a template for practicing mutability and conversion...
Python Programming- Practice Lists & Tuples B #This is a template for practicing mutability and conversion #Create and assign the following list of numbers to a list data type and variable name: 99.9,88.7,89,90,100 #convert the list to a tuple and assign the tuple a different variable name #Ask the user for a grade and convert it to a float type (no prompt) and assign it to a new variable name #append the user entered grade to the list #update the...
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? #Define showExspenses function def showExpenses(loan,insure,gas,oil,tyres,maintenance): expense=loan+insure+gas+oil+tyres+maintenance #Print monthly and yearly automobile operating expenses print("\nTotal Monthly expenses for operating expenses you entered = ",expense) print(f"\nTotal Yearly expenses for operating expenses you entered = 12 *",expense,f"= {expense*12:,}") #yearly expenses loan=int(input("Enter Loan :")) insure=int(input("Enter Insurance :")) gas=int(input("Enter Gas :")) oil=int(input("Enter Oil :")) tyres=int(input("Enter Tyres :")) maintenance=int(input("Enter Maintenance:")) showExpenses(loan,insure,gas,oil,tyres,maintenance)
Python Programming I have a skeleton code here for this project but do not know exactly...
Python Programming I have a skeleton code here for this project but do not know exactly where to start. My task is to implement a reliable File Transfer Protocol (FTP) service over UDP. I need help to write the reliable FTP client and server programs based on an Alternating Bit Protocol (ABP) that will communicate with a specially designated server. In socket-level programming as well... # program description # Library imports from socket import * import sys import time #...
Python programmingWrite a program whose input is a string which contains acharacter and a...
Python programmingWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT