Python 列表踢出某个元素上机作业题
source link: https://www.v2ex.com/t/821251
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
请根据输入的原始列表,输出目标列表:
原始列表:[1,2,None,None,None,None,None,None,3,4,5,6,None,None,None,None,7,8,9,10]
目标列表: [1,2] [3,4,5,6] [7,8,9,10]
princelai 6 小时 30 分钟前 1
[list(g[1]) for g in groupby(ll,key=lambda x:x is not None) if g[0]]
learningman 6 小时 1 分钟前 2
ma6254 5 小时 22 分钟前
deplivesb 4 小时 55 分钟前
stimw 3 小时 46 分钟前 via Android
bytesfold 3 小时 20 分钟前
JasonEWNL 2 小时 52 分钟前
@going 话说回来鉴于是作业,或可回归不依赖任何库的一般思路,日后有机会亦能写出不一定最具效率但较易维护的代码。
```
il = [1, 2, None, None, None, None, None, None, 3, 4, 5, 6, None, None, None, None, 7, 8, 9, 10]
ol = [[]]
for i in il:
if i:
ol[-1].append(i)
elif ol[-1]:
ol.append([])
print(ol) # [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]
```
keepeye 1 小时 56 分钟前
cnrting 1 小时 47 分钟前 via iPhone
fml87 1 小时 10 分钟前
[*map(eval,re.split(",,+",re.sub("[^0-9,]", "",str(lst) )))]
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK