简体   繁体   English

如何将元组列表转换为单独的字符串?

[英]How should I convert a list of tuples into separate strings?

import obd
connection = obd.OBD()
r = connection.query(obd.commands.GET_DTC)
print(r.value)


example output:
[
  ("P0030", "HO2S Heater Control Circuit"),
  ("P1367", "Unknown error code")
]

I would like to store/access the second value in each outputted item (eg "HO2S Heater Control Circuit") as its own variable. 我想在每个输出项(例如“ HO2S加热器控制电路”)中存储/访问第二个值作为其自己的变量。 Do I need to decode this output as a list or tuple or both? 我需要将此输出解码为列表还是元组,还是两者都解码?

From the page where this code seems to be from : 该代码似乎来自的页面

The value field of the response object will contain a list of tuples, where each tuple contains the DTC, and a string description of that DTC (if available). 响应对象的value字段将包含一个元组列表,其中每个元组都包含DTC,以及该DTC的字符串描述(如果有)。

So if r = connection.query(obd.commands.GET_DTC) , then r.value is a "list of tuples". 因此,如果r = connection.query(obd.commands.GET_DTC) ,则r.value是“元组列表”。 You can use the zip() function (as described in this question ) to transpose the structure with zip(*r.value) which gives 您可以使用zip()函数(如本问题所述 )将结构与zip(*r.value)换位,从而得到

    [('P0030', 'P1367'), ('HO2S Heater Control Circuit', 'Unknown error code')]

You just want the second element of this list, so 您只需要此列表的第二个元素,因此

    zip(*r.value)[1]

gives you the tuple 给你元组

    ('HO2S Heater Control Circuit', 'Unknown error code')

You could then use this as you wish. 然后,您可以根据需要使用它。 Notice that this gives you all of the "second values in each outputted item". 请注意,这为您提供了所有 “每个输出项目中的第二个值”。 You could iterate through all them (and, say print each one) with: 您可以使用以下方法遍历所有这些对象(并说打印每个对象):

    for description in zip(*r.value)[1]:
        print description

It may be a good idea to assign zip(*r.value)[1] to a variable if you want to use it more than once. 如果要多次使用zip(*r.value)[1] ,则可能是个好主意。

If you want to use each second element you can unpack the tuples in a for loop: 如果要使用第二个元素,则可以在for循环中将元组解包:

for _, var in r.value:
    # use var

ie: 即:

In [4]: l = [                                                                                           
  ("P0030", "HO2S Heater Control Circuit"),
  ("P1367", "Unknown error code")
]

In [5]: for _, var in l:
          print(var)
   ...:     
HO2S Heater Control Circuit
Unknown error code

If you wanted them all for some reason could also use a list comp with the logic above or operator.itemgetter : 如果出于某种原因想要全部使用它们,也可以使用具有以上逻辑或operator.itemgetter的list comp:

In [7]: list(map(itemgetter(1), l))
Out[7]: ['HO2S Heater Control Circuit', 'Unknown error code']

In [8]: from operator import itemgetter

In [9]: list(map(itemgetter(1), l))
Out[9]: ['HO2S Heater Control Circuit', 'Unknown error code']

You could also use itemgetter(-1) to get the last elements. 您还可以使用itemgetter(-1)来获取最后一个元素。

You could "unpack" a tuple. 您可以“打开”一个元组。 So, from your example, r.value[0] is the tuple ("P0030", "HO2S Heater Control Circuit") Then, 因此,根据您的示例, r.value[0]是元组("P0030", "HO2S Heater Control Circuit")然后,

id, desc = r.value[0]

would unpack the tuple, r.value[0] into varaiables id and desc so that P0030 is stored in id and HO2S Heater Control Circuit is stored in desc 会将元组r.value [0]解压缩为变量iddesc以便将P0030存储在id并将HO2S Heater Control Circuit存储在desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM