冬雪雪冬 发表于 2017-10-22 19:42:57

Python:每日一题 115

首先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。

题目:
我们知道用add方法可以给集合增加元素,如:
>>> set1 = {'qwe', 'asd', 'z'}
>>> set1.add('123')
>>> set1
{'123', 'qwe', 'asd', 'z'}

当add已有元素时,则不会有任何变化,这也是集合唯一性的表现,现在有一个全是字符串的集合,你设计程序,当加入一个字符串是集合中已有元素时,会自动在字符串后面加上“_1”再加入元素中,如
set1 = {'qwe', 'asd', 'z'}
#加入'qwe',实际加入qwe_1,集合变成
{'qwe_1', 'qwe', 'asd', 'z'}
进一步,如果已有'qwe'和'qwe_1',再加入'qwe'时,实际加入'qwe_2',以此类推。

BngThea 发表于 2017-10-22 21:33:36

set1 = {'qwe_1','qwe', 'asd', 'z'}


adds = input("请输入想要加入的元素:")

count = 0
for each in set1:
    if adds in each:
      count += 1
if count:
    adds += '_%d' % count

print("原set为:",set1)

set1.add(adds)

print("原set变为:",set1)

ooxx7788 发表于 2017-10-22 21:35:14

本帖最后由 ooxx7788 于 2017-10-23 09:04 编辑

def foo(se,strng):
    import re
    if strng not in se:
      se.add(strng)
    else:
      if re.search(r'(.*?)_\d*', strng):
            s,i = re.findall(r'(.*?)_(\d*)',strng)
            strng = '{0}_{1}'.format(s,int(i)+1)
            return foo(se, strng)
      else:
            strng = strng + '_1'
            return foo(se, strng)

不过如果出现本身就是_\d*的文本好像有点问题,应该要增加一个是不是从1开始的判断,有点小复杂呢,不改了。

SixPy 发表于 2017-10-23 00:09:57

本帖最后由 SixPy 于 2017-10-23 00:14 编辑

class SET(set):
    def add(self,x):
      n,x1 = 0,x
      while x in self:
                n += 1
                x = '%s_%d'%(x1,n)
      super().add(x)
                        
s=SET('abc')
print(s)
s.add('c')
print(s)
s.add('c')
print(s)

wyp02033 发表于 2017-10-23 10:09:45

def my_add(the_set, st):
    if st not in the_set:
      the_set.add(st)

    else:
      i = 1
      st += "_%d" % i
      while st in the_set:
            i += 1
            st = st.split('_') + '_%d' % i

      the_set.add(st)

    return the_set

solomonxian 发表于 2017-10-27 20:39:29

很少碰到用到类的题目,不熟,重写方法不用super就感觉要无限循环{:10_269:}
class New_set(set):
    def add(self, value):
      if value not in self:
            super().add(value)
      else:
            length = len(self)
            i = 1
            while len(self) == length:
                super().add("".join())
                i += 1

shigure_takimi 发表于 2017-12-2 03:08:33

def addSet(aSet, something):
    if isinstance(something, str):
      if something not in aSet:
            aSet.add(something)
      else:
            n = 1
            while something+'_'+str(n) in aSet:
                n += 1
            aSet.add(something+'_'+str(n))
    return aSet

set1 = {'qwe', 'asd', 'z'}
for i in range(10):
    print(addSet(set1, 'qwe'))

