免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4107 | 回复: 3
打印 上一主题 下一主题

python里的单个下划线 “_” 有何用处 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-29 15:57 |只看该作者 |倒序浏览
  1. import random


  2. articles = ["the", "a", "another", "her", "his"]
  3. subjects = ["cat", "dog", "horse", "man", "woman", "boy", "girl"]
  4. verbs = ["sang", "ran", "jumped", "said", "fought", "swam", "saw",
  5.          "heard", "felt", "slept", "hopped", "hoped", "cried",
  6.          "laughed", "walked"]
  7. adverbs = ["loudly", "quietly", "quickly", "slowly", "well", "badly",
  8.            "rudely", "politely"]

  9. for _ in [1, 2, 3, 4, 5]:
  10.     article = random.choice(articles)
  11.     subject = random.choice(subjects)
  12.     verb = random.choice(verbs)
  13.     if random.randint(0, 1) == 0:
  14.         print(article, subject, verb)
  15.     else:
  16.         adverb = random.choice(adverbs)
  17.         print(article, subject, verb, adverb)
  18. ~                                                                  
复制代码
请教,这个for语句里的下划线的作用

论坛徽章:
0
2 [报告]
发表于 2011-06-29 16:51 |只看该作者
应该是一个变量吧.

论坛徽章:
0
3 [报告]
发表于 2011-06-30 03:21 |只看该作者
一般用作你不关心其具体值的变量。

论坛徽章:
0
4 [报告]
发表于 2011-06-30 09:24 |只看该作者
谢谢,我最初奇怪的是,怎么在循环体里没有使用这个变量。明白了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP