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 ***** how happy to this {:5_91:} 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 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))
学习一下 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) print("\n".join(f"({s.count(c)}){c}" for i,c in enumerate(s)))
{:10_279:}你看我屌不屌
py3.6以上 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
>>> 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:17 编辑
from itertools import groupby
按换行符拆分,分组,按成员的数量贴上数字,再拼接起来
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] 哈哈,天天都有鱼币那 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) 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)) 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
(3)FishC
(2)FishC
(1)FishC
(0)FishC
(1)Little turtle
(0)Little turtle
(0)Python
(0)FishC
为什么FishC有两个0?
怎样换成升(降)序不要零?{:5_92:} 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 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))
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'))