#!/usr/bin/env python
# -*- coding:utf-8 -*- # 1.看代码写结果
'''
v1 = [1, 2, 3, 4, 5]
v2 = [v1, v1, v1]
v1.append(6) # 内部修改,都改变
print(v1) # [1, 2, 3, 4, 5,6]
print(v2) # v2 = [[1, 2, 3, 4, 5,6], [1, 2, 3, 4, 5,6], [1, 2, 3, 4, 5,6]]
''' # 2.看代码写结果
'''
v1 = [1, 2, 3, 4, 5]
v2 = [v1, v1, v1]
v2[1][0] = 111 # 内部修改 v1 = [111, 2, 3, 4, 5]
v2[2][0] = 222 # 内部修改 v1 = [222, 2, 3, 4, 5]
print(v1) # v1 = [222, 2, 3, 4, 5]
print(v2) # v2 = [[222, 2, 3, 4, 5], [222, 2, 3, 4, 5], [222, 2, 3, 4, 5]]
''' # 3.看代码写结果,并解释每一步的流程。
'''
v1 = [1,2,3,4,5,6,7,8,9]
v2 = {}
for item in v1:
if item < 6:
continue
if 'k1' in v2:
v2['k1'].append(item)
else:
v2['k1'] = [item] # v2 = {'k1':[6,7,8,9]}
print(v2)
''' # 4.简述深浅拷贝?
'''
深拷贝:拷贝嵌套层次中的所有可变类型.
浅拷贝:只拷贝第一层.
''' # 5.看代码写结果
'''
import copy v1 = "alex"
v2 = copy.copy(v1) # 浅拷贝 v2 = v1,指向同一内存地址
v3 = copy.deepcopy(v1) # 深拷贝 v3 = v1
print(v1 is v2) # True
print(v1 is v3) # True
''' # 6.看代码写结果
'''
import copy v1 = [1, 2, 3, 4, 5]
v2 = copy.copy(v1) # v2 == v1
v3 = copy.deepcopy(v1) # v3 == v1
print(v1 is v2) # False
print(v1 is v3) # False
''' # 7.看代码写结果
'''
import copy v1 = [1, 2, 3, 4, 5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1[0] is v2[0]) # True
print(v1[0] is v3[0]) # True
print(v2[0] is v3[0]) # True
''' # 8.看代码写结果
'''
import copy v1 = [1, 2, 3, 4, 5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1[0] is v2[0]) # True
print(v1[0] is v3[0]) # True
print(v2[0] is v3[0]) # True
''' # 9.看代码写结果
'''
import copy v1 = [1, 2, 3, {"name": '武沛齐', "numbers": [7, 77, 88]}, 4, 5]
v2 = copy.copy(v1)
print(v1 is v2) # False
print(v1[0] is v2[0]) # True
print(v1[3] is v2[3]) # True
print(v1[3]['name'] is v2[3]['name']) # True
print(v1[3]['numbers'] is v2[3]['numbers']) # True
print(v1[3]['numbers'][1] is v2[3]['numbers'][1]) # True
''' # 10.看代码写结果
'''
import copy v1 = [1, 2, 3, {"name": '武沛齐', "numbers": [7, 77, 88]}, 4, 5]
v2 = copy.deepcopy(v1)
print(v1 is v2) # False
print(v1[0] is v2[0]) # True
print(v1[3] is v2[3]) # False
print(v1[3]['name'] is v2[3]['name']) # True
print(v1[3]['numbers'] is v2[3]['numbers']) # False
print(v1[3]['numbers'][1] is v2[3]['numbers'][1]) # True
''' # 11.简述文件操作的打开模式
'''
r,只读模式,无文件报错.
w,只写模式,写入会先清空并覆盖原内容,无文件新建.
a,追加模式,无文件新建,在文件最后追加内容.
r+,读写模式,从光标位置读取,seek()定位光标,写入可能会覆盖原内容.
w+,写读模式,写入前先清空原内容,读取需要seek()定位光标.
a+,追加写读,根据光标位置读取,写入在最后.
''' # 12.请将info中的值使用 "_" 拼接起来并写入到文件 "readme.txt" 文件中。
'''
info = ['骗子,','不是','说','只有','10','个题吗?']
s = '_'.join(info)
f = open('readme.txt',mode='w',encoding='utf-8')
f.write(s)
f.close()
''' # 13.请将info中的值使用 "_" 拼接起来并写入到文件 "readme.txt" 文件中。
'''
info = ['骗子,','不是','说','只有',10,'个题吗?']
info[4] = str(info[4])
s = '_'.join(info)
f = open('readme.txt',mode='w',encoding='utf-8')
f.write(s)
f.close()
''' # 14.请将info中的所有键 使用 "_" 拼接起来并写入到文件 "readme.txt" 文件中。
# info = {'name': '骗子', 'age': 18, 'gender': '性别'}
# 1. 请将info中的所有键 使用 "_" 拼接起来并写入到文件 "readme.txt" 文件中。
'''
s = '_'.join(info)
f = open('readme.txt',mode='w',encoding='utf-8')
f.write(s)
f.close()
'''
# 2. 请将info中的所有值 使用 "_" 拼接起来并写入到文件 "readme.txt" 文件中。
'''
lst = list(info.values())
lst[1] = str(lst[1])
s = '_'.join(lst)
f = open('readme.txt', mode='w', encoding='utf-8')
f.write(s)
f.close()
'''
# 3. 请将info中的所有键和值按照 "键|值,键|值,键|值" 拼接起来并写入到文件 "readme.txt" 文件中。
'''
info = {'name': '骗子', 'age': 18, 'gender': '性别'}
s = ''
info['age'] = str(info['age'])
for k,v in info.items():
a = k +'|' + v + ','
s += a
f = open('readme.txt',mode='w',encoding='utf-8')
f.write(s[0:-1])
f.close()
''' # 15.写代码
# 要求:
# 如文件 data.txt 中有内容如下:
# wupeiqi|oldboy|wupeiqi@xx.com
# alex|oldboy|66s@xx.com
# xxxx|oldboy|yuy@xx.com
# 请用代码实现读入文件的内容,并构造成如下格式:
# info = [
# {'name':'wupeiqi','pwd':'oldboy','email':'wupeiqi@xx.com'},
# {'name':'alex','pwd':'oldboy','email':'66s@xx.com'},
# {'name':'xxxx','pwd':'oldboy','email':'yuy@xx.com'},
# ]
'''
info = []
f = open('data.txt',mode='r',encoding='utf-8')
for i in f.readlines():
dic = {}
new_i = i.strip()
new2_i = new_i.split('|')
dic['name'] = new2_i[0]
dic['pwd'] = new2_i[1]
dic['email'] = new2_i[2]
info.append(dic)
print(info)
f.close()
'''

