-1

Is it possible to create a dictionary like this?

data = {(136-00000-0001 : 072-00000-0001,072-00000-0002,072-00000-0003),
        (136-00000-0002 : 072-00000-0002,072-00000-0003,072-00000-0004)}

text = input("Put your 072-XXXXX-XXXX number in here: ")
wrapper =[]

for number in text:
 if number in data.keys()
  wrapper.append(data[number]

 for w in wrapper:
  print(w, end=" ")

I have a wrapper called 136-xxxxx-xxxx where 3*072-xxxxx-xxxx are in it. All I want to do is, asking for a 072-xxxxx-xxxx number and the program is giving me the Wrapper number of it back.

For example:

  • asking for 072-00000-0004
  • answer -> it´s inside 136-00000-0002

The only way I know is, for example that morse thing, where morse = {"a":".-", "b":"-...", but is there a way like this for my example at the top?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yierith
  • 141
  • 1
  • 5

2 Answers2

2

You would need to store multiple values for your keys in a list,tuple etc.. You cannot have more than one value for a key.

data = {"136-00000-0001" : ["072-00000-0001","072-00000-0002","072-00000-0003"],"136-00000-0002" : ["072-00000-0002","072-00000-0003","072-00000-0004"]}
print ( data.values())

[['072-00000-0001', '072-00000-0002', '072-00000-0003'], ['072-00000-0002', '072-00000-0003', '072-00000-0004']]

To append to a keys values:

data["136-00000-0001"].append("whatever")

Check if value is in the dict and print the key,value pair.:

In [5]: for k,v in data.items():
            if "072-00000-0004" in v:
                print (k,v)
   ...:         
136-00000-0002 ['072-00000-0002', '072-00000-0003', '072-00000-0004']

Use strings if you want use the format 072-00000-0002 will give an error SyntaxError: invalid token .

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

In Python 2.x:

>>> data = {"136-00000-0001":["072-00000-0001","072-00000-0002","072-00000-0003"],"136-00000-0002":["072-00000-0002","072-00000-0003","072-00000-0004"]}
>>> text = raw_input("Put your 072-XXXXX-XXXX number in here: ")
Put your 072-XXXXX-XXXX number in here: 072-00000-0004
>>> for d in data:
...     if text in data[d]:
...         print d
136-00000-0002

And for Python 3 you have to use input() instead of raw_input() and print() as a function.

>>> data = {"136-00000-0001":["072-00000-0001","072-00000-0002","072-00000-0003"],"136-00000-0002":["072-00000-0002","072-00000-0003","072-00000-0004"]}
>>> text = input("Put your 072-XXXXX-XXXX number in here: ")
Put your 072-XXXXX-XXXX number in here: 072-00000-0004
>>> for d in data:
...     if text in data[d]:
...         print(d)
136-00000-0002
orakaro
  • 1,468
  • 1
  • 9
  • 9
  • why would you have to enter a string in quotes? – Padraic Cunningham Jun 24 '14 at 09:24
  • because python will treat `text` as a number – orakaro Jun 24 '14 at 09:25
  • I did and I also added the sample. – orakaro Jun 24 '14 at 09:33
  • 1
    for python 3 that the OP is obviously using? – Padraic Cunningham Jun 24 '14 at 09:34
  • In where the OP is telling us that he is using Python 3 ? And I don't think that will become a reason for a downvote. – orakaro Jun 24 '14 at 09:37
  • Your answer is wrong, you have not taken into account that the OP is using python 3 syntax so along with incorrect instruction your code will not run because it has syntax errors. – Padraic Cunningham Jun 24 '14 at 09:39
  • I **did know** that Python 2 code will not run in Python 3. But the OP's syntax is wrong with either Python 2 or 3, so I don't get the point why are you saying that he is using python 3 ? – orakaro Jun 24 '14 at 09:45
  • print is a function in python 3 and a statement in python 2, the OP is using it as a function and I doubt he/she is using `from future import..` – Padraic Cunningham Jun 24 '14 at 10:13
  • Okay, the OP is using print as a function and _maybe_ he/she wrote it in Python 3. So while your answer also contains `print data.values()`, I can say that your answer is wrong, incorrect instruction and will not run :) Your reason for a down vote is not clear enough. – orakaro Jun 24 '14 at 10:22
  • if the OP was using python 2.7, he/she should use raw_input. You like the OP don't seem to fully understand what you are doing so for future instances when anybody searches SO and comes on this question,your answer would not be a good example of how to proceed with the code. Is that clear enough? – Padraic Cunningham Jun 24 '14 at 11:01
  • (1) In this case and in python 2.7 he/she can use input() as well as raw_input() (2) I do understand what i'm doing and I don't have to mention it to you while answering to the OP. (3) You pointed that maybe the OP used Python 3, but gave him a answer which will not run in Python 3. (4) At last, I admit that my answer may won't correct for Python 3 and not specify it was my fault. See answer updated. – orakaro Jun 24 '14 at 11:47
  • mine was a typo, your errors are from not knowing what you are doing, you just put "may wrong with Python 3", it is certainly wrong syntax for python 3 and why on earth would you use input, have a look at this question and maybe you will learn something: http://stackoverflow.com/questions/4960208/python-2-7-getting-user-input-and-manipulating-as-string-without-quotations. – Padraic Cunningham Jun 24 '14 at 11:53