博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习--第三天
阅读量:3937 次
发布时间:2019-05-23

本文共 2712 字,大约阅读时间需要 9 分钟。

问题一、生成随机数(指定范围和产生个数)。

def RandomNumber(length, num):    tmp = [i + 1 for i in range(num)]    random.shuffle(tmp)    i = 0    list1 = []    while i < length:        list1.append(tmp[i])        i = i + 1    list1.sort()    print(list1)RandomNumber(5, 35)RandomNumber(2, 12)

运行结果:

"F:\Python 3.8.0\python.exe"[4, 6, 9, 21, 22][1, 10]进程已结束,退出代码 0

问题二、请编写用例并加以说明,解释Python中深拷贝与浅拷贝的区别。

1. 赋值其实只是传递对象引用,引用对象id是一样的。2. 浅拷贝是指拷贝的只是原始对象元素的引用,换句话说,浅拷贝产生的对象本身是新的,但是它的内容不是新的,只是对原对象的一个引用。3. 深拷贝是指完全拷贝原始对象,而且产生的对象是新的,并且不受其他引用对象的操作影响。
from copy import copy, deepcopya = [1,2,3,['a','b','c']]b = a #都不是新的c = copy(a) #内容不是新的d = deepcopy(a) #都是新的print(a)print(b)print(c)print(d)#用is 判断两个标识符是不是引用自一个对象if (a is b):    print("1 - a 和 b 有相同的标识")else:    print("1 - a 和 b 没有相同的标识")if (a is c):    print("3 - a 和 c 有相同的标识")else:    print("3 - a 和 c 没有相同的标识")if (a is d):    print("5 - a 和 d 有相同的标识")else:    print("5 - a 和 d 没有相同的标识")a.append(9)#在a中添加个9a[3].append('d')#在a中添加个dprint(a)print(b)print(c)print(d)

运行结果:

"F:\Python 3.8.0\python.exe" [1, 2, 3, ['a', 'b', 'c']][1, 2, 3, ['a', 'b', 'c']][1, 2, 3, ['a', 'b', 'c']][1, 2, 3, ['a', 'b', 'c']]1 - a 和 b 有相同的标识3 - a 和 c 没有相同的标识5 - a 和 d 没有相同的标识[1, 2, 3, ['a', 'b', 'c', 'd'], 9][1, 2, 3, ['a', 'b', 'c', 'd'], 9][1, 2, 3, ['a', 'b', 'c', 'd']][1, 2, 3, ['a', 'b', 'c']]进程已结束,退出代码 0

问题三、定义一个整型数组,并将指定个数的元素翻转到该数组的尾部。

例如:arr = [1,2,3,4,5,6] 将长度为6的数组arr的前面2个元素   翻转到数组尾部。   得到: arr = [3,4,5,6,1,2]
def rverse(arr, start, end):    while (start < end):        temp = arr[start]        # temp=1        arr[start] = arr[end]    # arr[start]=2        arr[end] = temp          # arr[end]=1        start += 1        end = end - 1def exchange(arr, sub):    n = len(arr)-1    rverse(arr,0,sub-1)#把前面sub个交换顺序    rverse(arr,sub,n)#把后面n-sub个交换顺序    rverse(arr,0,n)#所有的交换顺序arr = [1, 2, 3, 4, 5, 6]exchange(arr, 2)print(arr)

运行结果:

"F:\Python 3.8.0\python.exe"[3, 4, 5, 6, 1, 2]进程已结束,退出代码 0

问题四、编写程序实现给定一个字符串,然后判断指定的子字符串是否存在于该字符串中。

str="Man is the master of his destiny."if "is" in str:    print("is存在")else:    print("is不存在")if str.find("word") >=0:    print("word存在")else:    print("word不存在")

运行结果:

"F:\Python 3.8.0\python.exe" is存在word不存在进程已结束,退出代码 0

问题五、编写程序实现给定一个字典,计算所有键值为数字值的乘积。

def is_number(s):    try:         float(s)        return True    except ValueError:          pass      try:        import unicodedata          unicodedata.numeric(s)          return True    except (TypeError, ValueError):        pass    return Falsemy_dictionary= {
'data1': 10, 'data2': -5, 'data3': 2,'data4':"adda",}result = 1for key in my_dictionary: if is_number(my_dictionary[key]) is True: #判断是否为数字 result = result * my_dictionary[key]print(result)

运行结果:

"F:\Python 3.8.0\python.exe" -100进程已结束,退出代码 0

转载地址:http://zzywi.baihongyu.com/

你可能感兴趣的文章
sql常用命令
查看>>
CloudStack云基础架构的一些概念
查看>>
在centos7里安装zabbix3.4
查看>>
cloudstack搭建
查看>>
docker-compose使用
查看>>
springboot多个项目部署在tomcat服务器上的shiro的session污染问题
查看>>
mysql插入数据避免重复(Replace,IGNORE,on duplicate key update)
查看>>
mysql索引选择及优化
查看>>
MySQL数据类型、选择与优化
查看>>
Springboot系列(一)同属性名多对象处理
查看>>
mysql主从同步(复制)canal跨机房同步
查看>>
优秀开源项目(持续更新)
查看>>
SpringMVC项目报javax.validation.ValidationException: Unable to create a Configuration, because no Bean
查看>>
git常用命令
查看>>
vue实现动态添加行(并计算合计,小计)
查看>>
springboot工程在使用docker,nginx做转发时候提示400
查看>>
SpringCloud(Finchley.SR2) Eureka注册时候提示Cannot execute request on any known server
查看>>
SpringCloud (Finchley.SR2)整合hystrix dashboard 提示Unable to connect to Command Metric Stream.
查看>>
subclipse使用详解
查看>>
oracle分配权限 学习笔记--转载
查看>>