程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python面试题(十一)

发布于2019-07-21 16:25     阅读(2196)     评论(0)     点赞(50)     收藏(8)


1、Python中list、tuple、dict、set有什么区别,主要应用在什么样的场景?并用for语句分别进行遍历

 

定义:
list:链表,有序的项目, 通过索引进行查找,使用方括号”[]”;
tuple:元组,元组将多样的对象集合到一起,不能修改,通过索引进行查找, 使用括号”()”;
dict:字典,字典是一组键(key)和值(value)的组合,通过键(key)进行查找,没有顺序, 使用大括号”{}”;
set:集合,无序,元素只出现一次, 自动去重,使用”set([])”
应用场景:
list, 简单的数据集合,可以使用索引;
tuple, 把一些数据当做一个整体去使用,不能修改;
dict,使用键值和值进行关联的数据;
set,数据只出现一次,只关心数据是否出现, 不关心其位置;

test_list = [1, 2, 3, 4, 4]
test_tuple = (1, 2, 3, 4, 5)
test_dict = {'a': 1, 'b': 2}
test_set = {12, 4, 5, 6}
for items in test_list:
    print('list:', items)
for items in test_tuple:
    print('tuple:', items)

for key, value in test_dict.items():
    print('dict:', key, value)

for items in test_set:
    print('set:', items)
答案

 

2、Python中静态函数、类函数、成员函数的区别?各写出一个实例。

 

class Animal(object):
    planet = 'earth'

    def __init__(self,name):
        self.name = name

    @staticmethod
    def eat():
        print("An animal is eating.....")

    @classmethod
    def live_on(cls):
        print("The Animal live on earth!")

    def print_name(self):
        print('The name of animal is',self.name)
Cat = Animal('Cafe')

Animal.eat()
Cat.eat()

Animal.live_on()
Cat.live_on()

Cat.print_name()
答案

 

3、用Python语言写一个函数,输入一个字符串,返回倒序排列的结果:如:string_reverse('abcdefg'),返回'gfedcba'

 

def string_reverse(input_str):
    return input_str[::-1]


print(string_reverse('nice'))
答案

 

4、介绍一下Python的异常处理机制和自己开发过程中的体会

 

python主要是用try except语句来捕获异常
使用raise来引发异常
使用try...finally来处理(无论是否发生异常)都要处理的内容
assert来触发断言异常

个人感悟:
    1、触发异常,一定要带上完整的异常信息:raise MyException('Error message')
    2、创建一个异常模块,创建一些异常基类和子类,触发不同的异常
    3、除了特殊情况不要,捕捉所有的异常
    4、减少try except中代码,只对出现异常的语句进行处理
    5、捕捉异常尽量使用as语句
答案

 

5、jQuery中的$()是什么?网页上有5个<div>元素,如何使用jQuery来选择他们?

 

1、$()是一个标签选择器
2、$()可以是一个特定的DOM元素
3、$()是$(function)定义一个函数

$("div")
答案

 

6、写一个Bash Shell脚本来得到当前的日期、时间、用户名和当前文件目录

 

`date +%Y%m%d-%H-%M-%S`
`whoami`
`pwd`
答案

 

7、Django中使用memcached作为缓存的具体使用方法?优缺点说明?

 

1、在setting里配置cache
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
2、缓存的方法:
A.整个站点缓存:
    django.middleware.cache.UpdateCacheMiddleware(放在最前)
    django.middleware.cache.FetchFromCacheMiddleware(放在最后)   


B.视图函数装饰器缓存:
    from django.views.decorators.cache import cache_page
    @cache_page(60 * 15)
    def my_view(request):
    ....
    其中cache_page的参数为超时时间,单位为秒。
C.在函数中调用cache来缓存
    from django.core.cache import cache
    def heavy_view(request):
        cache_key = 'my_heavy_view_cache_key'
        cache_time = 1800 # time to live in seconds
        result = cache.get(cache_key)
        if not result:
            result = # some calculations here
            cache.set(cache_key, result, cache_time)
        return result

D.在url中配置缓存
    urlpatterns = ('',
        (r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)),
    

memcached将数据放在内存中,无法持久化,数据库宕机会导致数据的丢失
答案

 

8、给定一个红包的数额属组gifts以及它的大小n,请返回是否有某个金额出现的次数超过总红包数的一半。若存在返回该红包金额,不存在请返回0

 

def select_most_often_gift(gifts):
    gift_items = set(gifts)
    n = len(gifts)
    for gift in gift_items:
        num = gifts.count(gift)
        if num > n/2:
            return gift
    return 0

print(select_most_often_gift([2,3,6,2,24,5,56]))
答案

 



所属网站分类: 技术文章 > 博客

作者:玛利亚

链接:https://www.pythonheidong.com/blog/article/2094/3de0728322bda0bf12e2/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

50 0
收藏该文
已收藏

评论内容:(最多支持255个字符)