I'm wondering what is the Python way to perform the following - Given a set :
s = {'s1','s2','s3'}
I would like to perform something like :
s.addToAll('!')
to get
{'s1!','s2!','s3!'}
Thanks!
I'm wondering what is the Python way to perform the following - Given a set :
s = {'s1','s2','s3'}
I would like to perform something like :
s.addToAll('!')
to get
{'s1!','s2!','s3!'}
Thanks!
For an actual set:
>>> s = {'s1','s2','s3'}
>>> {x + '!' for x in s}
set(['s1!', 's2!', 's3!'])
That method is 2.7+, If you are using Python 2.6 you would have to do this instead:
>>> s = set(['s1','s2','s3'])
>>> set(x + '!' for x in s)
set(['s1!', 's2!', 's3!'])
[x + '!' for x in s]tuple() it still.tuple(x + '!' for x in s), submit that if you want, it could be the answertuple constructor accepts an iterable to create a new tuple from it. That works with a list, tuple([x + '!' for x in s]), as you have shown. In Python if you remove the square brackets in a function call it uses a generator expression instead, which evaluates it's items lazily.You can try this:
>>> s = ['s1','s2','s3']
>>> list(i + '!' for i in s)
[i + '!' for i in s] list comps instead of list constructor