python通用编程:一阶段习题

练习一、打印金字塔

level = int(input('请输入层数:'))
for i in range(1,level+1):
    for j in range(level-i):
        print(' ',end='')
    print('*'*(2*i-1))

在这里插入图片描述

练习二、嵌套列表去重

l = [

    {'name': 'albert', 'age': 18, 'sex': 'male'},

    {'name': 'james', 'age': 35, 'sex': 'male'},

    {'name': 'taylor', 'age': 25, 'sex': 'female'},

    {'name': 'albert', 'age': 18, 'sex': 'male'},

    {'name': 'albert', 'age': 18, 'sex': 'male'},

]

# print(set(l))  # 将报错:unhashable type: 'dict'

# 方式一
s = set()
l1 = []
for item in l:
    val = (item['name'], item['age'], item['sex'])
    if val not in s:
        s.add(val)
        l1.append(item)
print(l1)

# 方式二、定义函数,既可以针对可以hash类型又可以针对不可hash类型(下一阶段课程)
def func(items, key=None):
    s = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in s:
            s.add(val)
            yield item

print(list(func(l, key=lambda dic: (dic['name'], dic['age'], dic['sex']))))

练习三、统计单词个数

对如下字符串进行统计:

s = 'hello albert albert say hello world world'

# 方式一
list1 = s.split()
dict1 = {}
for item in list1:
    if item in dict1:
        dict1[item] += 1
    else:
        dict1[item] = 1
print(dict1)

# 方式二
dict2 = {}
words = s.split()
for word in words:
    dict2[word] = s.count(word)
    #或者
    #dict2[word] = words.count(word)
print(dict2)

# 方式三
dict3 = {}
words = s.split()
for word in words:
    dict3.setdefault(word, s.count(word))
    print(dict3)

# 方式四  
from collections import Counter
cnt = Counter()
s='hello albert albert say hello world world'
l=s.split(' ')
for word in l:
    cnt[word] += 1
print(dict(cnt))

练习四、不改变顺序地删除文件重复行

import os
res_list = []
with open('a.txt') as f1,open('aa.txt','w') as f2:
    for line in f1:
        content = line.strip()
        if content not in res_list:
            res_list.append(content)
    f2.writelines('\n'.join(res_list))
os.remove('a.txt')
os.rename('aa.txt','a.txt')

练习五、指定路径,拷贝文件

# 方式一
import sys
import shutil

# 把命令行中解释器后空格分割的所有参数都存成列表
paths = sys.argv
shutil.copyfile(paths[1], paths[2]

保存该文件,在cmd中输入如下命令,格式为:
python py文件路径 需拷贝文件路径 新路径

python C:\Users\Desktop\copy.py C:\Users\Desktop\cat.jpg D:\Files\cat.jpg
# 方式二,通过字节形式读写
import sys

list1 = sys.argv  

src_file_path = list1[1]
dst_file_path = list1[2]

with open(r'%s' % src_file_path, mode='rb') as src_f, \
        open(r'%s' % dst_file_path, mode='wb') as dst_f:
    for line in src_f:
        dst_f.write(line)

练习六、在文件中插入指定内容

# 方式一:
# 1、先把文件内容全部读入内存
# 2、然后在内存中完成修改
# 3、再把修改后的结果覆盖写入原文件
# 缺点:会在文件内容过大的情况下,占用过多的内存

with open('user.txt', mode='r', encoding='utf-8') as f:
    data = f.read()
    data = data.replace('马一特', '马一特[Albert]')

with open('user.txt', mode='w', encoding='utf-8') as f:
    f.write(data)

# 方式二:
# 以读的方式打开原文件,以写的方式打开一个新文件,一行一行的读入文件内容
import os
 
with open('user.txt', mode='rt', encoding='utf-8') as read_f, \
         open('user.txt.swap', mode='wt', encoding='utf-8') as write_f:
     for line in read_f:
         if '马一特' in line:
             line = line.replace('马一特', '马一特[Albert]')
    
        write_f.write(line)

os.remove('user.txt')
os.rename('user.txt.swap', 'user.txt')

# 方式三
import os
with open('aa.txt','r') as f1, open('bb.txt','w') as f2:
    content = f1.read()
    # 指定文本
    words = 'oppo'
    # 插值内容
    add = '[vivo]'
    pos = content.find(words)
    if pos != -1:
        content = content[:pos+len(words)] + add + content[pos+len(words):]
        f2.write(content) 
os.remove('aa.txt')
os.rename('bb.txt','aa.txt')

练习七、三级菜单

练习八、购物商城

见:
https://github.com/XJuly/Python_program

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值