1

I have a string as follows:

names = "name:fred, name:wilma, name:barney, name2:gauss, name2:riemann"

let's say the string names has name and name2 attributes.

How do I write a function, is_name_attribute(), that checks if a value is a name attribute? That is is_name_attribute('fred') should return True, whereas is_name_attribute('gauss') should return False.

Also, how do I create a comma separated string comprising of only the name attributes i.e.,

"fred, wilma, barney" 
2
  • Did you mean "is_name_attribute('gauss')" in your example? Commented Jun 24, 2010 at 13:07
  • @Constantin: good call; I've updated. Commented Jun 24, 2010 at 13:36

4 Answers 4

5

Something like this:

>>> names = "name:fred, name:wilma, name:barney, name2:gauss, name2:riemann"
>>> pairs = [x.split(':') for x in names.split(", ")]
>>> attrs = [x[1] for x in pairs if x[0]=='name']
>>> attrs 
['fred', 'wilma', 'barney']
>>> def is_name_attribute(x):
...     return x in attrs
...
>>> is_name_attribute('fred')
True
>>> is_name_attribute('gauss')
False
Sign up to request clarification or add additional context in comments.

Comments

0

There are other ways of doing this (as you'll see from the answers) but perhaps it's time to learn some Python list magic.

>>> names = "name:fred, name:wilma, name:barney, name2:gauss, name2:riemann"
>>> names_list = [pair.split(':') for pair in names.split(', ')]
>>> names_list
[['name', 'fred'], ['name', 'wilma'], ['name', 'barney'], ['name2', 'gauss'], ['name2', 'riemann']]

From there, it's just a case of checking. If you're looking for a certain name:

for pair in names_list:
    if pair[0] == 'name' and pair[1] == 'fred':
        return true
return false

And joining just the name versions:

>>> new_name_list = ','.join([pair[1] for pair in names_list if pair[0] == 'name'])
>>> new_name_list
'fred,wilma,barney'

Comments

0

Simple regexp matching:

>>> names = re.compile ('name:([^,]+)', 'g')
>>> names2 = re.compile ('name2:([^,]+)', 'g')
>>> str = "name:fred, name:wilma, name:barney, name2:gauss, name2:riemann"
>>> 'fred' in names.findall(str)
True
>>> names.findall(str)
['fred', 'wilma', 'barney']

Comments

-1

I think writing als this stuff in a String is not the best solution, but:

import re

names = "name:fred, name:wilma, name:barney, name2:gauss, name2:riemann"

def is_name_attribute(names, name):
    list = names.split()
    compiler = re.compile('^name:(.*)$')
    for line in list:
        line = line.replace(',','')
        match = compiler.match(line)
        if match:
            if name == match.group(1):
                return True
    return False

def commaseperated(names):
    list = names.split()
    compiler = re.compile('^name:(.*)$')
    commasep = ""
    for line in list:
        line = line.replace(',','')
        match = compiler.match(line)
        if match:
            commasep += match.group(1) + ', '
    return commasep[:-2]

print is_name_attribute(names, 'fred')
print is_name_attribute(names, 'gauss')
print commaseperated(names)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.