**1.【編碼實現(xiàn)】去除一個整數(shù)數(shù)組中的重復元素,并進行從大到小排序股淡,最后打印出結果屯换。**
參考答案:
```python
def qchong(s):
? ? list1 = []
? ? for num in s:
? ? ? ? if num not in list1:
? ? ? ? ? ? list1.append(num)
? ? return sorted(list1, reverse=True)
if __name__ == "__main__":
? ? s = [12, 16, 15, 18, 26, 18, 17, 16]
? ? res = qchong(s)
? ? print(res)
? ? # out [26, 18, 17, 16, 15, 12]
```
**2.【編碼實現(xiàn)】提取如下html結構中的信息。**
```python
<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>
```
并生成如下數(shù)據(jù)結構:
{
? 'human': [{ age: 24, name: '蔡徐坤' },{ age: 21, name: '鹿晗' }],
? 'robot': [{ dateTime: '2019-01-01', name: '伊娃' }],
? 'animal': [{ age: 3, name: '豆豆' }],
}
參考答案:
```python
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': '豆豆'}]}
```
**3.【編碼實現(xiàn)】有如下數(shù)組谅畅,需要將每個字符串中排列在任意x前面的所有y替換為0,請計算并打印出每個字符串需要被替換的y的個數(shù)噪服。**
arr = ['xxyyxyyyyyxxx', 'yxxxx', 'xyyyxxyx', 'xxxx', 'xxxyyy'];
```python
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]
```