python Web面试题

2020-03-22  本文已影响0人  Yimmy_Lee
def find_list(list_1, ind, num):
    """
    :param list_1: 无序列表
    :param ind: 开始索引
    :param num: 间隔
    :return: 结果列表
    """
    start_list = []
    length_list_1 = len(list_1)
    if length_list_1 == 0:
        return None
    elif length_list_1 < ind:
        return None
    else:
        start_list.append(list_1[ind])
    mid = ind + num
    while mid < length_list_1:
        start_list.append(list_1[mid])
        mid += num
    return start_list


# list_test = [i for i in range(100)]
list_test = [2, 4, 6, 5, 6, 9]
res = find_list(list_test, 1, 2)
print(res) # [4, 5, 9]

<div class="list">
 <div class="item">
   <p><label>姓名:</label><span>蔡徐坤</span></p>
   <p><label>类型:</label><span>human</span></p>
   <p><label>年龄:</label><span>24</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>伊娃</span></p>
   <p><label>类型:</label><span>机器人</span></p>
   <p><label>生产日期:</label><span>2019-01-01</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>豆豆</span></p>
   <p><label>类型:</label><span>动物</span></p>
   <p><label>年龄:</label><span>3</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>鹿晗</span></p>
   <p><label>类型:</label><span>人类</span></p>
   <p><label>年龄:</label><span>21</span></p>
 </div>
</div>

并生成如下数据结构:
{
'human': [{ age: 24, name: '蔡徐坤' },{ age: 21, name: '鹿晗' }],
'robot': [{ dateTime: '2019-01-01', name: '伊娃' }],
'animal': [{ age: 3, name: '豆豆' }],
}
参考答案:

from lxml import etree


if __name__ == "__main__":
    content = """
    <div class="list">
 <div class="item">
   <p><label>姓名:</label><span>蔡徐坤</span></p>
   <p><label>类型:</label><span>human</span></p>
   <p><label>年龄:</label><span>24</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>伊娃</span></p>
   <p><label>类型:</label><span>机器人</span></p>
   <p><label>生产日期:</label><span>2019-01-01</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>豆豆</span></p>
   <p><label>类型:</label><span>动物</span></p>
   <p><label>年龄:</label><span>3</span></p>
 </div>
 <div class="item">
   <p><label>姓名:</label><span>鹿晗</span></p>
   <p><label>类型:</label><span>人类</span></p>
   <p><label>年龄:</label><span>21</span></p>
 </div>
</div>
    """
    page = etree.HTML(content)
    items = page.xpath('.//div[@class="item"]')
    all_content = {}
    people = []
    robot = []
    animal = []
    for item in items:
        nr = item.xpath('./p/span/text()')
        if 'human' in nr or '人类' in nr:
            # print(i)
            people.append({'age': nr[2], 'name': nr[0]})
        elif '机器人' in nr:
            robot.append({'dateTime': nr[2], 'name': nr[0]})
        else:
            animal.append({'age': nr[2], 'name': nr[0]})
    all_content['human'] = people
    all_content['robot'] = robot
    all_content['animal'] = animal
    print(all_content)
    out = {'human': [{'age': '24', 'name': '蔡徐坤'}, {'age': '21', 'name': '鹿晗'}],
           'robot': [{'dateTime': '2019-01-01', 'name': '伊娃'}],
           'animal': [{'age': '3', 'name': '豆豆'}]}

CPU密集型适合用多线程开发

出处 https://www.jianshu.com/p/cfe72a8c2456

6.【编码实现】有如下数组,需要将每个字符串中排列在任意x前面的所有y替换为0,请计算并打印出每个字符串需要被替换的y的个数。
arr = ['xxyyxyyyyyxxx', 'yxxxx', 'xyyyxxyx', 'xxxx', 'xxxyyy'];

import re


def change_str(listi):
    list_res = []
    pattern = re.compile(r'(y+)x')
    for s in listi:
        res = re.findall(pattern, s)
        list_res.append(len(''.join(res)))
    return list_res


if __name__ == "__main__":
    arr = ['xxyyxyyyyyxxx', 'yxxxxyxy', 'xyyyxxyx', 'xxxx', 'xxxyyy'];
    res = change_str(arr)
    print(res)
    # out = [7, 2, 4, 0, 0]
上一篇下一篇

猜你喜欢

热点阅读