在条件下将json对象的字符串有效地附加在一起吗? [英] Appending strings from json object on a condition together efficiently?
本文介绍了在条件下将json对象的字符串有效地附加在一起吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个数组的json对象,看起来像:
So I have an array of json objects that look like:
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循环,如下所示:
basically, I am using a for loop right now as follows:
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"
但这要花很多时间,因为我有不计其数的对象和长字符串.有没有一种更快的方法来实现这种融合?
But this is taking a lot of time because I have a bajillion objects and long strings. Is there a faster way to accomplish this concatentation?
正在调查 lambda 函数,这可能是要走的路吗?
Was looking into lambda functions, might that be the way to go?
推荐答案
all_key2 = " ".join([elem["key2"] for elem in data if langid.classify(elem["key2"])=="english"])
这篇关于在条件下将json对象的字符串有效地附加在一起吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文