【django】如何使用random这个内建过滤器?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

【django】如何使用random这个内建过滤器?

jt.wang
djangodoc说有random这样的东西,作用是返回随机的从列表中返回一个元素,但
是没有举例,搞不清楚怎么用,也不知道返回结果是什么样。
有用过的达人麻烦讲讲random怎么使用的。谢谢。

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

wjtbox01.vcf (356 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Jiahua Huang
这还需要用过么,
请执行 pydoc random

或者在 python/ipython 命令行执行
import random
help(random)

On Jan 4, 2008 11:23 AM, jt.wang <[hidden email]> wrote:
> djangodoc说有random这样的东西,作用是返回随机的从列表中返回一个元素,但
> 是没有举例,搞不清楚怎么用,也不知道返回结果是什么样。
> 有用过的达人麻烦讲讲random怎么使用的。谢谢。
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Jiahua Huang
In reply to this post by jt.wang
>>> import random
>>> jt = ['a', 'b', 'c', 'd', 'e']     # 这是个列表
>>> random.choice(jt)           # 随机从列表返回一个元素
'd'
>>> random.choice(jt)
'a'
>>> random.choice(jt)
'c'
>>> random.choice(jt)
'e'
>>> random.choice(jt)
'd'
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

jt.wang
Jiahua Huang 写道:

>>>> import random
>>>> jt = ['a', 'b', 'c', 'd', 'e']     # 这是个列表
>>>> random.choice(jt)           # 随机从列表返回一个元素
>>>>        
> 'd'
>  
>>>> random.choice(jt)
>>>>        
> 'a'
>  
>>>> random.choice(jt)
>>>>        
> 'c'
>  
>>>> random.choice(jt)
>>>>        
> 'e'
>  
>>>> random.choice(jt)
>>>>        
> 'd'
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
完发现不是。它的用法是

 {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
谢谢。


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

wjtbox01.vcf (356 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Moore Fu
呵呵,楼主如何解决的可以发出来大家参考下的说。

在08-1-4,jt.wang <[hidden email]> 写道:
Jiahua Huang 写道:

>>>> import random
>>>> jt = ['a', 'b', 'c', 'd', 'e']     # 这是个列表
>>>> random.choice(jt)           # 随机从列表返回一个元素
>>>>
> 'd'
>
>>>> random.choice(jt)
>>>>
> 'a'
>
>>>> random.choice(jt)
>>>>
> 'c'
>
>>>> random.choice(jt)
>>>>
> 'e'
>
>>>> random.choice(jt)
>>>>
> 'd'
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
完发现不是。它的用法是

{{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
谢谢。


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese




--
----------------------------------------------------------
傅成木
移动电话:13625003893
msn:[hidden email]
mail:   [hidden email]
-----------------------------------------------------------
Moore Fu
Mobile:86-136-25003893.
msn:[hidden email]
mail:   [hidden email]
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Jiahua Huang
In reply to this post by jt.wang
你多取几次就是啦

>>> jt = ['a', 'b', 'c', 'd', 'e']
>>> def jtchose(seq):
...     import random
...     iseq = set()
...     for i in range(random.randrange(1, len(seq))):
...         iseq.add(random.choice(seq))
...     return list(iseq)
...
>>> jtchose(jt)
['a', 'b', 'd']
>>> jtchose(jt)
['c', 'b', 'd']
>>> jtchose(jt)
['c']
>>> jtchose(jt)
['b', 'e']
>>> jtchose(jt)
['b']


On Jan 4, 2008 3:35 PM, jt.wang <[hidden email]> wrote:
> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
> 完发现不是。它的用法是
>
>  {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
> 谢谢。
>
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

jt.wang
Jiahua Huang 写道:

> 你多取几次就是啦
>
>  
>>>> jt = ['a', 'b', 'c', 'd', 'e']
>>>> def jtchose(seq):
>>>>        
> ...     import random
> ...     iseq = set()
> ...     for i in range(random.randrange(1, len(seq))):
> ...         iseq.add(random.choice(seq))
> ...     return list(iseq)
> ...
>  
>>>> jtchose(jt)
>>>>        
> ['a', 'b', 'd']
>  
>>>> jtchose(jt)
>>>>        
> ['c', 'b', 'd']
>  
>>>> jtchose(jt)
>>>>        
> ['c']
>  
>>>> jtchose(jt)
>>>>        
> ['b', 'e']
>  
>>>> jtchose(jt)
>>>>        
> ['b']
>
>
> On Jan 4, 2008 3:35 PM, jt.wang <[hidden email]> wrote:
>  
>> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
>> 完发现不是。它的用法是
>>
>>  {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
>> 谢谢。
>>
>>
>>    
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
方法一点不高明:写个 sql的查询放到里面。Select * From  表 Order By
Rand() Limit 10
哈哈。

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

wjtbox01.vcf (356 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

@@-2
Rand() 这个是mysql提供的?

On 1/4/08, jt.wang <[hidden email]> wrote:
Jiahua Huang 写道:

> 你多取几次就是啦
>
>
>>>> jt = ['a', 'b', 'c', 'd', 'e']
>>>> def jtchose(seq):
>>>>
> ...     import random
> ...     iseq = set()
> ...     for i in range(random.randrange(1, len(seq))):
> ...         iseq.add(random.choice (seq))
> ...     return list(iseq)
> ...
>
>>>> jtchose(jt)
>>>>
> ['a', 'b', 'd']
>
>>>> jtchose(jt)
>>>>
> ['c', 'b', 'd']

>
>>>> jtchose(jt)
>>>>
> ['c']
>
>>>> jtchose(jt)
>>>>
> ['b', 'e']
>
>>>> jtchose(jt)
>>>>
> ['b']
>
>
> On Jan 4, 2008 3:35 PM, jt.wang <[hidden email]> wrote:
>
>> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用

>> 完发现不是。它的用法是
>>
>>  {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
>> 谢谢。
>>
>>
>>
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
方法一点不高明:写个 sql的查询放到里面。Select * From  表 Order By
Rand() Limit 10
哈哈。

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Vingel Lai
我通常都是 使用 Item.objects.all()[:10].order_by("?")的。

不过据说这个会导致巨大的性能损失。所以很少用。

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 【django】如何使用random这个内建过滤器?

Jiahua Huang
恩,还划不来

On Jan 7, 2008 12:42 PM, Vingel Lai <[hidden email]> wrote:
> 不过据说这个会导致巨大的性能损失。所以很少用。
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese