python - 如何在 Python 中删除列表中的引号
问题描述
我想从变量平均值中删除引号。这是我的代码:
list_1 = ['LIST: ']
list_2 = [['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167]]
list_3 = ['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167]
combine = (str(list_1)[2:-2]) + (str(list_2)[1:-1])
print(combine)
new_list = input("Name to add to the list: ")
average = input("Average: ")
list_3.append(new_list)
list_3.append(average)
print(f"UPDATED LIST: {list_3}")
print("There are now {0} items".format(len(list_3)), "in the list")
这是我想要出来的:
LIST: ['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167]
Name to add to the list: Ann Kert
Average: 189
UPDATED LIST: ['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167, 'Ann Kert', 189]
There are now 8 items in the list
但这是我得到的:
LIST: ['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167]
Name to add to the list: Ann Kert
Average: 189
UPDATED LIST: ['Mary Smith', 132, 'Jean Jones', 156, 'Karen Karter', 167, 'Ann Kert', '189']
There are now 8 items in the list
如果您没有看到差异,那么差异是 189 需要没有 qoutes 但我得到的是“189”
解决方案
尝试将字符串类型转换为 int。
average = int(input("Average: "))
推荐阅读
- pandas - 将 Dataframe 推送到 Presto
- sql - “combined_lead_score”列的平均值
- azure - 带有 Azure AD SSO 的 Web 应用:是否可以让外部租户的用户访问他们的电子邮件?
- python - Python根据另一列将值从一行复制到另一行
- sql-server - Docker SQL Server 更改服务器名称本地
- java - Java中HashSet.contains的时间复杂度
- javascript - 如何以角度从http请求返回完全初始化的对象
- aws-lambda - 无法在 Terraform 中使用 AWS SecretManager 添加 Lambda 函数
- java - 是否可以设置 Jackson 以将未命名的包装器对象排除到 API 返回的 JSON 数组中?
- c# - 从 Mat 的特定值创建蒙版(在 EMGU 中)