-2

I am trying to write a script that will give each of the respective hours between 2 separate hours in 2 columns of data .

There will never be a negative integer and the list only will contain numbers 0 - 23. Here is the sample data:

Here is sample data

So, I would need it to print;

7:00, 8:00

7:00, 8:00, 9:00

8:00, 9:00, 10:00, 11:00, 12:00, 13:00

Etc.

I need to keep a count using a counter of how many times each of the times occurred.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Are you familiar with the [`range`](https://docs.python.org/3/library/functions.html#func-range) function? It takes a starting and ending integer, and produces a sequence with all the integers between them (including the first, not the last). The rest of your problem would just require looping through a file line-by-line. If you have any *specific* question please post your code and specify where you are stuck. – Cory Kramer Sep 26 '19 at 13:29
  • There is a function `range(from, to)` that would give you the range. If you need a list, wrap it into `list(range(0, 25))` (as example) – vav Sep 26 '19 at 13:30
  • Yes, but in my file I have a start point and and stop point in 2 separate columns, would I need to feed these independently? I have edited my question to include the data points I have. –  Sep 26 '19 at 13:32
  • 1
    So is your question about reading a file/csv/excel or how to produce these numbers? You have to be more specific and focus your question – Tomerikoo Sep 26 '19 at 13:34
  • I apologize I am new, and getting ripped up. Not sure how all this works. My fault. I am trying to read the CSV (I know how to do this) but apply the range function to each row and then count up the occurrences. –  Sep 26 '19 at 13:37
  • Start by converting each of your lines to a couple of integers. – Stop harming Monica Sep 26 '19 at 13:38
  • 1
    Please try to reword your question to be as accurate as possible. The best test is try reading after you write it and try to see if it could make sense to someone not familiar with your problem – Tomerikoo Sep 26 '19 at 13:38
  • I have reworded my question and the post has been reopened. Thank you. –  Sep 26 '19 at 18:21
  • Given what has been answered, you can at least get the range of numbers, right? Good questions usually include some attempt at the problem. And as you generate the values while printing them, it's possible to count them as well – OneCricketeer Sep 27 '19 at 00:20

2 Answers2

2

This is done with the built-in function range.

For example:

start = 6
end = 10

for num in range(start, end+1):
    print(num)

Produces:

6
7
8
9
10

Notice that end+1 is necessary as the end argument is exclusive.


To do that for multiple rows you can do something like:

import csv

count = {}

with open('data.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        start, end = row
        r = range(start, end+1)
        for num in r:
            count[num] = count.get(num, 0) + 1
        print(*r, sep=", ")

Hope this helps

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • I have edited the question to include my files structure. I understand what was printed here but doesn't seem to work for my file. Thanks for the help too. I also did not know the easiest way to count these occurrences up. –  Sep 26 '19 at 13:35
  • what do you mean count the occurences? If all you need is count, you don't need to create the numbers, just a mathematical calculation as `end - start + 1`... – Tomerikoo Sep 26 '19 at 13:40
  • So after I calculate each line of data's range, I need to count up how many times the integer appeared in one of the lists. So after the 5,500 lines are calculated using range function I need to know how many times "1" showed up, "2" showed up etc. –  Sep 26 '19 at 13:42
  • 1
    in that case you can use a dictionary to keep track of the counts, or better yet, a [`Counter`](https://docs.python.org/3.7/library/collections.html#collections.Counter). But again, try to rephrase your question. Have a read in the [help center](https://stackoverflow.com/help/asking) specificaly [how to ask](https://stackoverflow.com/help/how-to-ask) and how to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Tomerikoo Sep 26 '19 at 13:44
0

def in_between(a,b):
    return range(a,b+1)

in_between(1,9)
#[1,2,3,4,5,6,7,8,9]

in_between(-2,4)
#[-2,-1,0,1,2,3,4]
tomgalpin
  • 1,943
  • 1
  • 4
  • 12
  • I have reworded my question and my post has been reopened. Your suggestion does not work for what I am looking to have accomplished. Thank you. –  Sep 26 '19 at 18:22