0

I have a set containing names that begin with either A or with B. I want to separate the names into two sets, one containing names beginning with A and another beginning with B. Can somebody tell me the ways of solving this?

Here is my code

a = set()
b = set()
names = {'anand', 'bianca', 'benjamin', 'ayesha', 'brian', 'adarsh'}
print(names)

for ele in names:
      if ele == "a":
           a.add(ele)
      else:
           b.add(ele)

print("Set a contains : ", a)
print("Set b contains : ", b)
Muskaan
  • 15
  • 7
  • 1
    That _is_ how, but your condition is wrong - `ele == "a"` clearly does **not** meet the requirement _"begins with either A or with B"_. – jonrsharpe Sep 09 '21 at 07:19
  • I have tried this as well but it is giving an error - SyntaxWarning: "is" with a literal. Did you mean "=="? if ele is "a": – Muskaan Sep 09 '21 at 07:23
  • That's also clearly not the right condition, see https://stackoverflow.com/q/132988/3001761. This isn't really a programming problem, you need to think about what "beginning with" actually means - "a" begins with a, sure, but so do many other strings. – jonrsharpe Sep 09 '21 at 07:26
  • 1) Are you just asking "Why is my code not working", or also "Is there a more elegant way to do this"?, 2) Can words start with other letters than `a` or `b` as well? – tobias_k Sep 09 '21 at 07:28
  • Now I want that "Is there a more elegant way to do this"? – Muskaan Sep 09 '21 at 07:34
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 15 '21 at 00:28

3 Answers3

1

Assuming word start either with a or b:

names = {'anand', 'bianca', 'benjamin', 'ayesha', 'brian', 'adarsh'}
a = set(filter(lambda x: x.startswith('a'), names))
b = names.difference(a)

If there are other possibilities:

names = {'anand', 'bianca', 'benjamin', 'ayesha', 'brian', 'adarsh'}
a = set(filter(lambda x: x.startswith('a'), names))
b = set(filter(lambda x: x.startswith('b'), names))

output:

>>> a
{'adarsh', 'anand', 'ayesha'}
>>> b
{'benjamin', 'bianca', 'brian'}

NB. this requires the elements of the set to be strings, else you need to check the type first

mozway
  • 194,879
  • 13
  • 39
  • 75
0

The reason is you are just checking if ele == a while you should be checking if ele startswith a as below:

a = set()
b = set()
names = {'anand', 'bianca', 'benjamin', 'ayesha', 'brian', 'adarsh'}
print(names)

for ele in names:
      if ele.startswith("a"):
           a.add(ele)
      elif ele.startswith("b"):
           b.add(ele)

print("Set a contains : ", a)
print("Set b contains : ", b)

Output:

{'anand', 'bianca', 'benjamin', 'brian', 'adarsh', 'ayesha'}
Set a contains :  {'anand', 'adarsh', 'ayesha'}
Set b contains :  {'brian', 'benjamin', 'bianca'}
Bhagyesh Dudhediya
  • 1,800
  • 1
  • 13
  • 16
-1

Here is an alternative using itertools.groupby. The output is a dictionary, not separate variables:

from itertools import groupby
{k:set(v) for k,v in groupby(sorted(names), lambda x: x[:1])}

output:

{'a': {'adarsh', 'anand', 'ayesha'}, 'b': {'benjamin', 'bianca', 'brian'}}

NB. I am not sure if the sorted step is necessary

mozway
  • 194,879
  • 13
  • 39
  • 75
  • Neat, but not sure if this always works without `sorted`ing the set first. Iteration order of elements in the set seems to be by hash code, which just works out in this case, but might not for other elements (and definitely not for other conditions). Alternatively: `for x in names: res[x[0]].add(x)` (`res` being `dict` or `defaultdict`) – tobias_k Sep 09 '21 at 07:30
  • Well, I am unable to understand this as I haven't reached lambda yet. Could you please explain the entire thing ? @mozway – Muskaan Sep 09 '21 at 07:31
  • `lambda x: x[:1]` takes the first (if any) character of the strings and `itertools.groupby` will use it to make groups. @tobias_k I'll look into it, but I'm pretty sure this is correct – mozway Sep 09 '21 at 07:33
  • In fact, it does not even work for this condition. On my system, the iteration order seems to be different, `list(names)` being `['benjamin', 'anand', 'adarsh', 'ayesha', 'bianca', 'brian']`, thus the first group is just `{benjamin}`, which is later silently overwritten with `{bianca, brian}`. (not my downvote, as I said, the basic idea is neat) – tobias_k Sep 09 '21 at 07:35
  • @tobias_k ok, thanks for the feedback, I guess it might be dependent on the python version, I updated the answer. However the initial input is a set not a list (for which sorted is for sure required) – mozway Sep 09 '21 at 07:36