簡體   English   中英

如何替換使用 split() 時出現的單引號 function

[英]How to replace single quotes that appear when using split() function

我有一個字符串list1 ,我將其轉換為一個列表,以便將其與另一個列表list2進行比較,以找到共同的元素。

以下代碼有效,但我需要在最終的 output 中將'替換為" ,因為它將用於 TOML 前端。

list1 = "a b c"
list1 = list1.split(" ")

print list1
>>> ['a','b','c']

list2 = ["b"]

print list(set(list1).intersection(list2))
>>> ['b']

**I need ["b"]**

python 的新手。 我試過使用 replace() 並四處搜索,但找不到這樣做的方法。 提前致謝。

我正在使用 Python 2.7。

像任何其他結構化文本格式一樣,使用適當的庫來生成 TOML 值。 例如

>>> import toml
>>> list1 = "a b c"
>>> list1 = list1.split(" ")
>>> list2 = ["b"]
>>> v = list(set(list1).intersection(list2))
>>> print(v)
['b']
>>> print(toml.dumps({"values": v}))
values = [ "b",]

做好了

import json
l1 = "a b c"
l1 = l1.split(" ")

print(l1)


l2 = ["b"]

print(json.dumps(list(set(l1).intersection(l2))))

print(type(l1))

output:

['a', 'b', 'c']
["b"]
<type 'list'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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