0

I'm new to Python and struggling to get my head around lists and dictionaries, specifically accessing specific fields.

I am trying to capture and keep track of Bluetooth mac addresses so I have created a list of dictionaries, which is as close to the C concept of an array of structs as I can manage:

max_devices = 10
num_devices = 2
device_params = {'device_number','device_id','device_mac'}
devices = [device_params] * num_devices

For testing purposes I then create a couple of dummy dictionary entries:

devices[0] = {0, "zero", "00aa00bb00cc"}
devices[1] = {1, "one", "00dd00ee00ff"}

I can print out the entire dictionary for each device, one at a time:

print("Devices, 1 by 1:")
which_device = devices[0]
print(which_device)
which_device = devices[1]
print(which_device)

Straightforward, but why are they being printed out of sequence?, i.e.

Devices, 1 by 1:
{0, 'zero', '00aa00bb00cc'}
{'00dd00ee00ff', 1, 'one'}

That is an aside though. The main problem is that I've come unstuck by trying to incorporate printing specific dictionary fields into a loop:

print("Devices, looped:")
for device in devices:
    print("device_mac" in devices)

I've tried a lot of combinations for the final print command but nothing so far has worked and I am now going around in circles. I want to loop through each dictionary and print out a specific item such as "device_mac".

The next step is to add some dummy devices but it would be prudent for me to get past this bit first. I'd be grateful for some direction here.

alidaf
  • 1
  • 2
  • 3
    Because sets are not ordered in python. – cs95 Sep 27 '17 at 15:38
  • @cᴏʟᴅsᴘᴇᴇᴅ are they not `sets`? – Jerrybibo Sep 27 '17 at 15:39
  • 1
    Those are sets not dicts. – Moses Koledoye Sep 27 '17 at 15:39
  • Might want to go through the docs for dictionaries (how to initialize, what the `in` operator does): https://docs.python.org/3/library/stdtypes.html#typesmapping – c2huc2hu Sep 27 '17 at 15:40
  • @Jerrybibo yes my bad... but anyway, sets are implemented as dicts so I'm partially right? – cs95 Sep 27 '17 at 15:40
  • @cᴏʟᴅsᴘᴇᴇᴅ Sure, guess you can say that. Consider posting an answer :P – Jerrybibo Sep 27 '17 at 15:42
  • @Jerrybibo Better still would be to close as a duplicate. :-) – cs95 Sep 27 '17 at 15:45
  • Thanks for the comments. I've taken the ordering information on board. I don't think I made it particularly clear but the more important question is the one related to accessing a specific field. – alidaf Sep 27 '17 at 16:40
  • I've edited the original question for clarity but the responses so far have been helpful in developing my general understanding of Python. – alidaf Sep 27 '17 at 16:49
  • This isn't a duplicate of the linked question? While OP uses sets, that's a mistake and they intended to use dictionaries. – Jared Goguen Sep 27 '17 at 16:49
  • I have found the answer myself but I can't add the solution because this is marked as a duplicate, which is isn't. – alidaf Sep 28 '17 at 10:27

0 Answers0