[英]Python - Remove quotation marks from list
我有一个带有数字列表的 dataframe。 打印列表时,我要删除前面和末尾的引号。 我无法用替换 function 替换它们。
a = df.iloc[0]['ids']
b = [a]
b (Output)
['2769734487041924, 7608779650164612'] <-- one quotation mark at beginning and end
我想要的(手动创建):
row_ids = [1234, 4567]
row_ids (Output)
[1234, 4567] <-- That's the format I want to get the list to, without the quotation marks
b和row_ids数据类型都返回为“列表”,因此必须有一种方法可以使列表不带引号。
我认为a
是一个字符串,因此可以从中获取b
作为您需要的整数列表
b = list(map(int, a.split(",")))
解析字符串并拆分,然后使用十进制将其转换为 int。
lst = ['2769734487041924, 7608779650164612']
lst2 = []
for i in "".join(lst).split(","):
lst2.append(int(i, base=10))
print(lst2)
lst = '['2769734487041924, 7608779650164612']'
import json
lst = json.loads(lst )
output: lst = ['2769734487041924, 7608779650164612']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.