[Tutor] Dictionaries of Tuples of Strings

Adam Pridgen atpridgen at mail.utexas.edu
Fri Feb 23 21:15:04 CET 2007


Hello,

When I have a tuple with a single string in a dictionary entry and
try to iterate over the tuple and it breaks the string into individual
characters.  Is this supposed to be happening?

This problem is a little tricky to explain so I have included the
output and the corresponding example code.  In the example I have a
dictionary with tuples of strings as the values.  If I have more than
one tuple, I am ok and the value is not broken into individual
characters (look at the "type="in the code below.  If I have only one
string in the tuple value, then the string is broken into individual
characters.  Thanks again,

Adam.

Output:

name="years_of_newbness" type="xs:hexbinary"
name="years_of_newbness" type="xs:integer"

name="newb" type="x"
name="newb" type="s"
name="newb" type=":"
name="newb" type="t"
name="newb" type="r"
name="newb" type="i"
name="newb" type="n"
name="newb" type="g"

Example.py:

import sets

def Foo (elements=None):
    e_types = set()
    str = ""
    for k in elements:
        e_types.clear()
        for j in TYPES_PYTHON_TO_XML[k[1]]:# <-- Problem arises here
            # a tuple with a single string in dictionary is broken apart
            if j in e_types: continue
            str += CreateXMLElement(k[0], j)+"\n"
            e_types.add(j)
    print str

def CreateXMLComplex(name, elements):
    return XML_COMPLEX_STMNT%(name, elements)

def CreateXMLElement(name, type):
    return XML_ELEMENT_STMNT%(name, type)

XML_ELEMENT_STMNT = "name=\"%s\" type=\"%s\" "

TYPES_PYTHON_TO_XML = {
           'int':('xs:hexbinary','xs:integer'),
           'str':("xs:string")
           }
Foo([("years_of_newbness", "int")]) # <-- Works, more than one element
in the tuple
Foo([('newb', 'str')])# <-- Fails, only one element in the tuple
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Example.py
Type: text/x-python
Size: 893 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20070223/94e43070/attachment.py 


More information about the Tutor mailing list