I have a program that displays its results in this manner. I added the results in an array this is what I got.

[20]
[35]
[40]
[84]
[100]
[245]
[260]
[300]
[440]
[521]
[650]

How can I put it in this way i used append but it didnt work and need to calculate how many times a number appears please someone please help me. :sad:

Could you show your code that doesn't work?

He is my code

def toR(self):
        " Convert to a readable format"
        keys = self.fields.keys()
        keys.sort()
        leader = self.get_MARC()[:24]
        print "leader:", leader
        for key in keys:
            if key == 0:
                # XXX Skip?? What are these??
                pass
            elif key < 10:
                print "00"+str(key)+":", self.fields[key][0]
            else:
                for instance in self.fields[key]:
                    if key < 100:
                        keystr = "0" + str(key)
                    else:
                        keystr = str(key)
                    ind1 = instance[0]
                    ind2 = instance[1]
                    #print keystr
                result=[key]
                
                               
                print result          
                    #+ ": ind1=" + ind1 + ", ind2=" + ind2
                    #for sub in instance[2]:
                     #  print "  " + sub[0] + ":", character(sub[1])
                        
        print

If the key is less than 10 then add 00s all I need is to put this figures into an array. The code is really big but i am working on this area. Its just the final result I would like it in this format

I am a little lost here. I assume your data comes from a file? Well, anyway, using assumptions here is some code:

def strip_brackets(txt):
    if txt[0] == "[":   # remove leading '['
        txt = txt[1:]
    if txt[-1] == "]":  # remove trailing ']'
        txt = txt[:-1]
    return txt

# assume your data string looks like this
data_raw = """[20]
[35]
[40]
[84]
[100]
[245]
[260]
[300]
[440]
[521]
[650]
"""

data_list = data_raw.split()
# test
print data_list  # ['[20]', '[35]', '[40]', '[84]', ...]
# convert to a list of numeric strings
data_list2 = [strip_brackets(x) for x in data_list]
# test
print data_list2  # ['20', '35', '40', '84', ...]

Since the data is coming from a raw source (text file, database, user input...) wouldn't it be nice if we didn't assume that the "[" will always be the first character of "txt" and "]" is not always the last character of "txt".

Given that the data can also be of the form " [ 32] " or "[32 ]". To handle such situations here is what I have come up with....

data_raw = """[20 ]
[ 35 ]
[40  ]
[84 ]
[100 ]
[ 245]
[  260]
[ 300 ]
[ 440 ]
[   521     ]
[ 650    ]
"""
data_list = data_raw.split()
print data_list  
data_list2 = [x.strip(" []") for x in data_list]
print data_list2

Since the data is coming from a raw source (text file, database, user input...) wouldn't it be nice if we didn't assume that the "[" will always be the first character of "txt" and "]" is not always the last character of "txt".

Given that the data can also be of the form " [ 32] " or "[32 ]". To handle such situations here is what I have come up with....

data_raw = """[20 ]
[ 35 ]
[40  ]
[84 ]
[100 ]
[ 245]
[  260]
[ 300 ]
[ 440 ]
[   521     ]
[ 650    ]
"""
data_list = data_raw.split()
print data_list  
data_list2 = [x.strip(" []") for x in data_list]
print data_list2

No cigars on that one s.o.s, here is the result I get:

"""
my output -->
['[20', ']', '[', '35', ']', '[40', ']', '[84', ']', ...]
['20', '', '', '35', '', '40', '', '84', '', ...]
"""

Just a little tweakery needed:

data_raw = """[20 ]
[ 35 ]
[40  ]
[84 ]
[100 ]
[ 245]
[  260]
[ 300 ]
[ 440 ]
[   521     ]
[ 650    ]
"""

list1 = data_raw.split('\n')       # <---
print "***list1***\n", list1
list1_mod = [x.strip(' []') for x in list1 if x]   # <---
print "***list1_mod***\n", list1_mod
***list1***
['[20 ]', '[ 35 ]', '[40  ]', '[84 ]', '[100 ]', '[ 245]', '[  260]', '[ 300 ]', '[ 440 ]', '[   521     ]', '[ 650    ]', '']
***list1_mod***
['20', '35', '40', '84', '100', '245', '260', '300', '440', '521', '650']
commented: Good tweakery - ~s.o.s~ +12

That will do it Jeff! HeHe, love the word "tweakery".

No cigars on that one s.o.s, here is the result

Doesn't make a difference, I don't smoke anyways...:mrgreen:

And btw Jeff, thanks for correcting my code in my absence, would have done that anyways...:p.

Maybe should run my programs before I post them here...

Welcome S.O.S to the Python crowd. We are all quite nice in Python land, just here to learn more!

Small Miss Steaks are accepted, gives the rest of us the opportunity to show off how smart we are.

Welcome S.O.S to the Python crowd. We are all quite nice in Python land, just here to learn more!

Thanks for squeezing me in...:mrgreen:

Small Miss Steaks are accepted, gives the rest of us the opportunity to show off how smart we are.

Aha..thought something was fishy the way you said "No cigars on that one ~s.o.s~"..:twisted:

assuming data is as first posted:

>>> import re
>>> re.findall(r"\[(.*)\]",data_raw)
['20 ', ' 35 ', '40  ', '84 ', '100 ', ' 245', '  260', ' 300 ', ' 440 ', '   521     ', ' 650    ']

or if they contain spaces like the other posts...

>>> [ i.strip() for i in re.findall(r"\[(.*)\]",data_raw) ]
['20', '35', '40', '84', '100', '245', '260', '300', '440', '521', '650']

This was quite a lively discussion!

A question to ghostDog74, can you use regular expression to simply extract the number from each dataline, no matter what the non-numeric stuff is?

This was quite a lively discussion!

A question to ghostDog74, can you use regular expression to simply extract the number from each dataline, no matter what the non-numeric stuff is?

hi there
sure, re.findall(r"(\d+)",data_raw)
thanks

Thanks this info ghostdog! You must be one great regex expert!

re.findall(r"(\d+)",data_raw) works great on raw_data, but when I changed 40 to 40.5 it gave me [..., '40', '5', ...]
Looks like '\d+ only works on integers.
Do you have any suggestions for floats?

Maybe I should start new thread? Did start own thread see "Extract Numbers from Data Stream"

Thanks in advance!

Henri

i think its answered in the other thread

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.