Question

Please help me write a script in python that counts the percentage of each number that...

Please help me write a script in python that counts the percentage of each number that appears in the total number of lines, for example 16 appears 2 times out of 16 lines, etc. I got this so far, just dont know how to identify each number and calculate each percentage

with open('itemNumber.txt') as f:
lines = f.readlines()
f.close()

for line in lines:
for item in line.split():
itemCounts[item] += 1

--------itemset.txt-------

16 14 3 28 10
10 9 43 34 24
25 18 15 46 48
16 14 3 28 10
10 9 43 34 24
25 18 15 46 48

0 0
Add a comment Improve this question Transcribed image text
Answer #1
with open('itemNumber.txt') as f:
    lines = f.readlines()
    f.close()
    itemCounts = {}
    total = 0
    for line in lines:
        for item in line.split():
            if item not in itemCounts:
                itemCounts[item] = 0
            itemCounts[item] += 1
        total += 1
    print("Number", "Percentage")
    for k, v in itemCounts.items():
        p = (v*100)/total
        print(k, '\t', p)

Add a comment
Know the answer?
Add Answer to:
Please help me write a script in python that counts the percentage of each number that...
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
  • For each variable of interest, do the following: 1. Find the mean, five-number summary, range, variance,...

    For each variable of interest, do the following: 1. Find the mean, five-number summary, range, variance, and standard deviation. Display these numbers in a format that is easy to understand. 2. For each variable of interest, use its five-number summary to construct a boxplot. Each boxplot must be constructed horizontally, and must be accompanied by a brief descriptive paragraph that assesses whether the data appear to be symmetrical, left-skewed, or right-skewed. Construct a 95% confidence interval for the mean μ...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

  • For each variable of interest – Percent Time Asleep and Longevity – create a grouped frequency...

    For each variable of interest – Percent Time Asleep and Longevity – create a grouped frequency histogram. For each histogram, use a class width of 10; use a lower limit of 0 for Percent Time Asleep and 15 for Longevity. Each histogram must include an informative title, along with correct labels for both axes. For each histogram, include a paragraph that answers each of the following questions: Is the histogram symmetric, skewed to the left, or skewed to the right?...

  • I know how to do number one just not number 2. Please answer number 2 only...

    I know how to do number one just not number 2. Please answer number 2 only Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,'). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13,...

  • For each of the following chemical structures a 'H-NMR spectrum is provided on the following 3...

    For each of the following chemical structures a 'H-NMR spectrum is provided on the following 3 pages. Match each compound with its 'H-NMR spectrum. Draw the structure above the corresponding spectrum and clearly assign the chemical shifts to the appropriate protons on the molecule. CH, CH, CH 3-methylbutanal 1-pentene p-ethylanisole "ОН Br diethyl malonate 2-bromobutane butanoic acid .Br OH 1-bromopropane 1-propanol diethyl ethylmalonate 30 1 1e 20 20 10 12 10 08 16 14 22 20 18 34 32 30...

  • You are studying an invasive insect species in the USA. Understanding where invasive species comes from can help with management (e.g., importation of native natural enemies for biocontrol). The graph...

    You are studying an invasive insect species in the USA. Understanding where invasive species comes from can help with management (e.g., importation of native natural enemies for biocontrol). The graph below shows output from a STRUCTURE analysis of 10 microsatellite markers in 50 individuals taken from the invasive US population, and the three native countries of origin, Spain, France, and Italy. A-1pt) How many genetic groups, or clusters, were identified in the analysis? Assuming you’ve completely sampled the native range...

  • Conduct a formal hypothesis test of the claim that the mean longevity is less than 57...

    Conduct a formal hypothesis test of the claim that the mean longevity is less than 57 days. Test at significance α=0.05. Your written summary of this test must include the following: Your null and alternate hypotheses in the proper format. The type of distribution you used to construct the interval (t or normal). The P-value and its logical relationship to α (≤ or >). Your decision regarding the null hypothesis: reject or fail to reject. A statement regarding the sufficiency/insufficiency...

  • mips add matrix Can anyone help me with adding matrix in MIPS? This is the code...

    mips add matrix Can anyone help me with adding matrix in MIPS? This is the code given. We will be using it to run it in spim-cache Write an assembly code program to perform addition between two matrices A and B, and store the result into a matrix C, i.e, 1 2 3 4 51 [26 27 28 29 301 27 29 31 33 35 6 7 8 9 10 31 32 33 34 35 37 39 41 43 45...

  • You are curious about whether you can enhance the ability to hear differences between nonnative speech...

    You are curious about whether you can enhance the ability to hear differences between nonnative speech sounds when people learn a new language. You test your novel language training procedure on a group of 32 pariticipants using a pretest/posttest design. The pretest and the posttest measure performance as the number of correct trials out of 100. The data are shown on the right. Does the new language training procedure improve the perception of nonnative speech sounds? Number of correct items...

  • When answering, please refer to your answers according to the NUMBER in the box. Need to...

    When answering, please refer to your answers according to the NUMBER in the box. Need to know answer but also if there was change or no change in column #9 Finding Equilibrium for I = 50, G = 50, and T = 50 (1) Output (Income) Y (2) Net Taxes T (3) Disposable Income YdY-T (4) Consumption Spending C = 100+ .25Yd (5) Saving Yd-C (6) Planned Investment Spending I (7) Government Purchases G (8) Planned Aggregate Expenditure C+I+G (9)...

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
ADVERTISEMENT