Python Forum
Explanation of the left side of this statement please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explanation of the left side of this statement please
#1
The statement below is from here
I understand that it is slicing and stripping the nmea string, but what is the meaning of the left hand side part:
lat, _, lon = line.strip().split(',')[2:5]
In particular with the use of underscore?
An Nmea string example would look like:
$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F
And I understand we are extracting the lat and long as:
lat = 3723.46587704
lon = 12202.26957864
So how does it assign lat/lon in one line
(I actually used it successfully, but would like to understand it a bit better)
regards
Russell
Reply
#2
in slow motion:
line is str
line.strip() will remove any white-space in the begining or end of str
.split(',') will produce a list
[2:5] is slicing, so the right side is ['3723.46587704','N','12202.26957864']

what you have is unpacking the list (or any other iterable) into 3 variables. _ by convention means throw-away value (i.e. you are not interested in it)
TLTR: this is iterable unpacking. on the left side you have 3 variables and you unpack the iterable (assign each element as value to respective name). there is also extended iterable unpacking

also there is chained assignment, e.g. spam = eggs = 'foo'. both spam and eggs value is 'foo'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
just an FYI (which you may already know):

GPGGA is a log file that contains time, position and fix related data of the GNSS receiver
it uses the NMEA 0183 combined electrical and data specification
read more here: https://tronico.fi/OH6NT/docs/NMEA0183.pdf

also see: https://docs.novatel.com/oem7/Content/Lo...onNMEALogs

GNSS is the European Global navigation satellite.
see: https://www.gsa.europa.eu/european-gnss/what-gnss

The snippet as buran explains will contain just the latitude and longitude coordinates.
Reply
#4
Thanks guys for the great explanations
regards
Russell
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 371 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  How to horizontally align and display images side-by-side in an email using Python? shantanu97 0 997 Feb-22-2023, 11:41 PM
Last Post: shantanu97
  A better explanation of the last post Led_Zeppelin 9 2,370 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  PyQT5 - align left frohr 7 3,934 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,925 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  Get the Client-Side TLS ? JohnnyCoffee 1 1,346 Nov-30-2021, 10:49 PM
Last Post: Larz60+
  Operator meaning explanation Sherine 3 2,018 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,916 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,950 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,212 Jan-28-2021, 04:21 PM
Last Post: InputOutput007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020