python面试,日更

l1 = [11, 2, 3, 22, 2, 4, 11, 3]
去重并保持原来顺序
# 集合方法
l2 = list(set(l1))
l2.sort(key=l1.index) # 按照l1索引排序
print(l2)

# 列表
l3 = []
for item in l1:
    if item not in l3:
        l3.append(item)
print(l3)
答案
l4 = [
    {"name":"lmj11","age":11},
    {"name":"lmj44","age":44},
    {"name":"lmj33","age":33},
    {"name":"lmj77","age":77},
    {"name":"lmj66","age":66},
    {"name":"lmj55","age":55},
]
# 使用lambda表达式
l4.sort(key= lambda x:x["age"]) # 方式一
l4=sorted(l4,key=lambda x:x["age"]) # 方式二
print(l4)

# 其他方式
# def tmp(x):
#     return x["age"]
# l4.sort(key=tmp)
# print(l4)
答案

 

def extend_list(v, li=[]):
    li.append(v)
    return li

list1 = extend_list(10)
list2 = extend_list(123, [])
list3 = extend_list('a')

print(list1)
print(list2)
print(list3)

print(list1 is list3)

答案:

[10,'a']
[123,]
[10,'a']
答案

 

问以下代码的输出结果是什么?
list1 = ["a", "b", "c", "d", "e"]
print(list1[10:])

答案:

# 列表的切片操作
print(list1[3:]) # ['d', 'e']
print(list1[-3:]) # ['c', 'd', 'e']
print(list1[:-3]) # ['a', 'b']
print(list1[:3]) # ['a', 'b', 'c']
答案

 

list1=["a","b","c","d","e"]

# 实现打乱列表顺序
# 分别在原有列表和新建列表基础上

#请分别阐述潜复制,深复制和切片复制列表的特点
import random

random.shuffle(list1)
print(list1) # ['c', 'a', 'b', 'd', 'e']

# 以下给出该函数的官方解释,即打乱原本表格,返回None
help(random.shuffle)
"""
shuffle(x, random=None) method of random.Random instance
    Shuffle list x in place, and return None.
    
    Optional argument random is a 0-argument function returning a
    random float in [0.0, 1.0); if it is the default None, the
    standard random.random will be used.
"""

  • 直接赋值:其实就是对象的引用(别名)。

  • 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象。(字典或多层列表可明显看出区别)

  • 深拷贝(deepcopy): copy 模块的 deepcopy 方法,开辟了一块新的内存空间,完全拷贝了父对象及其子对象。

 

list1=["a","b","c","d","e"]

# 列表潜拷贝
list2=list1.copy()
print(list1) # ['a', 'b', 'c', 'd', 'e', 'fff']
print(list2) # ['a', 'b', 'c', 'd', 'e']
list1.append("fff")
print(list1) # ['a', 'b', 'c', 'd', 'e', 'fff']
print(list2) # ['a', 'b', 'c', 'd', 'e']

print('分割线'.center(50,'=')) # 列表深拷贝 import copy list3 = copy.deepcopy(list1) print(list1) # ['a', 'b', 'c', 'd', 'e', 'fff'] print(list3) # ['a', 'b', 'c', 'd', 'e', 'fff'] list1.append("ggg") print(list1) # ['a', 'b', 'c', 'd', 'e', 'fff', 'ggg'] print(list3) # ['a', 'b', 'c', 'd', 'e', 'fff']
# 列表换成多层时的潜拷贝
list2=list1.copy()
print(list1) # ['a', 'b', 'c', 'd', 'e', 'fff']
print(list2) # ['a', 'b', 'c', 'd', 'e']
list1[3].append("fff")
print(list1) # ['a', 'b', 'c', ['d', 'e', 'fff']]
print(list2) # ['a', 'b', 'c', ['d', 'e', 'fff']]
综上所述,可理解对象A浅拷贝B,就是A对B的一种内存地址的引用,无论B引用的值如何改变,A均可以取得最新值

 

 """
问:执行完下面的代码后,  l,m的内容分别是什么?
"""


def func(m):
    for k,v in m.items():
        m[k+2] = v+2


m = {1: 2, 3: 4}
l = m  # 浅拷贝
l[9] = 10
func(l)
m[7] = 8


print("l:", l)
print("m:", m) 
# python3.6 中会报错
# 在迭代一个列表或字典的时候,不能修改列表或字典的大小!
答案

 

# 将list1 拼接成字符串
list1 = [11,22,33]
new_list = "".join([str(i) for i in list1])
print(new_list)
答案

 

Python中字符串的格式化(%s,format),一般用哪种?为什么?

 

编写Python脚本,分析xx.log文件,按域名统计访问次数

xx.log文件内容如下:
https://www.sogo.com/ale.html
https://www.qq.com/3asd.html
https://www.sogo.com/teoans.html
https://www.bilibili.com/2
https://www.sogo.com/asd_sa.html
https://y.qq.com/
https://www.bilibili.com/1
https://dig.chouti.com/
https://www.bilibili.com/imd.html
https://www.bilibili.com/


输出:
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com
import re
from collections import Counter

# 1. 读取出内容
with open('yuming.log','r',encoding='utf-8')as f:
    data = f.read()

# 2. 取域名信息
res = re.findall(r'https://(.*?)/.*?',data)
# print(res)

# 3. 统计
dic = {}
for i in res:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

# 4. 排序(二种方法)
# print(dic) # {'www.sogo.com': 3, 'www.qq.com': 1, 'www.bilibili.com': 4, 'y.qq.com': 1, 'dig.chouti.com': 1}
# dic = Counter(res)
# print(dic) # Counter({'www.bilibili.com': 4, 'www.sogo.com': 3, 'www.qq.com': 1, 'y.qq.com': 1, 'dig.chouti.com': 1})
# for k in dic.items():
#     print(k)
'''
('www.sogo.com', 3)
('www.qq.com', 1)
('www.bilibili.com', 4)
('y.qq.com', 1)
('dig.chouti.com', 1)
'''

# 方法一
# res2 = sorted(dic,key=lambda x:dic[x],reverse=True)
# print(res2) # ['www.bilibili.com', 'www.sogo.com', 'www.qq.com', 'y.qq.com', 'dig.chouti.com']
# for k in res2:
#     print(dic[k],k)

'''
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com

'''

# 方法二
# print(dic.items()) # dict_items([('www.sogo.com', 3), ('www.qq.com', 1), ('www.bilibili.com', 4), ('y.qq.com', 1), ('dig.chouti.com', 1)])
res2 = sorted(dic.items(), key=lambda x:x[1], reverse=True)
# print(res2) # [('www.bilibili.com', 4), ('www.sogo.com', 3), ('www.qq.com', 1), ('y.qq.com', 1), ('dig.chouti.com', 1)]
for k,v in res2:
    print(v,k)
'''
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com

'''
答案

 

 

 

 

待续。。。

posted @ 2018-05-28 09:54  MJay_Lee  阅读(291)  评论(0编辑  收藏  举报