0

I have scanned for a list of Bluetooth devices which looks like:-

[{'address': '00:0C:99:88:77:66', 'name': 'FK20020', 'rssi': -49, 'packet_data': {'connectable_advertisement_packet': {'flags': bytearray(b'\x06'), 'complete_list_16-bit_service_class_uuids': bytearray(b'\t\x18\x02\x18'), 'complete_local_name': 'FK20020'}, 'scan_response_packet': {}}}, {'address': '77:11:22:33:44:55', 'name': 'PQR11225', 'rssi': -49, 'packet_data': {'connectable_advertisement_packet': {'flags': bytearray(b'\x06'), 'incomplete_list_128-bit_service_class_uuids': bytearray(b'\x07\xb9\xf9\xd7P\xa4 \x89w@\xcb\xfd,\xc1\x80H'), 'complete_local_name': 'PQR11225'}, 'scan_response_packet': {}}}, {'address': '77:55:33:22:44:99', 'name': 'PQR05286', 'rssi': -49, 'packet_data': {'connectable_advertisement_packet': {'flags': bytearray(b'\x06'), 'incomplete_list_128-bit_service_class_uuids': bytearray(b'\x08\xc9\xf9\xd9P\xa4 \x89w@\xcb\xdd,\xc3\x90H'), 'complete_local_name': 'PQR05286'}, 'scan_response_packet': {}}}]

From the list, i only want devices whose name starts with "PQR" (for example PQR11225 or PQR05286), and form a list which contains their 'name','address' and 'rssi'

Is there is way to achieve this?

5 Answers 5

2

You can loop through the list, and only pick out names that start with PQR

result = []
for device in devices:
    if device['name'].startswith('PQR'):
        result.append(device)`
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use a list comp:

got=[(i['name'],i['address'],i['rssi']) for i in devices if i['name'].startswith('PQR')]

Comments

1

The list comp approach-

[(i['name'], i['address'], i['rssi']) for i in bluetooth_list if i['name'].startswith('PQR')]

Comments

1

you can use filter for this as

res = []


for dev in list_:
    if dev['name'].startswith('PQR'):
            tmp = {'name':dev['name'],'address':dev['address'], 'rssi':dev['rssi']}
            res.append(tmp)

print(res)

output

[{'name': 'PQR11225', 'address': '77:11:22:33:44:55', 'rssi': -49}, {'name': 'PQR05286', 'address': '77:55:33:22:44:99', 'rssi': -49}]

Comments

1

you can filter your list base on the device name:

list(filter(lambda s: s['name'].startswith('PQR'), my_list))

output:

[{'address': '77:11:22:33:44:55',
  'name': 'PQR11225',
  'rssi': -49,
  'packet_data': {'connectable_advertisement_packet': {'flags': bytearray(b'\x06'),
    'incomplete_list_128-bit_service_class_uuids': bytearray(b'\x07\xb9\xf9\xd7P\xa4 \x89w@\xcb\xfd,\xc1\x80H'),
    'complete_local_name': 'PQR11225'},
   'scan_response_packet': {}}},
 {'address': '77:55:33:22:44:99',
  'name': 'PQR05286',
  'rssi': -49,
  'packet_data': {'connectable_advertisement_packet': {'flags': bytearray(b'\x06'),
    'incomplete_list_128-bit_service_class_uuids': bytearray(b'\x08\xc9\xf9\xd9P\xa4 \x89w@\xcb\xdd,\xc3\x90H'),
    'complete_local_name': 'PQR05286'},
   'scan_response_packet': {}}}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.