我有一个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函数,那可能是正确的方法吗?
.join()
并进行预处理呢?例如,我调用langid.classify(p["key2"])
来查看字符串是否为英语。为了单独分类每个字符串,我使用了一个for循环。但是,无论传递给join的内容都需要是已经预处理过的序列,对吗?这意味着我仍然需要使用for循环吗?我已经编辑了我的问题以匹配。 - ocean800