所以我有一个 json 对象数组,如下所示:
data = [{key1: 123, key2:"this is the first string to concatenate"},
{key1: 131, key2:"this is the second string to concatenate"},
{key1: 152, key2:"this is the third string to concatenate"} ]
基本上,我现在正在使用一个 for 循环,如下所示:
all_key2 = ""
data = json.load(json_file)
for p in data:
#make it all one big string
if langid.classify(p["key2"])=="english":
all_key2 = p["key2"] + " " + all_key2
所以答案应该是:
"this is the first string to concatenate this is the second string to concatenate this is the third string to concatenate"
但这会花费很多时间,因为我有大量的对象和长字符串。有没有更快的方法来完成这种连接?
[编辑] 正在调查 lambda功能,这可能是要走的路吗?
最佳答案
all_key2 = " ".join([elem["key2"] for elem in data if langid.classify(elem["key2"])=="english"])
关于python - 有效地将来自 json 对象的字符串附加到一个条件上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574639/