list.add("qianqi,15")用filter怎么获取到15
时间: 2023-11-28 09:47:37 浏览: 21
使用 `filter()` 函数可以过滤出符合条件的元素,但是需要先将字符串转换成列表格式。
可以先使用 `split()` 函数将字符串按照逗号分隔成列表,然后使用 `filter()` 函数过滤出年龄的字符串,最后使用 `map()` 函数将年龄字符串转换成整数类型,代码如下:
```python
# 将字符串转换成列表
str_list = "qianqi,15".split(",")
# 使用 filter 函数过滤出年龄的字符串
age_str = list(filter(lambda x: x.isdigit(), str_list))[0]
# 使用 map 函数将年龄字符串转换成整数类型
age_int = int(age_str)
print(age_int) # 输出 15
```
以上代码输出的结果是 15。
相关问题
<h1>通讯录</h1> <button class="add-btn">添加联系人</button> <input type="text" id="search-input" placeholder="搜索姓名或电话"> <table> <thead> <tr> <th>姓名</th> <th>电话</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody id="contact-list"> <!-- 数据渲染 --> </tbody> </table> <!-- 分页按钮 --> <div id="page-buttons"></div> <!-- 弹出层,用于添加或编辑联系人 --> <div id="modal" class="modal" style="display: none;"> <div class="modal-content"> <span class="close">×</span> <form> <div class="form-group"> <label for="name">姓名</label> <input type="text" id="name" name="name" required> </div> <div class="form-group"> <label for="phone">电话</label> <input type="text" id="phone" name="phone" required> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="text" id="email" name="email" required> </div> <button id="save-btn">保存</button> <button id="cancel-btn">取消</button> </form> </div> </div>帮我加入一个搜索框,可以查找到搜索框中输入的内容;让数据具有添加删除和编辑的功能,编辑的时候增加弹窗进行编辑;添加翻页的功能,每一页最多存放五条数据
好的,我可以帮你完成这个需求。首先,我们需要添加一个搜索框,可以通过输入姓名或电话来查找联系人。我会给你一个基本的HTML和JavaScript代码,你可以根据你的需求进行修改和完善。
HTML代码:
```
<h1>通讯录</h1>
<button class="add-btn">添加联系人</button>
<input type="text" id="search-input" placeholder="搜索姓名或电话">
<table>
<thead>
<tr>
<th>姓名</th>
<th>电话</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody id="contact-list">
<!-- 数据渲染 -->
</tbody>
</table>
<!-- 分页按钮 -->
<div id="page-buttons"></div>
<!-- 弹出层,用于添加或编辑联系人 -->
<div id="modal" class="modal" style="display: none;">
<div class="modal-content">
<span class="close">×</span>
<form>
<div class="form-group">
<label for="name">姓名</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="phone">电话</label>
<input type="text" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="text" id="email" name="email" required>
</div>
<button id="save-btn">保存</button>
<button id="cancel-btn">取消</button>
</form>
</div>
</div>
```
JavaScript代码:
```
// 定义一个数组,用于存放联系人数据
var contacts = [
{
name: '张三',
phone: '13800138000',
email: 'zhangsan@example.com'
},
{
name: '李四',
phone: '13900139000',
email: 'lisi@example.com'
},
{
name: '王五',
phone: '13600136000',
email: 'wangwu@example.com'
},
{
name: '赵六',
phone: '13700137000',
email: 'zhaoliu@example.com'
},
{
name: '钱七',
phone: '13500135000',
email: 'qianqi@example.com'
}
];
// 定义一个变量,用于存放当前页码
var currentPage = 1;
// 定义一个函数,用于渲染联系人列表
function renderContacts() {
// 获取搜索框中的输入内容
var searchText = document.getElementById('search-input').value;
// 如果搜索框中有输入内容,则只渲染符合条件的联系人数据
if (searchText) {
var filteredContacts = contacts.filter(function(contact) {
return contact.name.indexOf(searchText) !== -1 || contact.phone.indexOf(searchText) !== -1;
});
renderContactList(filteredContacts);
} else {
// 否则,渲染所有联系人数据
renderContactList(contacts);
}
}
// 定义一个函数,用于渲染联系人列表
function renderContactList(data) {
// 获取tbody元素
var tbody = document.getElementById('contact-list');
// 清空tbody元素中的内容
tbody.innerHTML = '';
// 计算起始索引和结束索引
var startIndex = (currentPage - 1) * 5;
var endIndex = Math.min(startIndex + 5, data.length);
// 遍历符合条件的联系人数据,并将其添加到tbody元素中
for (var i = startIndex; i < endIndex; i++) {
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + data[i].name + '</td>' +
'<td>' + data[i].phone + '</td>' +
'<td>' + data[i].email + '</td>' +
'<td><button class="edit-btn" data-index="' + i + '">编辑</button><button class="delete-btn" data-index="' + i + '">删除</button></td>';
tbody.appendChild(tr);
}
// 渲染分页按钮
renderPageButtons(data.length);
}
// 定义一个函数,用于渲染分页按钮
function renderPageButtons(totalCount) {
// 获取分页按钮所在的容器元素
var pageButtonsContainer = document.getElementById('page-buttons');
// 计算总页数
var totalPage = Math.ceil(totalCount / 5);
// 清空分页按钮容器元素中的内容
pageButtonsContainer.innerHTML = '';
// 如果总页数大于1,则渲染分页按钮
if (totalPage > 1) {
for (var i = 1; i <= totalPage; i++) {
var button = document.createElement('button');
button.innerText = i;
button.classList.add('page-btn');
if (i === currentPage) {
button.classList.add('active');
}
pageButtonsContainer.appendChild(button);
}
}
}
// 定义一个函数,用于显示弹出层
function showModal(title, data, onSave) {
// 获取弹出层元素
var modal = document.getElementById('modal');
// 获取弹出层中的表单元素
var form = modal.querySelector('form');
// 获取弹出层中的关闭按钮元素
var closeButton = modal.querySelector('.close');
// 获取弹出层中的保存按钮元素
var saveButton = modal.querySelector('#save-btn');
// 获取弹出层中的姓名输入框元素
var nameInput = modal.querySelector('#name');
// 获取弹出层中的电话输入框元素
var phoneInput = modal.querySelector('#phone');
// 获取弹出层中的邮箱输入框元素
var emailInput = modal.querySelector('#email');
// 设置弹出层中的标题
modal.querySelector('h2').innerText = title;
// 如果传入了联系人数据,则将其填充到表单中
if (data) {
nameInput.value = data.name;
phoneInput.value = data.phone;
emailInput.value = data.email;
} else {
nameInput.value = '';
phoneInput.value = '';
emailInput.value = '';
}
// 绑定关闭按钮的点击事件处理函数
closeButton.addEventListener('click', function() {
modal.style.display = 'none';
});
// 绑定保存按钮的点击事件处理函数
saveButton.addEventListener('click', function(event) {
event.preventDefault();
// 如果姓名或电话或邮箱为空,则弹出提示框
if (!nameInput.value || !phoneInput.value || !emailInput.value) {
alert('姓名、电话和邮箱不能为空!');
return;
}
// 调用保存函数,并传入表单中的数据
onSave({
name: nameInput.value,
phone: phoneInput.value,
email: emailInput.value
});
// 隐藏弹出层
modal.style.display = 'none';
});
// 显示弹出层
modal.style.display = 'block';
}
// 定义一个函数,用于添加联系人
function addContact(contact) {
// 将联系人数据添加到数组中
contacts.push(contact);
// 重新渲染联系人列表和分页按钮
renderContacts();
}
// 定义一个函数,用于删除联系人
function deleteContact(index) {
// 从数组中删除指定索引的联系人数据
contacts.splice(index, 1);
// 重新渲染联系人列表和分页按钮
renderContacts();
}
// 定义一个函数,用于更新联系人
function updateContact(index, contact) {
// 更新指定索引的联系人数据
contacts[index] = contact;
// 重新渲染联系人列表和分页按钮
renderContacts();
}
// 页面加载完成后,初始化联系人列表和分页按钮
window.addEventListener('load', function() {
// 渲染联系人列表和分页按钮
renderContacts();
// 绑定添加联系人按钮的点击事件处理函数
var addButton = document.querySelector('.add-btn');
addButton.addEventListener('click', function() {
showModal('添加联系人', null, addContact);
});
// 绑定分页按钮的点击事件处理函数
var pageButtonsContainer = document.getElementById('page-buttons');
pageButtonsContainer.addEventListener('click', function(event) {
if (event.target.classList.contains('page-btn')) {
currentPage = parseInt(event.target.innerText);
renderContacts();
}
});
// 绑定编辑按钮的点击事件处理函数
var tbody = document.getElementById('contact-list');
tbody.addEventListener('click', function(event) {
if (event.target.classList.contains('edit-btn')) {
var index = parseInt(event.target.getAttribute('data-index'));
showModal('编辑联系人', contacts[index], function(contact) {
updateContact(index, contact);
});
}
});
// 绑定删除按钮的点击事件处理函数
tbody.addEventListener('click', function(event) {
if (event.target.classList.contains('delete-btn')) {
var index = parseInt(event.target.getAttribute('data-index'));
if (confirm('确定要删除该联系人吗?')) {
deleteContact(index);
}
}
});
// 绑定搜索框的输入事件处理函数
var searchInput = document.getElementById('search-input');
searchInput.addEventListener('input', function() {
renderContacts();
});
});
```
这段代码实现了以下功能:
1. 定义了一个数组,用于存放联系人数据。
2. 定义了一个变量,用于存放当前页码。
3. 定义了一个函数,用于渲染联系人列表。
4. 定义了一个函数,用于渲染分页按钮。
5. 定义了一个函数,用于显示弹出层。
6. 定义了三个函数,分别用于添加、删除和更新联系人。
7. 页面加载完成后,初始化联系人列表和分页按钮,并绑定相应的事件处理函数。
你可以将这段代码复制到你的项目中,并根据你的需求进行修改和完善。
人脸数据采集与预处理 要求每一组采集自己全部组员以及另外4名随机同学共8人,每人在不同角度/表情的头像3张,其中2张用于训练(共16张),1张用于验证(共8张)。训练图像保存到training文件夹内,命名为 姓名的拼音_1 以及 姓名的拼音_2 (如 zhangsan_1.jpg),验证图像保存到validation文件夹内,命名为 姓名的拼音 (如 zhangsan.jpg)。 以opencv读取训练数据,并将每个图像改为灰度图,大小统一为 ,即307200像素。将训练数据保存为 的矩阵,变量命名为X_trianing,余下的作为验证数据,以同样的方式命名为X_validation并将两个矩阵保存至data.npy文件中。
好的,我了解了您的问题。这是一个人脸数据采集和预处理的问题,需要采集每个组员和4个随机同学的头像,并将其分为训练和验证集。然后,使用OpenCV读取训练数据,将每个图像转换为灰度图,并将其大小调整为307,200像素。最后,将训练数据保存为X_training矩阵,并将余下的数据保存为X_validation矩阵,并将这两个矩阵保存到data.npy文件中。
为了实现这个问题,您需要使用OpenCV库和NumPy库,使用OpenCV库读取图像并将其转换为灰度图像,使用NumPy库创建矩阵并将其保存到文件中。以下是一种实现方式:
```python
import cv2
import numpy as np
# 定义图像大小
IMG_SIZE = (640, 480)
# 定义图像文件夹路径
TRAIN_DIR = 'training/'
VAL_DIR = 'validation/'
# 定义图像文件名列表
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'qianqi', 'sunba', 'zhengjiu', 'zhoushi']
# 定义训练和验证数据的数量
num_train = 16
num_val = 8
# 定义训练和验证数据的矩阵
X_training = np.empty((num_train, IMG_SIZE[0] * IMG_SIZE[1]), dtype=np.uint8)
X_validation = np.empty((num_val, IMG_SIZE[0] * IMG_SIZE[1]), dtype=np.uint8)
# 读取图像并转换为灰度图像
def read_gray_image(filename, size):
img = cv2.imread(filename)
img = cv2.resize(img, size)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
# 读取训练数据
for i, name in enumerate(names):
for j in range(2):
filename = TRAIN_DIR + name + '_' + str(j + 1) + '.jpg'
img = read_gray_image(filename, IMG_SIZE)
X_training[i * 2 + j] = img.flatten()
# 读取验证数据
for i, name in enumerate(names):
filename = VAL_DIR + name + '.jpg'
img = read_gray_image(filename, IMG_SIZE)
X_validation[i] = img.flatten()
# 保存数据到文件
np.save('data.npy', {'X_training': X_training, 'X_validation': X_validation})
```
这个实现方式假设头像已经按照要求采集和保存到相应的文件夹中,并且文件名已经按照要求命名。如果您采集头像的方式不同,可能需要根据实际情况进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)