python面试的100题(7)

8.将字符串 "k:1 |k1:2|k2:3|k3:4",处理成字典 {k:1,k1:2,...}

str1 = "k:1|k1:2|k2:3|k3:4"
def str2dict(str1):
    dict1 = {}
    for iterms in str1.split('|'):
        key,value = iterms.split(':')
        dict1[key] = value
    return dict1
#字典推导式
d = {k:int(v) for t in str1.split("|") for k, v in (t.split(":"), )}

9.请按alist中元素的age由大到小排序

alist = [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}]
def sort_by_age(list1):
    return sorted(alist,key=lambda x:x['age'],reverse=True)

10.下面代码的输出结果将是什么?

list = ['a','b','c','d','e']
print(list[10:])

结果为:[]

11.写一个列表生成式,产生一个公差为11的等差数列

print([x*11 for x in range(10)])

结果为:[0, 11, 22, 33, 44, 55, 66, 77, 88, 99]

12.给定两个列表,怎么找出他们相同的元素和不同的元素?

list1 = [1,2,3]
list2 = [3,4,5]
set1 = set(list1)
set2 = set(list2)
print(set1 & set2)
print(set1 ^ set2)

思考:用与来表示相同的元素,不同的元素用或

结果为:

{3}
{1, 2, 4, 5}

13.请写出一段python代码实现删除list里面的重复元素?

l1 = ['b','c','d','c','a','a']
l2 = list(set(l1))
print(l2)

用list类的sort方法:

l1 = ['b','c','d','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print(l2)

也可以这样写:

l1 = ['b','c','d','c','a','a']
l2 = sorted(set(l1),key=l1.index)
print(l2)

也可以用遍历:

l1 = ['b','c','d','c','a','a']
l2 = []
for i in l1:
    if not i in l2:
        l2.append(i)
print(l2)

结果为:['b', 'c', 'd', 'a']

set类型

set 和 dict 类似,也是一组 key 的集合,但是不存储 value. 由于 key  不重复,所以,在 set 中, 没有重复的 key 集合是可变类型

集合的创建

# 第一种方式创建 set 类型
>>> print(type(set1), set1)
<class 'set'> {1, 3, 6, 'z', 'a', 'b'}
 
# 第二种方式创建 set 类型
>>> set2 = set(['z', 'a', 'b', 3, 6, 1])
>>> print(type(set2), set2)
<class 'set'> {1, 3, 6, 'z', 'a', 'b'}
结果为:<class 'set'> {'b', 1, 3, 6, 'a', 'z'}
# 第三种方式创建 set 类型 >>> set3 = set('hello') >>> print(type(set3), set3) <class 'set'> {'o', 'e', 'l', 'h'}
结果为:<class 'set'> {'l', 'o', 'h', 'e'}

set工厂函数

(1)add(self, *args, **kwargs)

  新增一个元素到集合

set1 = {'a', 'z', 'b', 4, 6, 1}
set1.add(8)
set1.add('hello')
print(set1)

结果为:{'b', 1, 4, 'hello', 6, 8, 'a', 'z'}

(2) clear()
  清空所有集合元素

set1 = {'a', 'z', 'b', 4, 6, 1}
set1.clear()
print(set1)

结果为:set()

(3)copy()
    拷贝整个集合并赋值给变量

set1 = {'a', 'z', 'b', 4, 6, 1}
set2 =set1.copy()
print(set2)

结果为:{'b', 1, 'a', 4, 6, 'z'}

(4)pop()
    随机删除集合中一个元素,可以通过变量来获取删除的元素

set1 = {'a', 'z', 'b', 4, 6, 1}
ys = set1.pop()
print('set1集合:', set1)
print('删除的元素:', ys)

结果为:

set1集合: {1, 4, 6, 'a', 'z'}
删除的元素: b

(5)remove(self, *args, **kwargs)
    删除集合中指定的元素,如果该集合内没有该元素就报错

set1 = {'a', 'z', 'b', 4, 6, 1}
set1.remove('a')
print(set1)
set1.remove('x')
print(set1)

结果为:

{'b', 1, 4, 6, 'z'}
Traceback (most recent call last):

File "<ipython-input-11-daa8bf30f7b8>", line 4, in <module>
set1.remove('x')

KeyError: 'x'

(6)discard(self, *args, **kwargs)
    删除集合中指定的元素,如果该集合内没有该元素也不会报错

set1 = {'a', 'z', 'b', 4, 6, 1}
set1.discard('a')
print(set1)
set1.discard('y')
print(set1)

结果为:

{'b', 1, 4, 6, 'z'}
{'b', 1, 4, 6, 'z'}

pop() 、remove() 、 discard() 三个集合删除函数比较:
    pop() 随机删除集合中一个元素remove() 删除集合中指定的元素,如果集合中没有指定的元素,程序报错!
    discard() 删除集合中指定的元素,如果集合中没有指定的元素,程序正常运行。

(7) intersection  & :交集; union | :并集合; difference - : 差集

set1 = {'a', 'b', 'x', 'y'}
set2 = {'i', 'j', 'b', 'a'}
 
# 交集
print(set1 & set2)
print(set1.intersection(set2))
结果为:

{'b', 'a'}
{'b', 'a'}

# 并集

print(set1 | set2)
print(set1.union(set2))
结果为:

{'b', 'x', 'y', 'i', 'j', 'a'}
{'b', 'x', 'y', 'i', 'j', 'a'}

# 差集

print(set1 - set2)
print(set1.difference(set2))
print(set2 - set1)
print(set2.difference(set1))
结果为:

{'x', 'y'}
{'x', 'y'}
{'j', 'i'}
{'j', 'i'}

(8)difference_update ()
    求差集,并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}