oldboy s21day07(深浅拷贝及文件操作)的更多相关文章

  1. day03深浅拷贝、文件操作和函数初识

    一.赋值.浅拷贝与深拷贝 直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法, ...

  2. Python 深浅copy 和文件操作

    深浅copy 1,先看赋值运算. l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barry', ...

  3. day07 深浅拷贝

    今日概要 深浅拷贝(重点) 文件操作 详细内容 直接赋值: 直接将对象的引用赋值给另一个对象 v1=1000 v2=v1 #v1 v2指向同一个内存地址 print(id(v1),id(v2))#相等 ...

  4. 知识点补充 set 深浅拷贝

    一 对前面知识点的补充 1.str中的join()方法是将列表转换成字符串 lst = ["韩雪","赵丽颖","黄渤","李连杰 ...

  5. python03-break、continue、for循环、数据bytes类型、字符串与字节的关系、变量指向与深浅拷贝、set集合、文件操作

    目录: 1.break.continue 2.for循环 3.数据bytes类型 4.字符串与字节的关系 5.变量指向与深浅拷贝 6.set集合 7.文件操作 一.break.continue bre ...

  6. Learn day3 深浅拷贝/格式化/字符串/列表/字典/集合/文件操作

    1. pass break continue # ### pass break continue # (1) pass 过 """如果代码块当中,什么也不写,用pass来 ...

  7. python基础之 编码进阶,文件操作和深浅copy

    1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码.即先将其他编码的字符串解码(decode)成unicode,再从unic ...

  8. python基础 (编码进阶,文件操作和深浅copy)

    1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码. 即先将其他编码的字符串解码(decode)成unicode,再从uni ...

  9. python基础知识-7-内存、深浅、文件操作

    python其他知识目录 1.一些对内存深入理解的案例 以下列举列表,列表/字典/集合这些可变类型都是一样的原理 变量是个地址,指向存储数据的内存空间的地址,它的实质就相当于c语言里的指针.变量和数据 ...

随机推荐

  1. vue 首页问题

    (现在其实处于不知道自己不知道状态,前端其实很多东东,不信弄个微博试试,还有那些概念的to thi tha) 1.压缩 一般 vue-cli已经压缩了 比如js 的,一般4M多压缩到 1M,还有css ...

  2. USING KERBEROS

    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/managing_smart_cards/u ...

  3. 文本分类实战(二)—— textCNN 模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  4. AI Conditional GAN

    Conditional GAN 参考链接: https://arxiv.org/pdf/1611.07004v1.pdf

  5. vs2015第二次装安装不能选择路径问题解决方法

    vs2015卸载后注册表还会存在vs2015的信息,下次安装的时候会读注册表里面记录的路径,不能自己选择路径. 解决方法: 1.在vs安装文件的路径打开命令,shift+鼠标右键 2.输入命令:cn_ ...

  6. 终于有人把“TCC分布式事务”实现原理讲明白了!

    之前网上看到很多写分布式事务的文章,不过大多都是将分布式事务各种技术方案简单介绍一下.很多朋友看了还是不知道分布式事务到底怎么回事,在项目里到底如何使用. 所以这篇文章,就用大白话+手工绘图,并结合一 ...

  7. Scrapy中选择器的用法

    官方文档:https://doc.scrapy.org/en/latest/topics/selectors.html Using selectors Constructing selectors R ...

  8. threejs 初识

    用于展示3D动效,就是 跟拍电影一样,需要有3大模块:scene,camera,renderer. scene:场景,用于放置用到的模型. camera:摄像机,拍电影似的,得有个摄像机. rende ...

  9. echarts报表显示%+没有0

    function showTablegroup(page) { var series; $.ajax({ type:'post', url:"<%=basePath%>flowA ...

  10. windows平台上用python 远程线程注入,执行shellcode

    // 转自: https://blog.csdn.net/Jailman/article/details/77573990import sys import psutil import ctypes ...