dictionary - Python: How to append to dict without "key"? -
i have python function returns dict. want call function various times , append these returned dicts first one.
def generate_dict(greeting): n = len(greeting) = int(np.random.random()*(n-1)) j = int(np.random.random()*(n-1)) text = greeting[i] + greeting[j] start = text.find(greeting[i]) return {"text": text, "intention": "greet", "entity": [ { "start": start, "end": start + len(greeting[i]), "value": greeting[i], "entity": "hello" } ], } greeting = ['hallo', 'hey', 'hi'] dict1 = generate_dict(greeting) k in range(1,10): dict1[k] = generate_dict(greeting) print dict1
this code gives me result like:
{1: {'text': 'heyhey', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}, 2: {'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}, 3: {'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}, 4: {'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}, ... }
but get:
{'text': 'heyhey', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'},{'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'},{'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'},{'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}, ...
how can archieve this?
i suppose want list store these data, maybe can try:
dict1= [generate_dict(greeting) k in range(1,10)]
dict1:
[{'text': 'hallohallo', 'entity': [{'start': 0, 'end': 5, 'value': 'hallo', 'entity': 'hello'}], 'intention': 'greet'}, {'text': 'hallohey', 'entity': [{'start': 0, 'end': 5, 'value': 'hallo', 'entity': 'hello'}], 'intention': 'greet'}, {'text': 'heyhallo', 'entity': [{'start': 0, 'end': 3, 'value': 'hey', 'entity': 'hello'}], 'intention': 'greet'}...]
Comments
Post a Comment