Python论坛  - 讨论区

标题:[python-chinese] 【django】如何使用random这个内建过滤器

2008年01月04日 星期五 11:23

jt.wang wjtbox01在gmail.com
星期五 一月 4 11:23:10 HKT 2008

djangodoc˵ÓÐrandomÕâÑùµÄ¶«Î÷£¬×÷ÓÃÊÇ·µ»ØËæ»úµÄ´ÓÁбíÖзµ»ØÒ»¸öÔªËØ£¬µ«
ÊÇûÓоÙÀý£¬¸ã²»Çå³þÔõôÓã¬Ò²²»ÖªµÀ·µ»Ø½á¹ûÊÇʲôÑù¡£
ÓÐÓùýµÄ´ïÈËÂé·³½²½²randomÔõôʹÓõġ£Ð»Ð»¡£
-------------- 下一部分 --------------
Ò»¸ö·ÇÎı¾¸½¼þ±»Çå³ý...
·¢ÐÅÈË: %(who)s
Ö÷Ìâ: %(subject)s
ÈÕÆÚ: %(date)s
´óС: 259
Url: http://python.cn/pipermail/python-chinese/attachments/20080104/5cafd75a/attachment.vcf 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 14:35

Jiahua Huang jhuangjiahua在gmail.com
星期五 一月 4 14:35:36 HKT 2008

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

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

On Jan 4, 2008 11:23 AM, jt.wang <wjtbox01 at gmail.com> wrote:
> djangodoc说有random这样的东西,作用是返回随机的从列表中返回一个元素,但
> 是没有举例,搞不清楚怎么用,也不知道返回结果是什么样。
> 有用过的达人麻烦讲讲random怎么使用的。谢谢。
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 14:46

Jiahua Huang jhuangjiahua在gmail.com
星期五 一月 4 14:46:08 HKT 2008

>>> 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'

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 15:35

jt.wang wjtbox01在gmail.com
星期五 一月 4 15:35:52 HKT 2008

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 python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用 
完发现不是。它的用法是

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

-------------- 下一部分 --------------
一个非文本附件被清除...
发信人: %(who)s
主题: %(subject)s
日期: %(date)s
大小: 259
Url: http://python.cn/pipermail/python-chinese/attachments/20080104/863e79f4/attachment.vcf 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 15:45

Moore Fu chengmu.fu在gmail.com
星期五 一月 4 15:45:21 HKT 2008

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

在08-1-4,jt.wang <wjtbox01在gmail.com> 写道:
>
> 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 python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
> 完发现不是。它的用法是
>
> {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
> 谢谢。
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
----------------------------------------------------------
傅成木
移动电话:13625003893
msn:fuchengmu在hotmail.com
mail:   cmfu在longtop.com
-----------------------------------------------------------
Moore Fu
Mobile:86-136-25003893.
msn:fuchengmu在hotmail.com
mail:   cmfu在longtop.com
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20080104/3ce6c673/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 15:49

Jiahua Huang jhuangjiahua在gmail.com
星期五 一月 4 15:49:19 HKT 2008

你多取几次就是啦

>>> 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 <wjtbox01 at gmail.com> wrote:
> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
> 完发现不是。它的用法是
>
>  {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
> 谢谢。
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 16:01

jt.wang wjtbox01在gmail.com
星期五 一月 4 16:01:30 HKT 2008

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 <wjtbox01在gmail.com> wrote:
>   
>> 其实我想问的是怎么从结果集中随机取值。问之前以为random是干这个的,结果用
>> 完发现不是。它的用法是
>>
>>  {{ items|random }},然后只返回一个值(我需要返回若干个值)。不过现在已经解决了。
>> 谢谢。
>>
>>
>>     
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
方法一点不高明:写个 sql的查询放到里面。Select * From  表 Order By 
Rand() Limit 10
哈哈。
-------------- 下一部分 --------------
一个非文本附件被清除...
发信人: %(who)s
主题: %(subject)s
日期: %(date)s
大小: 259
Url: http://python.cn/pipermail/python-chinese/attachments/20080104/b643462b/attachment.vcf 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月04日 星期五 17:59

@@ askfor在gmail.com
星期五 一月 4 17:59:41 HKT 2008

Rand() Õâ¸öÊÇmysqlÌṩµÄ£¿

On 1/4/08, jt.wang <wjtbox01在gmail.com> 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 <wjtbox01在gmail.com> wrote:
> >
> >> ÆäʵÎÒÏëÎʵÄÊÇÔõô´Ó½á¹û¼¯ÖÐËæ»úÈ¡Öµ¡£ÎÊ֮ǰÒÔΪrandomÊǸÉÕâ¸öµÄ£¬½á¹ûÓÃ
> >> Íê·¢ÏÖ²»ÊÇ¡£ËüµÄÓ÷¨ÊÇ
> >>
> >>  {{ items|random }}£¬È»ºóÖ»·µ»ØÒ»¸öÖµ£¨ÎÒÐèÒª·µ»ØÈô¸É¸öÖµ£©¡£²»¹ýÏÖÔÚÒѾ­½â¾öÁË¡£
> >> лл¡£
> >>
> >>
> >>
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> ·½·¨Ò»µã²»¸ßÃ÷£ºÐ´¸ö sqlµÄ²éѯ·Åµ½ÀïÃæ¡£Select * From  ±í Order By
> Rand() Limit 10
> ¹þ¹þ¡£
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080104/11eadf71/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月07日 星期一 12:42

Vingel Lai mail在vingel.com
星期一 一月 7 12:42:47 HKT 2008

ÎÒͨ³£¶¼ÊÇ Ê¹Óà Item.objects.all()[:10].order_by("?")µÄ¡£

²»¹ý¾Ý˵Õâ¸ö»áµ¼Ö¾޴óµÄÐÔÄÜËðʧ¡£ËùÒÔºÜÉÙÓá£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080107/27c3b27b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月07日 星期一 12:47

Jiahua Huang jhuangjiahua在gmail.com
星期一 一月 7 12:47:30 HKT 2008

恩,还划不来

On Jan 7, 2008 12:42 PM, Vingel Lai <mail at vingel.com> wrote:
> 不过据说这个会导致巨大的性能损失。所以很少用。
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号