Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Q on formatting
#1
Hi,

The variable ss retains a scanned list of local routers. I don't know why mine is shown twice but, that's a question for a different forum.
The variable a retains the rssi of my first encountered router. The second is filtered out.
The for loop expression is the typical ways of obtaining the same answer but, I'm not sure if it's correct even though I'm getting the same results.

Happy Holidays

ss = [(b'', b'\x8e\xd8\x1b;\xc2J', 4, -36, 3, False), (b'myRouter', b'\x8a\xd8\x1b;\xc2J', 4, -36, 3, False), (b'vodafoneAA3YWA', b'\x08~d\xa0-`', 11, -59, 3, False), (b'', b'\x8e\xd8\x1b;\xc2>', 4, -71, 3, False), (b'myRouter', b'\x8a\xd8\x1b;\xc2>', 4, -72, 3, False), (b'VM', b'\xd4{\xb0\xab|\x12', 1, -75, 3, False), (b'vodafone2F78', b'\xa4\x08\xf5\xef/~', 1, -86, 4, False)]

a = [x[3] for x in ss if x[0] == b'myRouter'][0]
print(a)

for x in ss:
    if x[0] == b'myRouter':
        print(x[3])
        break
Reply
#2
They both work, it's just a matter of style. Personally, I find comprehensions rather cryptic at times and aforloops logic easier to follow. What ever works best for you Wink
ebolisa likes this post
Reply
#3
(Dec-24-2021, 08:16 PM)BashBedlam Wrote: What ever works best for you Wink
I agree, however, one line coding is challenging.
Reply
#4
Personally I don't like these records with anonymous members. I tend to wrap them systematically into namedtuples, which makes such code less cryptic
from collections import namedtuple
from more_itertools import first

ss = [(b'', b'\x8e\xd8\x1b;\xc2J', 4, -36, 3, False), (b'myRouter', b'\x8a\xd8\x1b;\xc2J', 4, -36, 3, False), (b'vodafoneAA3YWA', b'\x08~d\xa0-`', 11, -59, 3, False), (b'', b'\x8e\xd8\x1b;\xc2>', 4, -71, 3, False), (b'myRouter', b'\x8a\xd8\x1b;\xc2>', 4, -72, 3, False), (b'VM', b'\xd4{\xb0\xab|\x12', 1, -75, 3, False), (b'vodafone2F78', b'\xa4\x08\xf5\xef/~', 1, -86, 4, False)]

RouterInfo = namedtuple('RouterInfo', 'name spam bust waist hips isbroken')
ss = [RouterInfo(*x) for x in ss]

a = first(r for r in ss if r.name == b'myRouter').waist
print(a)
ebolisa likes this post
Reply
#5
(Dec-24-2021, 10:38 PM)Gribouillis Wrote: Personally I don't like these records with anonymous members.
Big Grin that reminds me of an Italian movie. An American tourist just arrived at the airport of Rome and asked the taxi diver to take him to the hotel without knowing the hotel was only few steps away from where he was standing. So, the taxi driver drove him around a couple of blocks before taking him to the hotel to justify his high fees.
Gribouillis likes this post
Reply


Forum Jump:

User Panel Messages

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