how can i know if a python object have a attribute such as 'attr1'?

2010-01-23 Thread thinke365

for example, i may define a python class:
class A:
 def sayHello():
  print 'hello'

a = A()
a.attr1 = 'hello'
a.attr2 = 'bb'

b = A()
a.attr2 = 'aa'

how can i know whether an object have an attribute named attr1?

-- 
View this message in context: 
http://old.nabble.com/how-can-i-know-if-a-python-object-have-a-attribute-such-as-%27attr1%27--tp27286937p27286937.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


how to generate random numbers that satisfy certain distribution

2010-01-23 Thread thinke365

such as uniform distribution, Normal distribution or poisson distribution.
is there any package that can be used to generate such random numbers.

-- 
View this message in context: 
http://old.nabble.com/how-to-generate-random-numbers-that-satisfy-certain-distribution-tp27288180p27288180.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: how to generate random numbers that satisfy certain distribution

2010-01-23 Thread thinke365



Bugzilla from ra.ravi@gmail.com wrote:
> 
> On Jan 23, 10:37 pm, thinke365  wrote:
>> such as uniform distribution, Normal distribution or poisson
>> distribution.
>> is there any package that can be used to generate such random numbers.
>>
>> --
>> View this message in
>> context:http://old.nabble.com/how-to-generate-random-numbers-that-satisfy-cer...
>> Sent from the Python - python-list mailing list archive at Nabble.com.
> 
> Did you try random package?
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

of course i have tried random package, but can this package generate random
sequence that satisfy possion distribution , normal distribution and uniform
distribution 

-- 
View this message in context: 
http://old.nabble.com/how-to-generate-random-numbers-that-satisfy-certain-distribution-tp27288180p27288996.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


this customize sort did not work ,what's wrong?

2010-01-23 Thread thinke365

l = list()
l1 = list((1, 2, 3, 4))
l2 = list((1,2))
l3 = list((1, 2, 3, 4, 5))
l.append(l1)
l.append(l2)
l.append(l3)
print l

def sort_by_list(E1, E2):
print len(E1), len(E2)
return len(list(E1)) > len(list(E2))

l.sort(cmp=sort_by_list)
print l

output:
[[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
2 4
5 2
[[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]

the order of the elements in the list did not change!
-- 
View this message in context: 
http://old.nabble.com/this-customize-sort-did-not-work-%2Cwhat%27s-wrong--tp27289860p27289860.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: this customize sort did not work ,what's wrong?

2010-01-23 Thread thinke365

i mean the output i want is:
[ [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5]], that is sort according to the
length of the list element

thinke365 wrote:
> 
> l = list()
> l1 = list((1, 2, 3, 4))
> l2 = list((1,2))
> l3 = list((1, 2, 3, 4, 5))
> l.append(l1)
> l.append(l2)
> l.append(l3)
> print l
> 
> def sort_by_list(E1, E2):
> print len(E1), len(E2)
> return len(list(E1)) > len(list(E2))
> 
> l.sort(cmp=sort_by_list)
> print l
> 
> output:
> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
> 2 4
> 5 2
> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
> 
> the order of the elements in the list did not change!
> 

-- 
View this message in context: 
http://old.nabble.com/this-customize-sort-did-not-work-%2Cwhat%27s-wrong--tp27289860p27289922.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: this customize sort did not work ,what's wrong?

2010-01-23 Thread thinke365

jesus, now i fixed it, using odd lambda sort.
l.sort(lambda x,y: cmp(len(x), len(y)))
print l

BUT I AM STILL CONFUSED WHY COSTOMIZED SORT FAILED TO SORT AS IT IS 
PROGRAMMER!


thinke365 wrote:
> 
> i mean the output i want is:
> [ [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5]], that is sort according to the
> length of the list element
> 
> thinke365 wrote:
>> 
>> l = list()
>> l1 = list((1, 2, 3, 4))
>> l2 = list((1,2))
>> l3 = list((1, 2, 3, 4, 5))
>> l.append(l1)
>> l.append(l2)
>> l.append(l3)
>> print l
>> 
>> def sort_by_list(E1, E2):
>> print len(E1), len(E2)
>> return len(list(E1)) > len(list(E2))
>> 
>> l.sort(cmp=sort_by_list)
>> print l
>> 
>> output:
>> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
>> 2 4
>> 5 2
>> [[1, 2, 3, 4], [1, 2], [1, 2, 3, 4, 5]]
>> 
>> the order of the elements in the list did not change!
>> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/this-customize-sort-did-not-work-%2Cwhat%27s-wrong--tp27289860p27290014.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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