How can I auto-increment the value in a dictionary when there is a difference?

João Santos

It's much easier to explain my doubts with an example:

I have a list with dictionaries of this type,

[
    {
     'portal_id': 5, 
     'url_imovel': 'asd.pt', 
     'url_photos': {0: 'pfoto1.pt', 1: 'pphoto2.pt'}, 
     'total_photos': 2
    }, 
    {
     'portal_id': 5, 
     'url_imovel': 'asd2.pt', 
     'url_photos': {0: 'pfoto12.pt', 1: 'pphoto22.pt'}, 
     'total_photos': 2
    },
    {
     'portal_id': 5, 
     'url_imovel': 'foo.pt', 
     'url_photos': {0: 'fotofoo1.pt', 1: 'photofoo2.pt', 1: 'photofoo3.pt'}, 
     'total_photos': 3
    }
]

The dictionary is generated as follows:

all_im_fotos = []

    im_fotos = {
        "portal_id" : 5,
        "url_imovel" : str(),
        "url_photos" : dict(),
        "total_photos": int()
    }

    urls = "asd.pt"
    photos_url = ["pfoto1.pt", "pphoto2.pt"]

    im_fotos["url_imovel"] = urls
    for i in range(len(photos_url)):
        im_fotos["url_photos"].update({i: photos_url[i]})
    im_fotos["total_photos"] = len(im_fotos["url_photos"])

    all_im_fotos.append(im_fotos)

    ... same process ...

My question is, how can I compare the insertion of new values. My goal is to create a new dictionary that just insert new values, increasing the index of imfotos. For example:

difference = []
new_imovel = "asd.pt"
new_url_photos = ["pfoto1.pt", "pfoto3.pt"]

There is a new photo for the url "asd.pt", so we should add the difference by auto incrementing the index, resulting in:

[
        {
         'portal_id': 5, 
         'url_imovel': 'asd.pt', 
         'url_photos': {0: 'pfoto1.pt', 1: 'pphoto2.pt', 2: 'pfoto3.pt'}, 
         'total_photos': 3
        }
]

How can I make this as simple as possible? is it possible to save this dictionary as a json file in order to perform the same process later?

leaf_yakitori

is it possible to save this dictionary as a json file in order to perform the same process later?

you can use json.dumps() to convert python dict to json, and use json.loads() to read json file to python dict.

link:

code:

datas = [
    {
     'portal_id': 5, 
     'url_imovel': 'asd.pt', 
     'url_photos': {0: 'pfoto1.pt', 1: 'pphoto2.pt'}, 
     'total_photos': 2
    }, 
    {
     'portal_id': 5, 
     'url_imovel': 'asd2.pt', 
     'url_photos': {0: 'pfoto12.pt', 1: 'pphoto22.pt'}, 
     'total_photos': 2
    },
    {
     'portal_id': 5, 
     'url_imovel': 'foo.pt', 
     'url_photos': {0: 'fotofoo1.pt', 1: 'photofoo2.pt', 2: 'photofoo3.pt'}, 
     'total_photos': 3
    }
]

new_imovel = "asd.pt"
new_url_photos = ["pfoto1.pt", "pfoto3.pt"]
for data in datas:
    if data["url_imovel"] == new_imovel:
        for photo in new_url_photos:
            if not photo in data['url_photos'].values():
                data['url_photos'].update({len(data['url_photos']):photo})
                data['total_photos']+=1
print(datas)

result:

[
    {
        "portal_id": 5,
        "url_imovel": "asd.pt",
        "url_photos": {0: "pfoto1.pt", 1: "pphoto2.pt", 2: "pfoto3.pt"},
        "total_photos": 3,
    },
    {
        "portal_id": 5,
        "url_imovel": "asd2.pt",
        "url_photos": {0: "pfoto12.pt", 1: "pphoto22.pt"},
        "total_photos": 2,
    },
    {
        "portal_id": 5,
        "url_imovel": "foo.pt",
        "url_photos": {0: "fotofoo1.pt", 1: "photofoo2.pt", 2: "photofoo3.pt"},
        "total_photos": 3,
    },
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I increment a value in a python dictionary?

How can I set my auto-increment value to start from 1 in MySQL?

How can I get the corresponding user_id (auto increment) value of a particular row in my database?

Can I assume the highest auto increment value is always the newest entry?

How can I increment a value in scheme with closure?

How can I increment a value on page refresh?

How can I increment a value from firebase?

How do I create auto increment value for a field in elastic search

How can I set AUTO_INCREMENT for @PrimaryColumn()?

How can I set no auto increment in migration laravel 5.3?

How can I auto-increment an MVC 6 version number?

how can I create a auto-increment field in mongoose

SQL Server-How can I make a varchar auto increment

how can i make an ID auto increment and hidden from user?

How can I auto increment numbered results with the DOM in javaScript?

There can be only one auto column error when giving AUTO_INCREMENT value

Auto Increment Value on SQL SELECT when duplicate

when used Dictionary How can I get the value if it non nullable value type

how to set auto increment column value with condition

how to auto-increment a value in php?

How Use Auto Increment value in two columns

how to insert auto increment value for trigger in postgresql?

How to set initial value and auto increment in MySQL?

How to add auto increment value to realm

How to implement auto increment column value alphabetical?

how to define start value for GORM auto increment

How can I replace "None" as a dictionary value with an empty dictionary?

How can I modify a value of a dictionary that inside another dictionary

I can auto increment id in room database but when refresh database it's shows double