简体   繁体   English

如何替换使用 split() 时出现的单引号 function

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

I have a string, list1 that I'm converting to a list in order to compare it with another list, list2 , to find common elements.我有一个字符串list1 ,我将其转换为一个列表,以便将其与另一个列表list2进行比较,以找到共同的元素。

The following code works, but I need to replace ' with " in the final output, since it will be used in TOML front matter.以下代码有效,但我需要在最终的 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"]**

New to python. python 的新手。 I've tried using replace() and searched around, but can't find a way to do so.我试过使用 replace() 并四处搜索,但找不到这样做的方法。 Thanks in advance.提前致谢。

I'm using Python 2.7.我正在使用 Python 2.7。

Like any other structured text format, use a proper library to generate TOML values.像任何其他结构化文本格式一样,使用适当的库来生成 TOML 值。 For example例如

>>> 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",]

made it做好了

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: output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM