954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sorting

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 ['20','35','40'] i used append but it didnt work and need to calculate how many times a number appears please someone please help me. :sad:

wandie
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Could you show your code that doesn't work?

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

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 ['40','250']

wandie
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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', ...]
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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', '', ...]
"""
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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']
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 
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...

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 
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:

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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']
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

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?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
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

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

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

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

i think its answered in the other thread

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You