Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在元组列表中查找一个元素,并给出相应的元组_Python_List_Search_Sensors - Fatal编程技术网

Python 在元组列表中查找一个元素,并给出相应的元组

Python 在元组列表中查找一个元素,并给出相应的元组,python,list,search,sensors,Python,List,Search,Sensors,我想在I2C扫描仪中实现一个简单的查找功能。最终,它应该由电池供电,并带有一个小型OLED显示屏,用于测试和排除生产中的设备故障。 我的I2C扫描仪将找到的设备列表输出为十六进制地址,例如:[“0x3c”、“0x48'] 我的想法是使用一组元组knownDevices=[address1,Description1,address2,Description2] 我是python的初学者,所以我有点卡住了。我确实知道如何在正常列表中查找单个值,如果“列表”中有“x”,但如果有更多设备,这将非常庞大。

我想在I2C扫描仪中实现一个简单的查找功能。最终,它应该由电池供电,并带有一个小型OLED显示屏,用于测试和排除生产中的设备故障。 我的I2C扫描仪将找到的设备列表输出为十六进制地址,例如:[“0x3c”、“0x48'] 我的想法是使用一组元组knownDevices=[address1,Description1,address2,Description2] 我是python的初学者,所以我有点卡住了。我确实知道如何在正常列表中查找单个值,如果“列表”中有“x”,但如果有更多设备,这将非常庞大。
我想遍历我的设备列表,在与我的数据库匹配时,它应该打印出类似“在地址处找到”的内容,让Python为您完成这项工作,并将地址映射到字典中的描述:

desc = {"0xaa": "Proximity 1", "0xbb": "Motion 1"} # And so on
# If you really want a function that does the job (not necessary) then:
get_description = desc.get # Maximize the access speed to the dict.get() method
# The method get works as follows:
desc.get("0xaa", "Unknown device")
# Or you can call it as:
get_description("0xbb", "Unknown device")
# The method gives you the possibility to return the default value in case the key is not in the dictionary
# See help(dict.get)
# But, what you would usually do is:
desc["0xaa"] # Raises an KeyError() if the key is not found
# If you really need a function that returns a list of addr, desc tuples, then you would do:
def sensors ():
    return [(x, get_description(x, "Unknown device") for x in get_addresses()]

# Which is short and efficient for:
def sensors ():
    sensors_list = []
    for x in get_addresses():
        tpl = (x, get_description(x, "Unknown device"))
        sensors_list.append(tpl)
    return sensors_list
从字典中获取值是非常快速和高效的。 你不应该有时间和记忆问题。 有很多不同的方法可以使用索引而不是dict来加快事情的速度,但是相信我,如果你不太受内存和/或速度的限制,那么花时间和编码来实现它是不值得的。 例如,此方法将包括按这样的顺序生成I2C地址,以便您的算法可以将它们缩小到包含相应描述的元组的索引。这取决于您对I2C设备地址的控制程度。简而言之,您将构建一个查找表,并以类似的方式使用它 作为三角函数。一项简单的任务需要做大量的工作。我看了一下你正在使用的MPython和MCU,你肯定有足够的资源来使用一种标准的python方式来完成你的任务,那就是:字典

另外,我必须解释,在示例函数get_addresses下,我指的是检测当时存在的设备的函数。因此,如果出于某种原因需要元组列表,并且您的设备始终存在,您可以执行以下操作:

list(desc.items())
结果列表将与“我的传感器”功能中的列表相同,但它将始终返回所有设备,无论它们是否存在。此外,如果您同时添加了一个新设备,该设备不在字典中,则它不会作为未知设备出现在结果列表中

您还应该知道,出于优化原因,dict数据类型是无序的。这意味着listdesc.items不会按照您在dict中输入的顺序返回设备的元组。但my sensors函数将按照想象的get_addresses函数返回设备地址的顺序返回它们

例如,如果要按描述的字母顺序显示所有可能的设备,可以执行以下操作:

srtkey = lambda item: item[1]
for address, description in sorted(desc.items(), key=srtkey):
    print("'%s' @ address %s" % (description, address))
    # Of course, you would use the function and/or the algorithm  you use to display the string line

请查看helpdict.items、helpsorted和helplambda以了解此代码的工作原理。

这是否回答了您的问题@AriCooper Davis我不这么认为,因为OP使用的是嵌入式编程。除非它是一个具有操作系统和强大功能的设备,比如Raspberry Pi或其他什么,否则列表中的列表搜索对于速度和效率来说是个糟糕的主意。即使只有两个值,并且可以在On中进行比较,在找到正确的映射之前,仍然需要n次比较。dict更好。我应该澄清一下我使用的是哪种风格的python。它是ESP8266和/或ESP32上的微型蟒蛇。然而,我的桌子上也有一个树莓皮3b+和4。我根据你评论中的额外信息扩展了我的答案。请看一下编辑后的版本,了解更多关于利用Python字典功能的可能方法。非常感谢。稍后我会看看这个,然后试试看。