set2 = {'i', 'j', 'b', 'a'}
set1.difference_update(set2)
print(set1)

结果为:{'x', 'y'}

(9)intersection_update()
    求交集,并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}
set2 = {'i', 'j', 'b', 'a'}
 
set1.intersection_update(set2)
print(set1)

结果为:{'b', 'a'}

(10)symmetric_difference()  和 ^ 符号效果一样
    求交叉补集

set1 = {'a', 'b', 'x', 'y'}
set2 = {'i', 'j', 'b', 'a'}
 
print('symmetric_difference:', set1.symmetric_difference(set2))
print('^:', set1 ^ set2)

结果为:

symmetric_difference: {'x', 'y', 'i', 'j'}
^: {'x', 'y', 'i', 'j'}

(11)symmetric_difference_update()
  求交叉补集并赋值给源集合

set1 = {'a', 'b', 'x', 'y'}
set2 = {'i', 'j', 'b', 'a'}
 
set1.symmetric_difference_update(set2)
print(set1)

结果为:{'x', 'y', 'i', 'j'}

(12)update()
    更新集合,参数为可迭代对象

set1 = {'a', 'b', 'x', 'y'}
 
set1.update(('hello', 'world'))
print(set1)

结果为:{'world', 'b', 'x', 'y', 'hello', 'a'}

add() 和 update() 比较:
    add(): 只能添加一个元素到集合
    update(): 可以添加多个元素到集合,参数为 iterable

使用 frozenset 定义不可变集合

s = frozenset('hello')
print(s)
 

结果为:frozenset({'l', 'o', 'h', 'e'})

使用 frozenset 定义的集合,没有 add 或者 pop 等方法

参考地址:https://www.cnblogs.com/hukey/p/9242339.html

14.给定两个list A,B ,请用找出A,B中相同与不同的元素

A,B 中相同元素: print(set(A)&set(B))
A,B 中不同元素:  print(set(A)^set(B))
原文地址:https://www.cnblogs.com/Fiona-Y/p/10567255.html