What are __slots__ used for?

2005-07-04 Thread Ric Da Force
I am a C# programmer and new to the language and I am trying to debug some 
code which uses this feature.  Can anyone elaborate on what it is and how it 
is used?

Regards,

Ric 


-- 
http://mail.python.org/mailman/listinfo/python-list


Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Hi all,

I have a dictionary containing about 300 items, some of the values being 
repeated.  Both keys and values are strings.  How can I turn this thing on 
its head so that we create a key based on each unique value and build the 
values based on the keys corresponding to the repeated values?

It is hard to explain but this is what I mean:

Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is 
not'}

I want this to return a new dict with string keys and lists containing the 
previous keys for repeated values.

NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}

I am still learning Python and have struggled with this for hours before 
deciding to go for help.  Unfortunately, I didn't really know how to search 
for this in google and decided to post it here.  I apologise if this is too 
basic for this newsgroup...

Ric 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Thank you guys! (Reinhold, Mark and Markus)  I must confess that I am 
absolutely awe struck at the power of this language!  There is no way in the 
world that I would have envisaged such simple and elegant solutions!!!

Reinhold, is your solution specific to 2.4?

Kind Regards,

Ric

"Reinhold Birkenfeld" <[EMAIL PROTECTED]> wrote in 
message news:[EMAIL PROTECTED]
> Mark Jackson wrote:
>> "Ric Da Force" <[EMAIL PROTECTED]> writes:
>>
>>> It is hard to explain but this is what I mean:
>>>
>>> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This 
>>> is
>>> not'}
>>>
>>> I want this to return a new dict with string keys and lists containing 
>>> the
>>> previous keys for repeated values.
>>>
>>> NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}
>>
>> NewDict = {}
>> for x in Dict.keys():
>> try:
>> NewDict[Dict[x]].append(x)
>> except KeyError:
>> NewDict[Dict[x]] = [x]
>
> Or, more up-to-date:
>
> NewDict = {}
> for key, val in Dict.iteritems():
>NewDict.setdefault(val, []).append(key)
>
> Reinhold 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
How does setdefault work exactly? I am looking in the docs and can't figure 
it out...
Ric

"Ric Da Force" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Thank you guys! (Reinhold, Mark and Markus)  I must confess that I am 
> absolutely awe struck at the power of this language!  There is no way in 
> the world that I would have envisaged such simple and elegant solutions!!!
>
> Reinhold, is your solution specific to 2.4?
>
> Kind Regards,
>
> Ric
>
> "Reinhold Birkenfeld" <[EMAIL PROTECTED]> wrote in 
> message news:[EMAIL PROTECTED]
>> Mark Jackson wrote:
>>> "Ric Da Force" <[EMAIL PROTECTED]> writes:
>>>
>>>> It is hard to explain but this is what I mean:
>>>>
>>>> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This 
>>>> is
>>>> not'}
>>>>
>>>> I want this to return a new dict with string keys and lists containing 
>>>> the
>>>> previous keys for repeated values.
>>>>
>>>> NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}
>>>
>>> NewDict = {}
>>> for x in Dict.keys():
>>> try:
>>> NewDict[Dict[x]].append(x)
>>> except KeyError:
>>> NewDict[Dict[x]] = [x]
>>
>> Or, more up-to-date:
>>
>> NewDict = {}
>> for key, val in Dict.iteritems():
>>NewDict.setdefault(val, []).append(key)
>>
>> Reinhold
>
> 


-- 
http://mail.python.org/mailman/listinfo/python-list


Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-11 Thread Ric Da Force
Hi,

I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
text is of fixed size, what is the easiest way to change this list so that 
it reads:
'C1, C2 and C3' regardless of the length of the string.

Regards and sorry for the newbie question,

Ric 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads'C1, C2 and C3'

2005-07-12 Thread Ric Da Force
Hi guys,

Thank you all for your input!  It was good to see so much convergence in the 
approach! Again, I think that it speaks loudly for the concise way of doing 
thins in Python... Anyway, I have typed in all of the solutions and have 
gained a great understanding of how to do this in future.

Thanks again!

Ric
"Brian van den Broek" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Ric Da Force said unto the world upon 12/07/2005 02:43:
>> Hi,
>>
>> I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
>> text is of fixed size, what is the easiest way to change this list so 
>> that it reads:
>> 'C1, C2 and C3' regardless of the length of the string.
>>
>> Regards and sorry for the newbie question,
>>
>> Ric
>
> Hi Ric,
>
> the rsplit method of strings should get you going:
>
> >>> data = "the first bit, then the second, finally the third"
> >>> chunks = data.rsplit(',', 1)
> >>> chunks
> ['the first bit, then the second', ' finally the third']
> >>>
>
> Best,
>
> Brian vdB
>
> 


-- 
http://mail.python.org/mailman/listinfo/python-list