python请输入第一个数请输入第二个数_2019下半年Python高频面试题,第六弹

class Solution:

def twoSum(self,nums,target):

"""

:type nums: List[int]

:type target: int

:rtype: List[int]

"""

d = {}

size = 0

while size

给列表中的字典排序:假设有如下list对象,alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}],将alist中的元素按照age从大到小排序 alist=[{“name”:“a”,“age”:20},{“name”:“b”,“age”:30},{“name”:“c”,“age”:25}]alist_sort = sorted(alist,key=lambda e: e.__getitem__('age'),reverse=True)

二. python代码实现删除一个list里面的重复元素def distFunc1(a):

"""使用集合去重"""

a = list(set(a))

print(a)

def distFunc2(a):

"""将一个列表的数据取出放到另一个列表中,中间作判断"""

list = []

for i in a:

if i not in list:

list.append(i)

#如果需要排序的话用sort

list.sort()

print(list)

def distFunc3(a):

"""使用字典"""

b = {}

b = b.fromkeys(a)

c = list(b.keys())

print(c)

if __name__ == "__main__":

a = [1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]

distFunc1(a)

distFunc2(a)

distFunc3(a)

三. 统计一个文本中单词频次最高的10个单词?import re

# 方法一

def test(filepath):

distone = {}

with open(filepath) as f:

for line in f:

line = re.sub("\W+", " ", line)

lineone = line.split()

for keyone in lineone:

if not distone.get(keyone):

distone[keyone] = 1

else:

distone[keyone] += 1

num_ten = sorted(distone.items(), key=lambda x:x[1], reverse=True)[:10]

num_ten =[x[0] for x in num_ten]

return num_ten

# 方法二

# 使用 built-in 的 Counter 里面的 most_common

import re

from collections import Counter

def test2(filepath):

with open(filepath) as f:

return list(map(lambda c: c[0], Counter(re.sub("\W+", " ", f.read()).split()).most_common(10)))

四. 请写出一个函数满足以下条件

该函数的输入是一个仅包含数字的list,输出一个新的list,其中每一个元素要满足以下条件:

1、该元素是偶数

2、该元素在原list中是在偶数的位置(index是偶数)def num_list(num):

return [i for i in num if i %2 ==0 and num.index(i)%2==0]

num = [0,1,2,3,4,5,6,7,8,9,10]

result = num_list(num)

print(result)

五. 使用单一的列表生成式来产生一个新的列表

该列表只包含满足以下条件的值,元素为原始列表中偶数切片list_data = [1,2,5,8,10,3,18,6,20]

res = [x for x in list_data[::2] if x %2 ==0]

print(res)

六. 用一行代码生成[1,4,9,16,25,36,49,64,81,100][x * x for x in range(1,11)]

七. 输入某年某月某日,判断这一天是这一年的第几天?import datetime

y = int(input("请输入4位数字的年份:"))

m = int(input("请输入月份:"))

d = int(input("请输入是哪一天"))

targetDay = datetime.date(y,m,d)

dayCount = targetDay - datetime.date(targetDay.year -1,12,31)

print("%s是 %s年的第%s天。"%(targetDay,y,dayCount.days))

八. 两个有序列表,l1,l2,对这两个列表进行合并不可使用extenddef loop_merge_sort(l1,l2):

tmp = []

while len(l1)>0 and len(l2)>0:

if l1[0] 0:

tmp.append(l1[0])

del l1[0]

while len(l2)>0:

tmp.append(l2[0])

del l2[0]

return tmp

九. 给定一个任意长度数组,实现一个函数

让所有奇数都在偶数前面,而且奇数升序排列,偶数降序排序,如字符串’1982376455’,变成’1355798642’# 方法一

def func1(l):

if isinstance(l, str):

l = [int(i) for i in l]

l.sort(reverse=True)

for i in range(len(l)):

if l[i] % 2 > 0:

l.insert(0, l.pop(i))

print(''.join(str(e) for e in l))

# 方法二

def func2(l):

print("".join(sorted(l, key=lambda x: int(x) % 2 == 0 and 20 - int(x) or int(x))))

十. 写一个函数找出一个整数数组中,第二大的数def find_second_large_num(num_list):

"""

找出数组第2大的数字

"""

# 方法一

# 直接排序,输出倒数第二个数即可

tmp_list = sorted(num_list)

print("方法一\nSecond_large_num is :", tmp_list[-2])

# 方法二

# 设置两个标志位一个存储最大数一个存储次大数

# two 存储次大值,one 存储最大值,遍历一次数组即可,先判断是否大于 one,若大于将 one 的值给 two,将 num_list[i] 的值给 one,否则比较是否大于two,若大于直接将 num_list[i] 的值给two,否则pass

one = num_list[0]

two = num_list[0]

for i in range(1, len(num_list)):

if num_list[i] > one:

two = one

one = num_list[i]

elif num_list[i] > two:

two = num_list[i]

print("方法二\nSecond_large_num is :", two)

# 方法三

# 用 reduce 与逻辑符号 (and, or)

# 基本思路与方法二一样,但是不需要用 if 进行判断。

from functools import reduce

num = reduce(lambda ot, x: ot[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值