F
F
freecam2021-07-28 20:42:02
Python
freecam, 2021-07-28 20:42:02

How to output a list with curly braces?

You cannot use the set function to convert the sequence to a set.
Hello.
Exercise:

unique(sequence): returns a set with all the elements of sequence, but only one copy, so it should behave as the set function. You cannot use the set function for this implementation.

def unique(sequence):
   r = []
   for elem in sequence:
      if(elem not in r):
         r.append(elem)
   return r

Output: {"banana", "apple", "orange"}
How can I return a list with curly braces? Or do you need to make a line in which the data will be enclosed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gennady S, 2021-07-28
@gscraft

You are not easy to understand. Why is curly braces needed? set is not a function, but a class, try deriving type(set). When you call s = set() you create an instance of the dataset class, with set(list()) you convert the list into a set . If the task involves creating a data type similar to set with unique values, then it is somewhat more complicated than what you are doing, and curly braces have nothing to do with it. You need to implement a class with methods that add values ​​and check for uniqueness. Moreover, this is done in different ways, for example:

class MySet(object):
  def __init__(self):
    self.items = list()

  def add(self, item):
    try:
       self.items.index(item)
       self.items.append(item)
    except ValueError as e:
       pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question