1,105,340 Community Members

Q: Extract integers from a list

Member Avatar
junoh1991
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

# I wanted to change the question.

I have a list something like this:
list1=

For this list, if the element in the list starts with N, i want '+' to be inserted before the integer. If the element in the list starts with S, I want '-' to be inserted before the integer.

For example, I want the above list to look like this:

I can't use any module. Can anyone help me with this one?


# made some correction.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

That is not list of elements any type I know.

Member Avatar
valorien
Light Poster
27 posts since Apr 2008
Reputation Points: 27 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 1 [?]
 
1
 

Here's my quick and dirty solution:

list1= ['NNW 30', 'SE 15', 'SSW 60', 'NNE 70', 'N 10']

for index, item in enumerate(list1):
    if item.startswith('N'):
        list1[index] = "+" + item[-2:]
    elif item.startswith('S'):
        list1[index] = "-" + item[-2:]

print(list1)

Notice the use of startswith(), and the negative used to slice the letters in each list item. This will not work if the number has more or less than 2 digits, but it can easily be changed to treat that as well - use that as an exercise.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

It might be safer to extract the numeric value ...

list1= ['NNW 30', 'SE 15', 'SSW 60', 'NNE 70', 'N 10']

list2 = []
for item in list1:
    s = ""
    for c in item:
        # extract numeric value
        if c in '1234567890.-':
            s += c
    if item[0] == 'N':
        s = '+' + s
    if item[0] == 'S':
        s = '-' + s
    list2.append(s)

print(list2)  # ['+30', '-15', '-60', '+70', '+10']
Member Avatar
valorien
Light Poster
27 posts since Apr 2008
Reputation Points: 27 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 1 [?]
 
0
 

Excellent solution Vegaseat, definitely better than mine.
"c in '123...'" is very useful. Thanks for sharing.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Ok, if we are going to give (again) ready answer:

>>> values = [('+' if item.startswith('N') else ('-' if item.startswith('S') else '')) + item.rsplit(' ', 1)[-1] for item in list1]
>>> values
['+30', '-15', '-60', '+70', '+10']
>>>
Member Avatar
valorien
Light Poster
27 posts since Apr 2008
Reputation Points: 27 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 1 [?]
 
0
 

Great answer Tony, though I'll need some time to fully comprehend it ;-).

What would you recommend when faced with such questions? I'm still learning the ropes here.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: