首页 > 解决方案 > 如何在 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”

标签: python

解决方案


尝试将字符串类型转换为 int。

average = int(input("Average: "))

推荐阅读