>>>
{'z', 'qwe', 'qwe_1', 'asd'}
{'qwe_2', 'z', 'qwe', 'qwe_1', 'asd'}
{'z', 'qwe_2', 'qwe', 'qwe_1', 'qwe_3', 'asd'}
{'z', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd'}
{'z', 'qwe_5', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd'}
{'z', 'qwe_6', 'qwe_5', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd'}
{'z', 'qwe_6', 'qwe_5', 'qwe_7', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd'}
{'z', 'qwe_6', 'qwe_5', 'qwe_7', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd', 'qwe_8'}
{'qwe_9', 'z', 'qwe_6', 'qwe_5', 'qwe_7', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd', 'qwe_8'}
{'qwe_9', 'z', 'qwe_10', 'qwe_6', 'qwe_5', 'qwe_7', 'qwe_2', 'qwe', 'qwe_1', 'qwe_4', 'qwe_3', 'asd', 'qwe_8'}

PYTHON90小菜鸟 发表于 2018-1-1 15:47:01

def Add(set1,element):
    i=0
    length_0=len(set1)
    set1.add(element)
    length_1=len(set1)
    while length_0==length_1:
      i+=1
      set1.add(element+'_'+str(i))
      length_1=len(set1)
      
    return set1

set1={'qwe','asd','z'}
y_n=None

while y_n!='no':
    element=input('Please enter the element whilch you want to join:')
    print(Add(set1,element))
    y_n=input('Do you want to enter again :')
    y_n.lower()


yjsx86 发表于 2018-2-3 11:18:07

text = {'qwe_1', 'qwe', 'asd', 'z','qwe_2'}
def setAdd(string):
    before = len(text)
    text.add(string)
    last = len(text)
    temp = []
    if last == before:
      for x in text:
            if string in x:
                n = x
                temp.append(int(n if n else 0))
      text.add('%s_%d' % (string,max(temp)+1))

setAdd('qwe')
setAdd('asd')
setAdd('3721')

#result
{'z', 'asd_1', '3721', 'asd', 'qwe_1', 'qwe_3', 'qwe_2', 'qwe'}

majia1015 发表于 2018-2-25 19:03:35

set1 = {'majia','weiwei'}
x = 1

while True:

    element = str(input('> '))
    '''判断元素是否属于这个集合成员,如果属于就添加后缀'''
    if element in set1:
      element = element + '_' + str(x)
      set1.add(element)
      x += 1
            
    else:
      set1.add(element)
      break   
      
    print(set1)
   
print(set1)

victor.xu 发表于 2018-4-1 11:00:40

def add_element(sett,elem):
    counter = 0
    for i in sett:
      if i.count(elem) == 1:
            counter += 1
    if counter:
      suffix = '%d' % (counter)
      elem = elem + '_' + suffix
      sett.add(elem)
    else:
      sett.add(elem)
    return sett
set1 = {'qwe', 'asd', 'z', 'qwe_1', 'qwe_2'}
lis = ['123', 'qwe', 'qwe_1', 'qwe_2']
for j in lis:
    result = add_element(set1,j)
    print(result)

Wangzy1025 发表于 2018-5-29 15:45:58

def iter():
    a = 0
    while True:
      a += 1
      yield a


def insert_set(str_set, str):
    if str in str_set:
      temp = str
      a = iter()
      while temp in str_set:
            temp = str + '_%d' % next(a)
      str_set.add(temp)
    else:
      str_set.add(str)


set1 = {'qwe', 'asd', 'zxc'}
insert_set(set1, 'qwe')
insert_set(set1, 'qwe')
insert_set(set1, 'qwe')
insert_set(set1, 'qwe')
insert_set(set1, 'asd')
insert_set(set1, 'tyu')
insert_set(set1, 'qwe')
print(set1)

hj5818 发表于 2019-4-6 21:59:35

set1={"qwe","asd","z"}
dict1={"qwe":0,"asd":0,"z":0}
while True:
    n=str(input("输入集合元素:"))
    if n not in set1:
      set1.add(n)
      dict1=0
    else:
      for each in set1:
            if each==n:
                dict1+=1
      set1.add(n+"_"+str(dict1))   
    print (set1)

kinkon 发表于 2022-3-7 13:14:33

from collections import Counter

set1 = {'qwe', 'asd', 'z'}


s = input("请输入想要加入的元素:")

set2 = Counter(set1)

if s in set1:
    set1.add(s + '_' + str(set2))
else:
    set1.add(s)
   
set2 += 1

print(set1)
页: [1]
查看完整版本: Python:每日一题 115