Hi All, I need one more help in string manipulation.. I am having a line as follows:

variable (x1|y1|z1)(x2||z2)(x3|y3|z3)

From this I need to take values and store in lists like

xList = [x1, x2, x3]
yList = [y1, 0, y3]
zList = [z1, z2, z3]

Please help me to acheive this.. Thanks..

Recommended Answers

All 3 Replies

what have you tried already?
http://www.daniweb.com/forums/announcement114-2.html

I am new to Python and now learning. This is somethig i did in some other language and just tried to do in Python (NOT assaignment:cool: ). This is what I did.

import re
import string

class enumerate_list:
      def __init__(self, seq):
           self.seq = seq
      def __getitem__(self, inx):
           return inx, self.seq[inx]

class StringManipulation(object):
    def __init__(self, line):
        self.line = line
        self.xList = []; self.yList = []; self.zList = []

    def getFuncName(self):
        MatchFunc = re.match(r'(^.*) \((.*)', self.line, re.M)
        return MatchFunc.group(1)

    def getxList(self):
        xStr = self.line.partition(' ')[2]
        xStr = filter(lambda x: x != " ", xStr)
        xStr = xStr.split(")")
        xStr = [item.strip("(") for item in xStr]
        xStr = filter(lambda x: x != "\n", xStr)
        xDict = {}
        for i, ch in enumerate_list(xStr):
            xDict[i] = ch
        for i in range(len(xDict)):
            self.xList.append(xDict[i].partition('|')[0])
            yRem = xDict[i].partition('|')[2]
            self.yList.append(yRem.partition('|')[0])
            zRem = yRem.partition('|')[2]
            self.zList.append(zRem.partition('|')[0])

#test.txt contains following two line, var1 (x1|y1|z1)(x2||z2)(x3|y3|z3)\n, var2 (a1|b1|c1) (a2|b3|c2) (a3||c3) (a4|b4|c4)
fh = open("test.txt", 'r')
for line in fh.readlines():
    str = StringManipulation(line)
    FuncName = str.getFuncName()
    print FuncName;                        # prints like var1, var2
    str.getxList()
    print str.xList, str.yList, str.zList; # prints like, ['x1', 'x2', 'x3'] ['y1', '', 'y3'] ['z1', 'z2', 'z3']
                                           #              ['a1', 'a2', 'a3', 'a4'] ['b1', 'b3', '', 'b4'] ['c1', 'c2', 'c3', 'c4']
fh.close()

Please tell wheather this is efficient or Is there some other way to do efficiently.

Thanks.

Well first of all, you don't need to make an enumerate_list class as there's already the built-in one enumerate which you can use on lists, strings, tuples, etc. Meaning that you could use this instead:

for i, ch in enumerate(xStr):
    xDict[i] = ch

Also, don't use str as your variable name as that's the name of Python's string object, like int , list , tuple , float , etc.

And, didn't you want any blank indices in the lists to have a zero put there? Like you showed with your yList in your first post. Just put a conditional if statement to handle that in your getxList function.

Finally, I wouldn't use partition for splitting the bracketed pair at the pipe |. Use the split function. It works like this:

s = "a|b|c|d"
t = s.split("|")
""" t -->
['a', 'b', 'c', 'd']
"""

Incorporating that into your getxList function rather than using multiple partition calls would make it much cleaner and simpler looking. Here's a crude example that you can modify to fit into your code properly, but it works and it's much simpler looking:

def getxList(self):
    line = '(x1|y1|z1)(x2||z2)(x3|y3|z3)\n' # example string
    groups = line.split(')(')
    for item in groups:
        item = item.strip().split('|')
        item = filter(lambda x: x != ' ', item)
        item = [x.strip('(').strip(')') for x in item]

Hope I helped a bit!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.