MagicRom
Posts: 4
Joined: Wed Oct 02, 2019 10:26 am

Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 4:50 pm

Hi guy !

At first sorry if my english make your eyes bleeding :mrgreen: :mrgreen:

I want to expose my problem here as i didn't find an answer ...

i'm making a script that pick up JSON file from URL, take the information I need and put them on a list

Code: Select all


scontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
url = 'https://www.prevision-meteo.ch/services/json/menton'
reponse = urllib.request.urlopen(url, context=scontext)
result = json.loads(reponse.read().decode('UTF-8'))

""" 2 list to stock value from 2 dict from the URL"""

info_valeur = []
condition_valeur = []

""" put the required informations on the list """

for key, value in result['city_info'].items():
	info_valeur.append(value)

for key, value in result['current_condition'].items():
	condition_valeur.append(value)
	
print(info_valeur)

Then i print the liste from my raspberry and my Mac, see below the result:

from my Mac:
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']

The list is ordered in the same way

and now the same file but from the Raspberry pi:

pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['7.4952779', '12', '43.7758331', 'Menton', 'France', '07:35', '18:59']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['18:59', 'Menton', '07:35', '12', '43.7758331', '7.4952779', 'France']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['07:35', '12', 'France', '18:59', '43.7758331', 'Menton', '7.4952779']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', '18:59', 'Menton', '12', '43.7758331', '7.4952779', '07:35']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', 'Menton', '43.7758331', '7.4952779', '18:59', '12', '07:35']

As you see, the information in the liste are everytimes in a different place on the raspberry pi...

Can someone explain me this behaviour ?

Thanks :) !!!

ejolson
Posts: 3724
Joined: Tue Mar 18, 2014 11:47 am

Re: Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 5:31 pm

MagicRom wrote:
Wed Oct 09, 2019 4:50 pm
Hi guy !

At first sorry if my english make your eyes bleeding :mrgreen: :mrgreen:

I want to expose my problem here as i didn't find an answer ...

i'm making a script that pick up JSON file from URL, take the information I need and put them on a list

Code: Select all


scontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
url = 'https://www.prevision-meteo.ch/services/json/menton'
reponse = urllib.request.urlopen(url, context=scontext)
result = json.loads(reponse.read().decode('UTF-8'))

""" 2 list to stock value from 2 dict from the URL"""

info_valeur = []
condition_valeur = []

""" put the required informations on the list """

for key, value in result['city_info'].items():
	info_valeur.append(value)

for key, value in result['current_condition'].items():
	condition_valeur.append(value)
	
print(info_valeur)

Then i print the liste from my raspberry and my Mac, see below the result:

from my Mac:
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']

The list is ordered in the same way

and now the same file but from the Raspberry pi:

pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['7.4952779', '12', '43.7758331', 'Menton', 'France', '07:35', '18:59']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['18:59', 'Menton', '07:35', '12', '43.7758331', '7.4952779', 'France']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['07:35', '12', 'France', '18:59', '43.7758331', 'Menton', '7.4952779']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', '18:59', 'Menton', '12', '43.7758331', '7.4952779', '07:35']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', 'Menton', '43.7758331', '7.4952779', '18:59', '12', '07:35']

As you see, the information in the liste are everytimes in a different place on the raspberry pi...

Can someone explain me this behaviour ?

Thanks :) !!!
Sort order is determined by localisation settings, which may be different on the two computers.

User avatar
B.Goode
Posts: 8889
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 5:43 pm

MagicRom wrote:
Wed Oct 09, 2019 4:50 pm
Hi guy !

At first sorry if my english make your eyes bleeding :mrgreen: :mrgreen:

I want to expose my problem here as i didn't find an answer ...

i'm making a script that pick up JSON file from URL, take the information I need and put them on a list

Code: Select all


scontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
url = 'https://www.prevision-meteo.ch/services/json/menton'
reponse = urllib.request.urlopen(url, context=scontext)
result = json.loads(reponse.read().decode('UTF-8'))

