简体   繁体   English

从 python 中的列表中错误地访问值

[英]accessing values incorrectly from list in python

I have two example files.我有两个示例文件。

myheader.h

#define MACRO1 42
#define lang_init ()  c_init()
#define min(X, Y)  ((X) < (Y) ? (X) : (Y))

and pyparser.py和 pyparser.py

from pyparsing import *
# define the structure of a macro definition (the empty term is used 
# to advance to the next non-whitespace character)
macroDef = "#define" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \
            empty + restOfLine.setResultsName("value")
with open('myheader.h', 'r') as f:
    res = macroDef.scanString(f.read())
    res = list(res)
    print(res[0])
    print(res[1])
    print(res[2])

the output is output 是

((['#define', 'MACRO1', '42'], {'macro': ['MACRO1'], 'value': ['42']}), 0, 17) ((['#define', 'lang_init', '() c_init()'], {'macro': ['lang_init'], 'value': ['() c_init()']}), 18, 48) ((['#define', 'min', '(X, Y) ((X) < (Y)? (X): (Y))'], {'macro': ['min'], 'value': ['(X, Y) ((X) < (Y)? (X): (Y))']}), 49, 91)

I thought print(res[0]) would print "#define", print print(res[1]) would print 'MACRO1' and so on.我以为 print(res[0]) 会打印“#define”,print print(res[1]) 会打印“MACRO1”等等。 I'm not that familiar with Python, but I'm assuming res is not an array correct?我不太熟悉 Python,但我假设 res 不是数组,对吗? How does indexing works in this case?在这种情况下索引如何工作? Thanks谢谢

Question, what is the value of len(res).问题,len(res) 的值是多少。

In python when you have a list inside of a list you can use a second indexer to access the elements inside of it.在 python 中,当您在列表中有一个列表时,您可以使用第二个索引器来访问其中的元素。 So for example if the first element res[0] was a list, you could do res[0][0] to get '#define'.因此,例如,如果第一个元素 res[0] 是一个列表,您可以执行 res[0][0] 以获得“#define”。

However, your output that you have shown is in a different format than typical nested list syntax, so doing res[0][0] might not work (because it might not be the right type of object).但是,您显示的 output 的格式与典型的嵌套列表语法不同,因此执行 res[0][0] 可能不起作用(因为它可能不是正确的对象类型)。

This is what a nested list is supposed to look like: [[0, 1], [1, 2, 100], [2, 3], [3, 4], [4, 5]]嵌套列表应该是这样的:[[0, 1], [1, 2, 100], [2, 3], [3, 4], [4, 5]]

Your output looks like its in json formatting, but without knowing the type of data object it is for sure I can't be certain.你的 output 看起来像它的 json 格式,但不知道数据类型 object 我肯定不能确定。 If it is json, you might be able to do json.loads(res) and then parse it that way.如果它是 json,您可以执行 json.loads(res) 然后以这种方式解析它。 https://www.freecodecamp.org/news/python-json-how-to-convert-a-string-to-json/ https://www.freecodecamp.org/news/python-json-how-to-convert-a-string-to-json/

Part of your confusion stems from a misunderstanding of what you get back from scanString.您的部分困惑源于对从 scanString 返回的内容的误解。 scanString (which is a legacy compatibility name for the new method scan_string) is actually a generator, which yields a tuple for each found match. scanString(这是新方法 scan_string 的遗留兼容性名称)实际上是一个生成器,它为每个找到的匹配项生成一个元组。 The tuple contains:元组包含:

  • the ParseResults returned from matching some text to the scan expression将一些文本与扫描表达式匹配返回的 ParseResults
  • the starting position of where the text was found找到文本的起始 position
  • the ending position of where the text was found找到文本的结尾 position

If you don't want the position values, use search_string instead, which will just return a list of the ParseResults for each match.如果您不想要 position 值,请改用 search_string,它将只返回每个匹配项的 ParseResults 列表。

Check out the online docs for more details on how to work with scan_string, search_string, ParserElements, and ParseResults.查看在线文档,了解有关如何使用 scan_string、search_string、ParserElements 和 ParseResults 的更多详细信息。 https://pyparsing-docs.readthedocs.io/en/latest/ https://pyparsing-docs.readthedocs.io/en/latest/

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

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