簡體   English   中英

Python - 從列表中刪除引號

[英]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

brow_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM