python修改的打印函数

2024-10-04 03:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我列出了5个表示标记(词汇类)的列表——标识符、常量、运算符、关键字和分隔符

从.txt文件中读取,我将文件元素排序到列表中

我的.txt文件

a273 = 4 + 1337
variable = 50.123132123 + 3.123123132 / 23.121212

a273被追加到标识符“”中,“\n”被追加到分隔符中,=被追加到运算符中,4和1337被追加到常量中,+被追加到运算符中。(下一行相同)

所以我想做一个修改后的打印函数,它可以像这样打印我的元素:

('a273', identifiers)
(' ', separators)
('=', operators)
(' ', separators)
('4', constants)
(' ', separators)
('+', operators)
(' ', separators)
('1337', constants)

所有行都这样打印

我尝试了类似的方法(仅用于标识符)

for i in newList:
       if i in identifiers:
           print(i, " identifiers")

但我打印了多个相同的标识符

这是我对分隔符、标识符和常量进行排序的代码

lexicalClass = file.readlines()

for lex in lexicalClass:

    if len(re.findall('\s+', lex)):
            separators.extend(re.findall('\s+', lex))
        
a_string = lex.split()
for word in a_string:
     if len(re.findall(r"\b(?!S+)[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]? 
     \d+)?\b(?!S+)", word)):
        constants.extend(re.findall(r"\b(?!S+)[+-]? *(?:\d+(?:\.\d*)?|\.\d+) 
        (?:[eE][+-]?\d+)?\b(?!S+)", word))

newList = re.findall('\S+', lex)
for element in newList:
    if len(re.findall('[a-z]+[0-9]+|[a-z]+', element)):
        identifiers.extend(re.findall('[a-z]+[0-9]+|[a-z]+', element))

编辑:

我添加了以下代码:

tokens = identifiers.copy()
tokens.extend(operators)
tokens.extend(separators)
tokens.extend(keywords)
tokens.extend(constants)  

for i in range(len(lexicalClass) - 1):
    print(f'Line {i}: ')
    for element in tokens:
        if element in operators:
            print({element}, 'operators')
        elif element in identifiers:
            print({element}, 'identifers')
        elif element in separators:
            print({element}, 'identifers')
    print("\n")

但是我得到了这个输出

Line 0: 
{'a273'} identifers
{'i'} identifers
{'aifj'} identifers
{'variable'} identifers
{'+'} operators
{'+'} operators
{'++'} operators
{'/'} operators
{'='} operators
{'='} operators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators

Line 1: 
{'a273'} identifers
{'i'} identifers
{'aifj'} identifers
{'variable'} identifers
{'+'} operators
{'+'} operators
{'++'} operators
{'/'} operators
{'='} operators
{'='} operators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{' '} separators
{'\n'} separators

我想一行一行地打印它的元素,但我要在每一行打印整个文件,而且元素也没有排序(所有标识符都是先打印的,然后是运算符…)


Tags: inreforif标识符elementprinttokens
2条回答

您可以利用python中的set概念

s = set(newList)
for i in s:
       if i in identifiers:
           print(i, " identifiers")

在这里,列表中的所有唯一元素都将添加到集合中。因此,这将删除输出中的重复值

另一种稍有不同的方法是按“word”解析“word”行,然后边打印边打印。您当前使用一个正则表达式一次性解析整行。相反,请使用^{}将该行拆分为其所有标记,然后分析每个标记:

import re

s = """a273 = 4 + 1337
variable = 50.123132123 + 3.123123132 / 23.121212"""

separators = []
constants = []
operators = []
identifiers = []

for line in s.splitlines():
    for token in re.split(r"(\s+)", line):
        if re.match(r"\s+$", token):
            separators.append(token)
            kind = "separator"
        elif re.match(r"\d+(.\d+)?$", token):
            constants.append(token)
            kind = "constant"
        elif re.match(r"[a-z]+([0-9]+)?$", token):
            identifiers.append(token)
            kind = "identifier"
        elif re.match(r"[=+/*-]$", token):
            operators.append(token)
            kind = "operator"
        else:
            kind = None

        print(f"({token}, {kind})")

这将打印:

('a273', identifier)
(' ', separator)
('=', operator)
(' ', separator)
('4', constant)
(' ', separator)
('+', operator)
(' ', separator)
('1337', constant)
('variable', identifier)
(' ', separator)
('=', operator)
(' ', separator)
('50.123132123', constant)
(' ', separator)
('+', operator)
(' ', separator)
('3.123123132', constant)
(' ', separator)
('/', operator)
(' ', separator)
('23.121212', constant)

在跑步结束时,您还将填写列表。例如:

>>> print(constants)
['4', '1337', '50.123132123', '3.123123132', '23.121212']

相关问题 更多 >