RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Introducing a large quantity of Variables

Sun Feb 17, 2019 11:11 am

Using Python 3.
I would like to introduce a large number (24) of variables. One for each hour of the day and all set initially to zero.
Is there a better way than:
hourr0 = 0
hourr1 = 0 etc,etc

User avatar
DougieLawson
Posts: 35373
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 11:21 am

Code: Select all

hours=[0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0]

hours[0] = hours[0]+1
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 4727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 11:23 am

Lists are what you need :-)

https://www.tutorialspoint.com/python3/python_lists.htm

Code: Select all

Accessing Values in Lists

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −
Live Demo

#!/usr/bin/python3

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]

print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

When the above code is executed, it produces the following result −

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 11:58 am

Thank you both for your replies.
For this instance, I particularly like the answer from Dougie because it will easily enable me to modify the values.
I will use the answer from Peter in another program.

User avatar
PeterO
Posts: 4727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 12:07 pm

RDS wrote:
Sun Feb 17, 2019 11:58 am
Thank you both for your replies.
For this instance, I particularly like the answer from Dougie because it will easily enable me to modify the values.
I will use the answer from Peter in another program.
:o It's the same answer, just with more detail and a useful link to a tutorial ! :roll:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 35373
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 1:13 pm

PeterO wrote:
Sun Feb 17, 2019 12:07 pm
:o It's the same answer, just with more detail and a useful link to a tutorial ! :roll:
Because Peter has more desire to do your Googling for you.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 4727
Joined: Sun Jul 22, 2012 4:14 pm

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 1:16 pm

DougieLawson wrote:
Sun Feb 17, 2019 1:13 pm
PeterO wrote:
Sun Feb 17, 2019 12:07 pm
:o It's the same answer, just with more detail and a useful link to a tutorial ! :roll:
Because Peter has more desire to do your Googling for you.
No, I just felt that not even including the word "list" in your reply was very unhelpful. What was RDS going to google for without knowing he was using a list ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MrYsLab
Posts: 334
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 1:49 pm

Depending upon how you wish to access your data, you may also want to consider using a dictionary. Al Sweigart does a nice job comparing lists and dictionaries https://automatetheboringstuff.com/chapter5/

Nothing wrong with lists, but you need to know the index into the list to access the data. In many instances, that is the correct approach. If you need a more "random access" approach, dictionaries can help. Each data item has a key (a name) associated with the data item. To access the data item, you use the key to retrieve the data.

Each has its purpose and are very useful. It would probably help you quite a bit to do a little reading about Python data structures.

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 3:11 pm

Thanks all.

@Dougie - I had hesitated before posting the question because I know how often 'Google' tends to be mentioned on here but I can assure you I did spend a lot of time looking at various tutorials but none that I checked mentioned you method you suggested (and I have now used) - Thanks.

User avatar
bensimmo
Posts: 4072
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 5:34 pm

In python there are list comprehensions

So you would (once you know) setup a large list with them and there will be many other ways

For and list of 24 zero's

Code: Select all

hour_listofzeros = [0] * 24
 
print(hour_listofzeros)

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Introducing a large quantity of Variables

Sun Feb 17, 2019 7:06 pm

@bensimmo
This gets better each time but there seems to be so many ways to do the same thing.

Thank you.

scotty101
Posts: 3596
Joined: Fri Jun 08, 2012 6:03 pm

Re: Introducing a large quantity of Variables

Mon Feb 18, 2019 11:21 am

bensimmo wrote:
Sun Feb 17, 2019 5:34 pm
In python there are list comprehensions

....

For and list of 24 zero's

Code: Select all

hour_listofzeros = [0] * 24
 
print(hour_listofzeros)
Technically not a list comprehension.

This would be one to do the same thing though

Code: Select all

x = [0 for a in range(24)]
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
bensimmo
Posts: 4072
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Introducing a large quantity of Variables

Mon Feb 18, 2019 11:41 am

I know but it seemed silly writing more character out ;-)
I should have mentioned it, but hey ho it was really a pointer for terms to search on.

Depending on the use here, the dictionary may be better as each hour can have a key for direct named access and no need to remember the location* in a list.
But that depends on the purpose, no doubt both will work fine.

*Ok quite easy when using a clock.

User avatar
bensimmo
Posts: 4072
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Introducing a large quantity of Variables

Mon Feb 18, 2019 11:47 am

As a side

What would the the methods for setting up a dictionary?
I don't know them to well.
Any comparable methods for not typing it all out?

Code: Select all

hours_of_day = { "hour0" : 0,
                 "hour1" = 0,
                     ...
                 "hour23" = 0 }

Return to “Python”