面试总结

python问题: 主要是list操作

django问题:mtv的模式

linux问题: 命令 

 

1. list去重

a = [1,2,3,3,2]

b=[]

for i in a:

    if i not in b:

        b.append(i)


通过这几天的面试,正则表达式是常问到的,今天把正则表达式复习了一下,总结一下:

* 必配前面0次或多次

+匹配前面1次或多次

?匹配前面0次或一次,非贪婪

 {m}匹配前面m次

[ ] 取[]中的一个字符,对单个字符来说

 \d匹配数字【0-9】,\D匹配非数字

\w匹配数字字符字符【A-Za-z0-9】,\W相反

\s匹配空白字符

\b匹配单词边界(和\B 相反)

|  管道 “或” 的关系*

. 匹配任意单个字符(除\n)

prthon中的re模块

1. re.match()

从头匹配,返回一个对象

>>>m = re.match('hello', 'hello world')  #可以匹配,返回一个对象

>>>m.group() # ‘hello’

>>>m = re.match('world', 'hello world') # m为None

2.re.search()

返回一个对象,第一个匹配的,没有为None

>>>m = re.search('world', 'hello world') #可以匹配,返回一个对象

3.re.findall()

找到每个出现的部分,返回一个列表。

>>>re .findall('car','carry scary')

['car', 'car','car']

4. re.sub(),subn()

搜索和替换,sub()返回一个字符串, subn()返回元组(字符串,替换的次数)

>>>re.sub('[ae]', 'X', 'abcdef')

‘XbedXf’

>>>re.subn('[ae]','X','abcdef')

('XbcdXf', 2)

5.re.split()

分割,返回列表

>>>re.split(':', 'str1:str2:str3')

['str1', 'str2','str3']

\b  和  \B

re.search('^The', 'The end.') #匹配

re.search('^The', 'end The.') #不匹配,不在开头

re.search(r'\bThe', 'end The.') #匹配,只匹配在词的边界的

re.search(r'\bThe', 'endThe') #不匹配

re.search(r'\BThe', 'endThe') #匹配

原始字符 r' '

是为解决正则表达式和ASCII冲突的问题

例如:\b 在正则中是特殊字符,在ASCII中是退格

re.match('\bthe', 'the end') #不会匹配,\b为退格建

re.match(r'\bthe', 'the end') #匹配,\b为匹配单词边界

面试题:

1. 字符串a = ‘abcedfghigk’, 利用正则表达式三个就加个‘,’,  a = ‘abc,def,。。。。’

from re import findall

m = findall('\w{1,3}',a)

a = ','.join(m)

2.  s = "[lol]你好,帮我把这些markup清掉,[smile]。谢谢!",用正则表达式清除字符串中[]和其中的内容。

re.sub('\[\w*\]','',s)

3. 打印1到100的数,如果能被3正除,就打印Fizz,如果能被5整除打印Buzz,如果都能整除打印FizzBuzz。

print '\n'.join('Fizz'*(not i%3) + 'Buzz'*(not i%5) or str(i) for i in range(1, 101))

    

你可能感兴趣的:(面试总结)