-2

I'm designing a very simple database for products I have in a mock warehouse.

There is a code (the key), a description (value), number of units available per pack (value), individual prices (value) and the total cost of the pack (value):

Product Number | Description | Pack | Individual Cost | Total 
01234567 | Black Ballpoint Pen | 4 | 0.50 | 2.00
12345670 | Football | 2 | 4.50 | 9.00

The product is the key and the other attributes are all values.

I tried to use a dictionary structure but that didn't work as it didn't have a fixed number of fields.

I haven't seen an example as such that involves more than one field. I haven't seen an array or dictionary example that has more than one field to store all of this data.

I want it something like:

{01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:(Football, 2, 4.50, 9.00)}

Is this possible?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
vik1245
  • 546
  • 2
  • 9
  • 26
  • 2
    You could have found out for yourself in less time than in took to write up the question: yes, a dictionary where the values are tuples is possible, as the interpreter would happily have told you. – jonrsharpe Jul 03 '16 at 09:30
  • @jonrsharpe I tried that but it came to no avail – vik1245 Jul 03 '16 at 09:31
  • Well then show a [mcve] of that code. – jonrsharpe Jul 03 '16 at 09:31
  • When you say it "came to no avail," do you mean that you tried that exact snippet, including leading zeros and a variable name instead of a string for the "Football" key? – TigerhawkT3 Jul 03 '16 at 09:36
  • Indeed @TigerhawkT3 – vik1245 Jul 03 '16 at 09:36
  • @jonrsharpe the snippet is in the question body – vik1245 Jul 03 '16 at 09:37
  • 1
    In that case, are you using Python 2 so that a leading zero is an octal number instead of an immediate syntax error? And is the variable `Football` defined anywhere, or is it supposed to be a string that you forgot to enclose in quotes? – TigerhawkT3 Jul 03 '16 at 09:39
  • @TigerhawkT3 is right, I'd expect `NameError: name 'Football' is not defined` in 2.x (because... well, it's not, is it?) and `SyntaxError: invalid token` in 3.x (see e.g. http://stackoverflow.com/q/13013638/3001761). But neither of these is: 1. mentioned in the question; or 2. anything to do with whether that kind of data structure is valid. – jonrsharpe Jul 03 '16 at 09:40
  • There is no leading-zero error in this code. – Burhan Khalid Jul 03 '16 at 09:53

1 Answers1

0

I want it something like:

{01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:(Football,2, 4.50, 9.00)}

Did you try it? All you have to do is change Football to "Football", and it works as expected:

>>> i = {01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:("Football",2, 4.50, 9.00)}
>>> i[01234567]
('Black Ballpoint Pen', 4, 0.5, 2.0)

A dictionary can hold any kind of value - lists, strings, numbers, tuples and even other dictionaries.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284