I can not understand how it is possible in Python to add data to the dictionary (array) at a specific index, without using for and append cylinders.

The data is returned by a Solr filter, but it is not assigned in the right way ( append ).

And I need the data to be contained as follows. For the time being, for example, I added them manually:

 clean_data = { 'results': [ { 'name': u'Classic', 'select_url': '/ru/catalogue/category/Classic' }, { 'name': u'Common', 'select_url': '/ru/catalogue/category/Common' } ] } 

For example, the Classic object is under the index [0] , followed by the object [1] common object was added immediately under the index [1] .

Of course, I could use for-loop , and append, but this creates a problem, when you re-apply the common object is written under the index [0], and classic under [1], but you need the collection to be constant, i.e. dict[common[0],classic[1]] , not dict[common[0]], dict[common[1]] , due to the logic of Solr.

And for this, I would like to initially assign the [] index of the incoming object, and change the size of the collection depending on the amount of incoming data. in C code, this was done as follows: dict.length = 1 ; dict[0] = 'common' , or dict.set(0) = 'common' , but how to do this in python. The point is not to add data through for and append , since that's my problem.

    1 answer 1

    I see such options: pre-fill the list with empty values, for example, None and subsequently replace them with their indices:

     clean_data = { 'results': None } # Добавление 10 пустых записей clean_data['results'] = [None for _ in range(10)] clean_data['results'][0] = { 'name': u'Classic', 'select_url': '/ru/catalogue/category/Classic' } clean_data['results'][3] = { 'name': u'Common', 'select_url': '/ru/catalogue/category/Common' } 

    Or instead of a list, use an associative array (dictionary), in which the index key will be, then you will not need to bother with filling the list:

     clean_data = { 'results': dict() } clean_data['results'][0] = { 'name': u'Classic', 'select_url': '/ru/catalogue/category/Classic' } clean_data['results'][3] = { 'name': u'Common', 'select_url': '/ru/catalogue/category/Common' } 
    • But what does such a dictionary look like in python? - LighFusion
    • Braces with keys and values ​​- a dictionary. Brackets - list. Just curly brackets with values ​​- set. Parentheses are a tuple. To create these collections there are built-in functions, respectively: dict, list, set and tuple - gil9red
    • But I clearly didn’t notice any changes, I just don’t quite understand the problem, the solr felts re-form the data and they are assigned to the dictionary, or something else. And maybe it should be decided differently, check incoming data for changes (adding indices by the solar), and only if this data is not in the dictionary to write already, otherwise I think it turns out that upon request, the recorded data is processed again (and not correct), how would you make them static? At the exit, I have such nastiness - i.imgur.com/EaDcc9V.png?2 i.imgur.com/ObfzssL.png?1 - common changed the index - LighFusion
    • Better check for changes. The question on solr is no longer relevant to this question; it is better to create a separate one with the corresponding label, code and examples of data. And if my answer suits you, click on the check mark to the left of the answer. - gil9red
    • @ gli9red okay, I just try to solve the problem in a simpler, if it is even possible, but there was not much experience with the Python Slavarists - so thanks - LighFusion