zoukankan      html  css  js  c++  java
  • Python词频统计

     

    一、目的

       掌握集合与字典的定义及其操作使用方法;

    二、内容

      1. 输入一个整数列表L,判断L中是否存在相同的数字:

      (1)若存在,输出YES,否则输出NO;

       代码:

    1 L=input('请输入一列整数,并以空格隔开:')
    2 lis=L.split(' ')
    3 if len(set(lis))!=len(lis):
    4     print('YES')
    5 else:
    6     print('NO')

       运行结果

        

      (2)若存在,输出YES,同时输出相同的数字;否则输出NO。

        代码:

     1 L=input('请输入一列整数,并以空格隔开:')
     2 lis=L.split(' ')
     3 if len(set(lis))!=len(lis):
     4     print('YES')
     5     for i in range(len(lis)-1):
     6         for j in range(i+1,len(lis)):
     7             if lis[i]==lis[j]:
     8                 print(lis[i])
     9 else:
    10     print('NO')

           运行结果:

       

     2. 从英文字母(区分大小写)和 0~9数字组成的列表中随机生成10个6位数的密码。

      代码:

    1 from random import sample
    2 s=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u",
    3    "v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
    4    "Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
    5 for i in range(10):
    6     answer = sample(s,6)
    7     for j in answer:
    8         print(j,end='')
    9     print('
    ')

       运行结果:

       

  • 相关阅读:
    Spring中文文档
    学装饰器之前必须要了解的四点
    三元运算符
    functools 中的 reduce 函数基本写法
    filter 函数基本写法
    map 函数基本写法
    迭代器和可迭代对象区别
    斐波那契数列进一步讨论性能
    无论传入什么数据都转换为列表
    将每一个分隔开的字符的首字母大写
  • 原文地址:https://www.cnblogs.com/ynly/p/12843736.html
Copyright © 2011-2022 走看看