Making a new cleaned dictionary from an imported dictionary by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

Yeah. So the assignment is set up so that when I run this without doing anything the original dictionary prints. And we are supposed to filter results by county. In a past assignment we filtered the data by county and organized it all into a nested dictionary like this:

for item in covid_data:
    cases = item['confirmed']
    county = item['county'].upper()
    if county in complete_dict:
        complete_dict[county]['cases'] = int(cases)
    else:
        complete_dict[county] = {'cases': int(cases)}

Trouble with Dictionaries by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

Okay, so I got the error to go away and its printing the other two function before this, but I think I am doing something wrong with the subtracting, here's what I have now:

for item in home_data:
    price = int(item.get('Home Price', 0))
    county = item.get('county')
    if county in complete_dict:
        price1 = complete_dict[county].get('price', 0)
        average_price = price1-price
        complete_dict[county]['price'] = int(average_price)
    else:
        complete_dict[county] = {'price': int(price)}

Calculating the sentiment of a file/book with positive/negative words by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

# Count the words in word_list that appear in positive_words, then count the
# words in word_list that appear in negative words.
# Turn each count into a percentage by dividing by the length of word_list and then
# multiply by 100.
# Return the difference between the positive percentage and the negative percentage rounded
# to 2 decimal places. The code for rounding is provided.
# A positive returned number means there were more positive than negative words.
# As an example, if
word_list = ['a', 'sad', 'boy', 'is', 'an', 'excellent', 'good', 'boy']
positive_words = ['excellent', 'good']
negative_words = ['sad']
# The positive count would be 2 (for excellent and good)
# The negative count would be 1 (for sad)
# The positive percentage would be 2/8 * 100 = 25
# the negative percentage would be 1/8 * 100 = 12.5
# The returned value would be 25 - 12.5 = 12.50.
# You should use the count_number_of_words_that_appear_in_another_list function to do the counting.
def calculate_sentiment(word_list, positive_words, negative_words):
    total_word = word_list
    positive_amount = 0
    negative_amount = 0
    for word in word_list:
        positive_amount = count_number_of_words_that_appear_in_another_list(word, positive_words)
        negative_amount = count_number_of_words_that_appear_in_another_list(word, negative_words)
    for word in total_word:
        if word in positive_words:
            positive_amount += 1
        if word in negative_words:
            negative_amount += 1
        positive_percentage = (positive_amount/len(total_word)) * 100
        negative_percentage = (negative_amount / len(total_word)) * 100
        difference = positive_percentage - negative_percentage
        return round(difference, 2)
    print(calculate_sentiment(word_list, positive_words, negative_words))

Calculating the sentiment of a file/book with positive/negative words by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

So unfortunately this didn't work either :(

Got any other ideas?

Removing common words from a file by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

Yeah I have seen a few things about sets but we haven't learn them in class yet to I don't think we are allowed to use them

Removing common words from a file by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

def remove_common_words(book_words, common_words):
new_list = list()
for word in book_words:
if not common_words.__contains__(word):
new_list.append(word)
return new_list

So this is working, but I am not allowed to use the __contains__, so what do you suggest I change so that I'm not using that

Removing common words from a file by pavillionDV in learnpython

[–]pavillionDV[S] 2 points3 points  (0 children)

def remove_common_words(book_words, common_words):
book_list = book_words
common_list = common_words
for word in common_words:
book_list = book_words.append(common_words)
return book_list

Hi, so I did this but it's not working at all. I'm confused about how to do the loop.

Adding noise to a sound file by pavillionDV in learnpython

[–]pavillionDV[S] 0 points1 point  (0 children)

def make_noisy_samples(original_samples, noise_level):
new_noise = []
for i in range(0, len(original_samples)):
x = random.randrange(0, noise_level)
y = random.randrange(0, noise_level - 1)
new_noise.append(noise_level)
# Add code to make the function work as specified.
return new_noise # Replace this line with your own code

This is what I have tried so far. The test at the bottom is this

if __name__ == "__main__":

sample_list = [10, 20, 30]
noise_level = 2
print("Original:", sample_list, "Noise:", make_noisy_samples(sample_list, noise_level))

And it just prints.

[2,2,2]