2017-04-23 106 views
3

だから私は次のようになり、JSONオブジェクトの配列している次のように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の機能を調べていたが、それは行く方法かもしれない?

答えて

4
all_key2 = " ".join([elem["key2"] for elem in data if langid.classify(elem["key2"])=="english"]) 
+0

upvoteは、joinでgencompの代わりにlist-を使用します。 – timgeb

+0

それを逆転すべきか(データ)?営業は前もって –

+0

@ルチコありがとうございます!私には関係はありません: – ocean800

関連する問題