1つの辞書から一意の第1タプル値に基づいて新しい個別辞書を作成し、対応するキーと値をマップできますか?Python:新しい辞書を作成するタプルのインデックス化
の作業の説明:
this = {'dog': 3, 'animal': 3} animal = {'this': 3, 'animal': 1} dog = {'this': 2, 'that': 1} that = {'animal': 1}
:このことから
Counter({('this', 'dog'): 3, ('this', 'animal'): 3, ('animal', 'this'): 3, ('dog', 'this'): 2, ('animal', 'animal'): 1, ('dog', 'that'): 1, ('that', 'animal'): 1})
が、これらの個々のdictsを作成することが可能である:
import re
from collections import Counter
TestString = 'this dog, this dog. this, animal this animal animal this animal this dog that animal'
def get_dict(string):
# Create list of individual words without punctuation
x = re.findall(r"[\w']+", string)
# Get sum of individual two-word shingles from list
x = (Counter(zip(x,x[1:])))
print x
get_dict(TestString)
には、以下の辞書を返します。新しいdict名はタプルの一意の第1要素であり、キーと値はそれに応じてマップされますか?
それは可能ですが、ほとんどの場合それを望んでいません...辞書ウィットh個の 'this' /' animal'などのユニークキー...その値は辞書ですか? –
これは、下流分析に必要なデータを構造化するための特別なアプローチです。 – RDJ
どのような単語が構文的に有効な変数名またはシャドービルトインでないか、キーワードと同じである場合はどうなりますか? :p –