""" 2 list to stock value from 2 dict from the URL"""

info_valeur = []
condition_valeur = []

""" put the required informations on the list """

for key, value in result['city_info'].items():
	info_valeur.append(value)

for key, value in result['current_condition'].items():
	condition_valeur.append(value)
	
print(info_valeur)

Then i print the liste from my raspberry and my Mac, see below the result:

from my Mac:
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']
mbp-de-romain:RPI romainhenry$ python3 meteo.py
['Menton', 'France', '43.7758331', '7.4952779', '12', '07:35', '18:59']

The list is ordered in the same way

and now the same file but from the Raspberry pi:

pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['7.4952779', '12', '43.7758331', 'Menton', 'France', '07:35', '18:59']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['18:59', 'Menton', '07:35', '12', '43.7758331', '7.4952779', 'France']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['07:35', '12', 'France', '18:59', '43.7758331', 'Menton', '7.4952779']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', '18:59', 'Menton', '12', '43.7758331', '7.4952779', '07:35']
pi@raspberrypi:/media/DD1/MONNAS/meteo $ python3 meteo.py
['France', 'Menton', '43.7758331', '7.4952779', '18:59', '12', '07:35']

As you see, the information in the liste are everytimes in a different place on the raspberry pi...

Can someone explain me this behaviour ?

Thanks :) !!!

"A python dictionary doesn't track the insertion order, and iterating it gives the values in an arbitrary order." Found in various online Python resources.

One implementation appears to be returning the data in a predictable order, but should not be relied upon to do so.

gkreidl
Posts: 6119
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 5:59 pm

B.Goode wrote:
Wed Oct 09, 2019 5:43 pm

"A python dictionary doesn't track the insertion order, and iterating it gives the values in an arbitrary order." Found in various online Python resources.

One implementation appears to be returning the data in a predictable order, but should not be relied upon to do so.
It depends on the Python version. Starting with Python 3.6 dictionaries keep the order in which the key/value pairs were created. This is an implementation detail and may change again in the future.
In earlier Python versions you can use "from collections import OrderedDict" and use OrderedDict instead of normal dicts.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

MagicRom
Posts: 4
Joined: Wed Oct 02, 2019 10:26 am

Re: Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 6:49 pm

Thanks for your answer ! :D so I should better use dict for my script, many thanks

Andyroo

Re: Disordered list on raspberry pi and not on mac

Wed Oct 09, 2019 8:28 pm

By aware that json does the same when you use the dump / load methods.

I would argue that if position in any list is required then it should be kept as a field in the list itself rather than relied on as a physical attribute. It’s way too easy to eat cpu time up with extra (possibly buggy code) maintaining keys manually when the database will do it for you.

It took my poor old head a fair bit of time to handle a dictionary of lists but it saves some effort long term :lol:

blimpyway
Posts: 185
Joined: Mon Mar 19, 2018 1:18 pm

Re: Disordered list on raspberry pi and not on mac

Thu Oct 10, 2019 7:05 am

Code: Select all

for key, value in result['city_info'].items():
    info_valeur.append((key,value))
                    
print(info_valeur)
info_valeur.sort()
print(info_valeur)

As you see below there is no implicit order on dictionary items() output. You can sort them yourself, then print values only and they will be consistently keep their places.

Output:

Code: Select all

[('sunrise', '07:36'), ('longitude', '7.4952779'), ('latitude', '43.7758331'), ('country', 'France'), ('sunset', '18:57'), ('name', 'Menton'), ('elevation', '12')]

[('country', 'France'), ('elevation', '12'), ('latitude', '43.7758331'), ('longitude', '7.4952779'), ('name', 'Menton'), ('sunrise', '07:36'), ('sunset', '18:57')]


PS I would recommend against that because it assumes the site you extract data from will never add fields in their Json response. ( == improve their api)

You-re much safer by retrieving data by exact key indexes (field names), and assume publisher will not remove already established keys, instead of assuming they will not change response data at all and make a bulk sorted read.

Return to “Python”