将来自 json 对象的字符串有效地附加到一个条件上?

2024-01-06

所以我有一个 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" 

但这需要花费很多时间,因为我有无数的对象和很长的字符串。有没有更快的方法来完成这种串联?

[EDIT]正在调查lambda http://caisbalderas.com/blog/iterating-with-python-lambdas/功能,这可能是要走的路吗?


all_key2 = " ".join([elem["key2"] for elem in data if langid.classify(elem["key2"])=="english"])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将来自 json 对象的字符串有效地附加到一个条件上? 的相关文章

随机推荐