冬雪雪冬 发表于 2017-9-8 07:55:40

Python:每日一题 95(答题领鱼币)

有一个多行的字符串,要给每行的开头加上数字,这个数字代表其后紧接着的与本行内容相同的行数。例如
FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python
FishC
结果为
(3)FishC
(2)FishC
(1)FishC
(0)FishC
(1)Little turtle
(0)Little turtle
(0)Python
(0)FishC


我的解法:
**** Hidden Message *****

baicel21314 发表于 2017-9-8 08:49:40

how happy to this

sunnychou 发表于 2017-9-8 08:52:13

{:5_91:}

BngThea 发表于 2017-9-8 09:47:03

def addhead(list1):
    count = 0
    for i in range(0,len(list1)):
      for j in range(i + 1,len(list1)):
            if list1 == list1:
                count += 1
      print('(' + str(count) + ')' + list1)
      count = 0


list2 = ['FishC', 'FishC', 'FishC', 'FishC', 'Little turtle', 'Little turtle', 'Python', 'FishC']
addhead(list2)
      

白牙 发表于 2017-9-8 09:49:46

本帖最后由 白牙 于 2017-9-8 10:58 编辑

list0= ['FishC', 'FishC', 'FishC', 'FishC', 'Little turtle', 'Little turtle', 'Python', 'FishC']
fuben_list=list0.split(',')
next = list1[-1]
list[-1]='(0)'+list1[-1]
for i in range(-2,-len(list1)-1,-1):
    if list1==next:
      count+=1
    else:
      count=0
    next=list1
    list0='(%d)'%conut+list
print('\n'.join(list1))

一条漂浮的枫叶 发表于 2017-9-8 10:21:28

学习一下

bush牛 发表于 2017-9-8 11:02:17

from itertools import groupby
a = '''
FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python
FishC
'''
s =
b =
for i in range(len(b)):
    for j in range(len(b)):
      print(len(b)-1-j,b)

sky 发表于 2017-9-8 11:05:16

print("\n".join(f"({s.count(c)}){c}" for i,c in enumerate(s)))
{:10_279:}你看我屌不屌
py3.6以上

ChrisYang 发表于 2017-9-8 11:27:04

s='''FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python\nFishC'''
ss=s.split('\n')
t=[]#全是0
for i in range(len(ss)):
    t.append(0)


for i in range(1,len(ss)):   
    if ss[-i-1]==ss[-i]:
      t[-i-1]=1+t[-i]
      
for i in range(len(ss)):
    print("("+str(t)+")"+str(ss))

————————————————————————————————
运行结果
===================== RESTART: D:\Python36-32\caogao.py =====================
(3)FishC
(2)FishC
(1)FishC
(0)FishC
(1)Little turtle
(0)Little turtle
(0)Python
(0)FishC
>>>

BngThea 发表于 2017-9-8 13:15:32

def addhead(list1):
    count = 0
    for i in range(0,len(list1)):
      for j in range(i + 1,len(list1)):
            if list1 == list1:
                count += 1
            else:
                break
      print('(' + str(count) + ')' + list1)
      count = 0


list2 = ['FishC', 'FishC', 'FishC', 'FishC', 'Little turtle', 'Little turtle', 'Python', 'FishC']
addhead(list2)

之前没看到题目要求是紧跟着的

jerryxjr1220 发表于 2017-9-8 18:12:48

本帖最后由 jerryxjr1220 于 2017-9-8 18:17 编辑

from itertools import groupby

solomonxian 发表于 2017-9-8 18:46:18

按换行符拆分,分组,按成员的数量贴上数字,再拼接起来
import itertools as it

def fun(s):
    lst = ["".join(("(%d)"%k, i,"\n")) for i,j in it.groupby(s.split('\n')) for k in range(len(list(j))-1,-1,-1)]
    return "".join(lst)[:-1]

ChrisYang 发表于 2017-9-8 18:56:33

哈哈,天天都有鱼币那

wc365 发表于 2017-9-9 00:19:16

a = '''
FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python
FishC
'''
list1 = a.split(sep='\n')
list2 = ['(0)' + list1[-1]]
for i in range(2,len(list1)+1):
    if list1[-i] != list1[-i+1]:
      list2.append('(0)' + list1[-i])
      n = 0
    else:
      n += 1
      list2.append('('+str(n)+')'+list1[-i])
for each in list2[::-1]:
    print(each)

醉挽丶南风 发表于 2017-9-9 13:48:35

s1='''FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python
FishC'''
s1=s1.split('\n')
num=len(s1)
for i in range(num):
    count=0
    for j in range(i+1,num):
      if s1==s1:
            count+=1
      else:
            break
    s1='('+str(count)+')'+s1
print('\n'.join(s1))

mintaka 发表于 2017-9-9 16:36:37

def num(list,n):
    m=0
    if n==len(list)-1:
      return m
    for i in range(n+1,len(list)):
      if list==list:
            m+=1
      else:
            return m


list='''FishC
FishC
FishC
FishC
Little turtle
Little turtle
Python
FishC'''
list=list.split('\n')
for i in range(0,len(list)):
      print '('+str(num(list,i))+')'+list

樱花冷雨 发表于 2017-9-9 18:03:42

(3)FishC
(2)FishC
(1)FishC
(0)FishC
(1)Little turtle
(0)Little turtle
(0)Python
(0)FishC
为什么FishC有两个0?
怎样换成升(降)序不要零?{:5_92:}

左手十字 发表于 2017-9-9 21:37:03

words='''Fishc
Fishc
Fishc
Fishc
Little turtle
Little turtle
Python
FishC'''
from collections import Counter
ss= dict(Counter(words.split('\n')))
for keys in ss:
        for i in range(ss):
                print('(%s)'%str(ss-i-1),keys)

结果(3) Fishc
(2) Fishc
(1) Fishc
(0) Fishc
(1) Little turtle
(0) Little turtle
(0) Python
(0) FishC

苏格拉顶 发表于 2017-9-10 02:36:55

strVal = """
    FishC
    FishC
    FishC
    FishC
    Little turtle
    Little turtle
    Python
    FishC
"""
newStrVal = strVal.split('\n')
strlist =
length = len(strlist)

for i in range(length):
    j = i+1 if i+1<length else length
    n = 0
    for j in range(j,length):
      if strlist == strlist:
            n += 1
      else:
            break
    print("(%d)%s" % (n,strlist))

Greenland 发表于 2017-9-10 10:53:59

def counteRepeatedLines(string):
    '给多行字符串的每行开头加上紧随其后重复的行数'
    counter = 0
    lines = []
    splits = string.split('\n')
    for i in range(len(splits)):
      if i+1 == len(splits) or splits != splits:
            while counter > 0:
                lines.append('(%d)'% counter + splits)
                counter -= 1
            lines.append('(0)' + splits)
      else:
            counter += 1
    return '\n'.join(lines)

print(counteRepeatedLines('FishC\nFishC\nFishC\nFishC\nLittle turtle\nLittle turtle\nPython\nFishC'))
页: [1] 2 3
查看完整版本: Python:每日一题 95(答题领鱼币)