Comparing lists
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course to iterate throug the lists and compare each of the element, but is there a more efficient way? Thanks in advance! -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne -- http://mail.python.org/mailman/listinfo/python-list
Dictionary to tuple
I have a dictionary, and I want to convert it to a tuple,
that is, I want each key - value pair in the dictionary
to be a tuple in a tuple.
If this is the dictionary {1:'one',2:'two',3:'three'},
then I want this to be the resulting tuple:
((1,'one'),(2,'two'),(3,'three')).
I have been trying for quite some time now, but I do not
get the result as I want it. Can this be done, or is it not
possible?
I must also add that I'm new to Python.
Thanks in advance.
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
--
http://mail.python.org/mailman/listinfo/python-list
find a specified dictionary in a list
I have this list:
[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]
All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?
Thanks in advance
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
--
http://mail.python.org/mailman/listinfo/python-list
Re: find a specified dictionary in a list
On 2005-07-22, John Machin <[EMAIL PROTECTED]> wrote:
> Odd-R. wrote:
>> I have this list:
>>
>> [{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]
>>
>> All the dictionaries of this list are of the same form, and all the oids
>> are distinct. If I have an oid and the list, how is the simplest way of
>> getting the dictionary that holds this oid?
>>
>
> Something like this:
>
> def oidfinder(an_oid, the_list):
> for d in the_list:
> if d['oid'] == an_oid:
> return d
> return None
> # These are not the oids you are looking for.
Thank you for your help, but I was hoping for an even simpler
solution, as I am suppose to use it in a
http://mail.python.org/mailman/listinfo/python-list
regex problem
Input is a string of four digit sequences, possibly
separated by a -, for instance like this
"1234,-,4567,"
My regular expression is like this:
rx1=re.compile(r"""\A(\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,)*\Z""")
When running rx1.findall("1234,-,4567,")
I only get the last match as the result. Isn't
findall suppose to return all the matches?
Thanks in advance.
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
--
http://mail.python.org/mailman/listinfo/python-list
Re: regex problem
On 2005-07-26, Duncan Booth <[EMAIL PROTECTED]> wrote:
rx1=re.compile(r"""\b\d{4}(?:-\d{4})?,""")
rx1.findall("1234,-,4567,")
> ['1234,', '-,', '4567,']
Thanks all for good advice. However this last expression
also matches the first four digits when the input is more
than four digits. To resolve this problem, I first do a
match of this,
regex=re.compile(r"""\A(\b\d{4},|\d{4}-\d{4},)*(\b\d{4}|\d{4}-\d{4})\Z""")
If this turns out ok, I do a find all with your expression, and then I get
the desired result.
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
--
http://mail.python.org/mailman/listinfo/python-list
SOAPpy and http authentication
I use the following piece of code to contact a webservice, and read a wsdl file. from SOAPpy import WSDL from SOAPpy import URLopener url= ' http://someserver/somewebservice url1 = URLopener.URLopener(username='user',passwd='pass') server=WSDL.Proxy(url1.open(url)) This yields no errors, and everyting is fine. However, when I start calling the actual methods of this webservice, I get this error SOAPpy.Errors.HTTPError: How can I avoid this? Thanks in advance for all help! -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne -- http://mail.python.org/mailman/listinfo/python-list
searching a list of dictionaries for an element in a list.
If input is ['red','blue'],
list1 is [ {'primarycolor':'red', 'secondarycolor':'burgundee'},
{'primarycolor':'red', 'secondarycolor':'wine'},
{'primarycolor':'yellow','secondarycolor':'plain'},
{'primarycolor':'blue','secondarycolor':'ocean'}]
I want to search list1, and the result should be all dictionaries where
primarycolor is in input. I can do this using a double for-loop, but is
there a more efficent way?
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
--
http://mail.python.org/mailman/listinfo/python-list
error when parsing xml
I use xml.dom.minidom to parse some xml, but when input contains some specific caracters(æ, ø and å), I get an UnicodeEncodeError, like this: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 604: ordinal not in range(128). How can I avoid this error? All help much appreciated! -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne -- http://mail.python.org/mailman/listinfo/python-list
Re: error when parsing xml
On 2005-09-05, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Odd-R. wrote: > >> I use xml.dom.minidom to parse some xml, but when input >< contains some specific caracters(æ, ø and å), I get an >> UnicodeEncodeError, like this: >> >> UnicodeEncodeError: 'ascii' codec can't encode character >> u'\xe6' in position 604: ordinal not in range(128). >> >> How can I avoid this error? > > if you're getting this on the way in, something is broken (posting a short > self-contained test program will help us figure out what's wrong). This is retrieved through a webservice and stored in a variable test ]> æøå printing this out yields no problems, so the trouble seems to be when executing the following: doc = minidom.parseString(test) Then I get this error: File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\minidom.py", line 1918, in parseString return expatbuilder.parseString(string) File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\expatbuilder.py", line 940, in parseString return builder.parseString(string) File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\expatbuilder.py", line 223, in parseString parser.Parse(string, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 157-159: ordinal not in range(128) In the top of the file, I have put this statement: # -*- coding: utf-8 -*- > if you're getting this on the way out, the problem is that you're trying to > print Unicode strings to an ASCII device. use the "encode" method to > convert the string to the encoding you want to use, or use codecs.open > to open an encoded stream and print via that one instead. Can you give an example of how this is done? Thanks again for all help! -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne -- http://mail.python.org/mailman/listinfo/python-list
Exception-handling
I have come over a strange problem regarding exceptions This is my code: try: #some operation except Exception, info: #some message except: #?? When executing my code, I get to the last block here. This I find rather strange, because I thought Exception would catch all exceptions. But this is obviously not the case here. What is this, and how can I get a hold of what causes the exception? Thanks! -- Har du et kjøleskap, har du en TV så har du alt du trenger for å leve -Jokke & Valentinerne -- http://mail.python.org/mailman/listinfo/python-list
