randint(a,b)包括 [a,b]中随机, 包含a,b

  range(n)= 0,1,2,3....n-1

  chr() 数字转字符:

    chr(65) 得到 :A

  ord()字符转数字:

     ord('a') 得到:97

ys = [random.randint(1,2) for i in range(10)] #产生1~2
print(ys) x = range(5)
print(list(x)) #0,1,3,4 

  

随机生成一些字符,并存入list里面:

import random
def getRandCharList(n, f):
# n为所需随机序列的长度,f=1为只要大写,f=2为大写加小写,f=3为再加上数字 a = [ chr(i+65) for i in range(26)] #'A'-'Z'的顺序序列
b = [ chr(i+97) for i in range(26)] #'a'-'z'的顺序序列
c = [ i for i in range(10)] #0-9的顺序序列
c = a + b + c
if f == 1:
rt = [ c[random.randint(0,25)] for i in range(n)]
elif f == 2:
rt = [ c[random.randint(0,51)] for i in range(n)]
else:
rt = [ c[random.randint(0,61)] for i in range(n)]
return rt rt = getRandCharList(100,3)
print(rt)

统计一片英文文章里面的字母出现次数:

def tongji(path):
# 传入文件所在地址,统计文件内大小写字母的个数,按次数降序返回list
with open(path, 'rb') as f:
str = f.read().decode('utf-8') d = [ chr(i+65) for i in range(26)] #'A'-'Z'的顺序序列
x = [ chr(i+97) for i in range(26)] #'a'-'z'的顺序序列
mychar = x + d
dist = {}
for i in range(52):
dist[mychar[i]] = 0
for i in str:
if (i >= 'a' and i <= 'z') or (i >= 'A' and i <= 'Z'):
dist[i] = dist[i] + 1
dist = sorted(dist.items(), key = lambda x:x[1], reverse = True ) #sorted返回新对象
return dist #filepath = 'C:\\Users\\sss\\Desktop\\ys.txt';
filepath = 'ys.txt'
tj = tongji(filepath);
#print(tj)
ct = 0
for k,v in tj:
if v > 0:
ct = ct + 1
print(k,': ', v)
print("ct: ", ct)

  

字典排序:

sorted(dict.items(), key=lambda e:e[1], reverse=True)

Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。

语法
items()方法语法:

dict.items()

lambda:

lambda函数也叫匿名函数,即,函数没有具体的名称。先来看一个最简单例子:

def f(x):
return x**2
print f(4)

Python中使用lambda的话,写成这样

g = lambda x : x**2
print g(4)

Python sorted() 函数:

sorted() 函数对所有可迭代的对象进行排序操作。

sort 与 sorted 区别:

sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作

17-list,字典使用练习的更多相关文章

  1. 【python cookbook】【数据结构与算法】17.从字典中提取子集

    问题:想创建一个字典,其本身是另一个字典的子集 解决方案:利用字典推导式(dictionary comprehension)可轻松解决 # example of extracting a subset ...

  2. #11 Python字典

    前言 前两节介绍了Python列表和字符串的相关用法,这两种数据类型都是有序的数据类型,所以它们可以通过索引来访问内部元素.本文将记录一种无序的数据类型——字典! 一.字典与列表和字符串的区别 字典是 ...

  3. 7、python中的字典

    字典是python内置的一种无序.可变的数据结构. 字典也叫哈希表.什么是哈希表?哈希表就是会对表中的键(key)执行哈希计算,并根据计算结果在内存中分配一个区域来储存该键所对应的值(value).这 ...

  4. python3笔记-字典

    5 1 # 创建字典 6 2 d=dict(name='lily',age=18,phone='') 7 3 print(d) 4 # {'name': 'lily', 'age': 18, 'pho ...

  5. 一鼓作气 博客--第三篇 note3

    1 推荐读书消费者行为学 -商业的本质,APP得到,5分钟商学院 2定义字典 dic={'name':haibao,'age':18} 3字典的基本操作--查询 dic={'name':'haibao ...

  6. Python2.2-原理之类型和运算

    此节来自于<Python学习手册第四版>第二部分 一.Python对象类型(第4章) 1. Python可以分解成模块.语句.表达式以及对象:1.程序由模块构成:2.模块包含语句:3.语句 ...

  7. python3.5.1语法

    1.print (变量名)  print("字符串") 2.a=1 id(a)返回a在内存中的地址 3.可以用table弹出提示  #coding:utf-8 4.输入3/2 结果 ...

  8. 你可能不知道的 30 个 Python 语言的特点技巧

        列表按难度排序,常用的语言特征和技巧放在前面. 1.1   分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...

  9. 你可能不知道的30个Python语言的特点技巧

    1 介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等), ...

  10. 30 个 Python 语言的特点技巧

    1   介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等 ...

随机推荐

  1. angular的require模块的使用($requireProvider的作用)

      今天我们学习一下angular的另一个几乎被忽略的模块angular-require 先给出链接地址(git:)   https://github.com/Treri/angular-requir ...

  2. eclipse导出jar,再转换为exe可执行程序

    转自: https://blog.csdn.net/mommomm/article/details/8227876 若只想知道如何把jar转换成exe,直接看第四步即可. 一.导出jar文件: 选中你 ...

  3. 排序 第K大等问题总结

    在公司面试时,当场写排序比较多,虽然都是老掉牙的问题,还是要好好准备下 快速排序,以第一个元素为关键词比较,每次比较结束,关键词都会去到最终位置上 //7 3 2 9 8 3 4 6 //7 3 2 ...

  4. python中高阶函数学习笔记

    什么是高阶函数 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数 def fun(x, y, f): print f(x), f(y) fun ...

  5. Java类型强制转换

    int intA = 10; String StrB = "12"; int c = Integer.parseInt(StrB); // 把String转换成int String ...

  6. SharedPreference工具类

    public class SPUtils { /** * 保存在手机里的SP文件名 */ public static final String FILE_NAME = "my_sp" ...

  7. configure: error: mcrypt.h not found. Please reinstall libmcrypt.

    编译出现错误: configure: error: mcrypt.h not found. Please reinstall libmcrypt. 解决方法: yum install -y libmc ...

  8. 关于select的一个死循环

    #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/sel ...

  9. java mybatisGenerator with velocity

    mybatisGenerator + velocity 模板生成dao+ mapper,并将mysql命名规范的table name + column -> java命名规范的 Class na ...

  10. IDA Pro 权威指南学习笔记(九) - 数据搜索

    Search -> Next Code 命令将光标移动到下一个包含指令的位置 Jump -> Jump to Function 命令可以打开所有函数,可以迅速选择一个函数并导航到该函数所在 ...