Python:每日一题 107
本帖最后由 冬雪雪冬 于 2017-10-1 22:28 编辑首先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 为了让大家独立思考,跟帖改为“回帖仅作者可见”。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
将一个由数字组成的列表,生成一个新列表,要求,如果有连续的相同数字,则只取一个,并在后面加上一个新元素--一个数字,表示相同数字的个数,其他数字照抄。
例如:
第0个元素为1,则新列表照抄,第1~3元素为2,则新列表中写2, 3,以此类推得到新列表:
from itertools import groupby
def groups(lst,new = []):
for k,v in groupby(lst):
l = len(list(v))
new.extend( if l>1 else )
return new from itertools import groupby
s =
ns = []
gs = ['']
for k, v in groupby(s):
gs = list(v)
if len(gs) > 1:
ns.append(k)
ns.append(len(gs))
else:
ns.append(k)
print(ns) in_num = input("请输入一个数字列表:")
new_num = []
temp = 1
for i in range(len(in_num)-1):
if int(in_num) - int(in_num) == 0:
temp += 1
continue
else:
new_num.append(in_num)
if temp > 1:
new_num.append(temp)
temp = 1
new_num.append(in_num)
if temp > 1:
new_num.append(temp)
print(new_num)
>>> from itertools import groupby
>>> from tkinter import _flatten as fltn
>>> a=
>>> fltn([(k,(k,v))for k,v in((k,len(list(v)))for k,v in groupby(a))])
(1, 2, 3, 1, 2, 3, 4, 2, 1, 2) list1 =
new_list = []
n = 1
for i in range(len(list1)):
if i != 12 and list1 != list1 or i ==12:
if n == 1:
new_list.append(list1)
else:
new_list.append(list1)
new_list.append(n)
n = 1
else:
n += 1
print(new_list) 本帖最后由 SixPy 于 2017-9-30 15:17 编辑
from itertools import groupby
a=
print([n for i,n in enumerate(
sum((for k,g in groupby(a)),[])
,1)if i&1|n-1]) 分组,取长度,判断,输出
from itertools import groupby
def fun(lst):
a = [(i, len(list(j))) for i,j in groupby(lst)]
return def new(list1):
list2=[ ]
k = 0
for i in range(k,len(list1)):
if list1==list1:
continue
else:
list2.append(list1)
list2.append(i-k)
k = i
print(list2)
#手机打的现无电脑验证本人初学者,请指教 考虑到列表中所有元素相同的情况list1 =
list2 = []
count = 0
for i in range(len(list1)-1):
if list1 == list1:
count += 1
else:
list2.append(list1)
if count != 0:
list2.append(count+1)
count = 0
sign = 0
if list1[-1] == list1[-2]:
for i in range(-1,-len(list1),-1):
if list1 != list1:
list2.append(list1[-1])
list2.append(-i)
sign = 1
break
else:
list2.append(list1[-1])
if sign == 0:
list2.append(list1)
list2.append(len(list1))
print(list2)
倔强幸福 发表于 2017-9-30 11:03
输入时作为一个字符串输入了,并没有作为列表输入,审题有误 from itertools import groupby
def group_by(list_x):
result = []
for a,b in groupby(list_x):
result.append(a)
l = len(list(b))
if l > 1:
result.append(l)
return result
if __name__ == '__main__':
print(group_by()) a =
def newList(lst):
result = ] # 第一个元素比然放进去
count = 1 # 第一个元素至少1个
for index in range(1, len(lst)):
if lst == lst:
count += 1 # 如果和前面数字相同则个数加1
if index == len(lst)-1: # 因为最后一个元素后面没有数字可以比较
result.append(count) # 需要手动将个数加入result
else:
if count > 1: # 如果个数大于1个则加入个数,否则照抄
result.append(count)
result.append(lst) # 如果和前面的数字不同,重新开始计数
count = 1
return result
print(newList(a))
>>>
def fun(num_list):
list1 = []
k = 0
while k < len(num_list)
num = 1
j = num_list
for i in range(k+1,len(num_list)):
k = i-1
if j == num_list:
num += 1
else:
break
if num == 1:
list1.append(num_list)
else:
list1.extend(, num])
print(list1)
pass
num_list =
fun(num_list) def func(lis):
t=None;l=1;re=[]
for i in lis:
if i==t:l+=1
else:
if l!=1:re.append(l)
l=1;t=i;re.append(i)
if l!=1:re.append(l)
return re def fun107(nums):
ans = []
pre, cout = -1, 0
for num in nums:
if num == pre:
cout += 1
else:
if cout > 1:
ans.append(cout)
ans.append(num)
pre, cout = num, 1
if cout > 1:
ans.append(cout)
return ans
print(fun107())
页:
[1]