Py学习  »  Python

修改列表中包含特定数字(而不仅仅是字符)的元素的python方法

Bowen Liu • 4 年前 • 637 次点击  

我在写一个函数“simplify”来简化多项式 simplify("2xy-yx") 可以返回 "xy" , simplify("-a+5ab+3a-c-2a") 可以返回 "-c+5ab" 等等。

我在合并相同单项式的阶段,但某些单项式的系数 +1 -1 . 我想把它们改成 + - . 但我不能直接删除1否则值为 +12 -18 将更改为 +2 -8 分别是。

例如

input = '3xy+y-2x+2xy'

到目前为止,我的过程告诉我:

Var = ['xy', 'y', 'x']
Coe = ['+5', '+1', '-2']

然后我得到了一个中间结果: IR = ['+5xy', '+1y', '-2x']

我目前的解决方案:

[(e[0]+e[2:]) if (e[1]== '1' and e[2].isalpha() ) else e for e in IR ]

目前看来这项工作是为我做的,但看起来很笨重。我想知道是否有一个更干净和更简洁的方法来实现同样的目标。最好不用regex。

谢谢。

编辑:这个问题的代码。我还在调试呢。

def simplify(poly):

    #If the first coefficient is positive, then I add a + for calculation later on
    if poly[0] !='-':
        poly = '+'+poly
    L = list(poly)

    #Put each monomial in a list as an element
    Temp, e = [], ''
    for i in L:
        if i != '+' and i != '-':
            e += i
        else:
            Temp.append(e)
            e = ''
            e += i

    #The last one will be left out and the first one will be None
    Temp.append(e)
    Temp.pop(0)

    #If the monomial only has a + or - in front of it, then give it a '1' so it's easier for calculation
    SortAndGiveOne = [''.join(e[0] + '1' + e[1:]) if not e[1].isdigit() else e for e in Temp]

    #Try to get letters from each element of the list
    Var = [''.join(sorted(i for i in e if i.isalpha())) for e in SortAndGiveOne]
    #Try to get coefficients from each element of the list
    Coe = [''.join(i for i in e if not i.isalpha()) for e in SortAndGiveOne]

    #Calculation of equivalent monomials
    newvar = []
    newcoe = []
    for va, co in zip(Var, Coe):
        try:
            ind = newvar.index(va)
            newcoe[ind] = str(int(newcoe[ind]) + int(co))
        except ValueError:
            newvar.append(va)
            newcoe.append(co)

    # Put the new lists together
    Put_t = list(map(lambda x,y : y + x, newvar, newcoe))

    # Add the plus sign as it will be gone if there was calculation involved.
    FinalSign = ['+'+ e if e[0] != '+' and e[0] != '-' else e for e in Put_t]

    #Delete the elements with 0 as coefficient
    Delete0 = [e for e in FinalSign if not e[1]=='0']

    #Change the +1 and -1 coefficients to + and - 
    Change1 = [(e[0]+e[2:]) if (e[1]== '1' and e[2].isalpha() ) else e for e in Delete0 ]

    #Sort the list based on length and then lexi order
    FS_sort = sorted(Change1, key = lambda s: (len(''.join(filter(str.isalpha, s))), (''.join(filter(str.isalpha, s)))))

    #Join together as a list
    JT = ''.join(FS_sort)

    #Delete leading plus sign
    if JT[0] == '+':
        JT = JT[1:]

    return JT
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43214
 
637 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Rocky Li
Reply   •   1 楼
Rocky Li    5 年前

看来你只是想修改 ['5xy', '+1y', '-2x'] ['5xy', '+y', '-2x'] ,等等。在这种情况下,我将使用regex:

import re
newlist = [re.sub('(?<=[+-])1(?=[A-Za-z])', '', e) for e in your_list]

测试:

k = ['5xy', '+1y', '-2x', '-1xy', '+11xy']
newlist = [re.sub('(?<=^[+-])1(?=[A-Za-z])', '', e) for e in k]
>>> newlist
['5xy', '+y', '-2x', '-xy', '+11xy']

不过,这看起来并不比你的解决方案好多少。

Daniel Mesejo
Reply   •   2 楼
Daniel Mesejo    5 年前

假设可能有两位数以上的数字,则代码会被窃听,例如:

IR = ['5xy', '51y', '-2x']
result = [(e[0]+e[2:]) if (e[1]== '1' and e[2].isalpha() ) else e for e in IR ]
print(result)

返回:

['5xy', '5y', '-2x']

一个可能更像蟒蛇的方法是使用一个函数来代替:

def transform(e):
    multiplier = ''.join(c for c in e if c.isnumeric())
    return e[0] + e[2:] if multiplier == '1' else e


IR = ['5xy', '51y', '-2x']
result = [transform(e) for e in IR]
print(result)

IR = ['5xy', '+1y', '-2x']
result = [transform(e) for e in IR]
print(result)

产量

['5xy', '51y', '-2x']
['5xy', '+y', '-2x']

注意,transforms假设数字是整数,但是可以很容易地修改为包括float(只包括对 . )