Python:每日一题82(答题领鱼币)
本帖最后由 冬雪雪冬 于 2017-8-19 20:16 编辑题目:
有一个长度不一的有10个子列表的二维列表,如下面的例子(这里为了好看,没有有列表的形式显示)
A0 A1 A2 A3 A4 A5 A6 A7 A8
B0 B1 B2 B3 B4 B5
C0
D0 D1 D2 D3 D4
E0 E1 E2 E3 E4
F0 F1 F2 F3 F4 F5 F6 F7 F8
G0
H0 H1 H2 H3 H4 H5 H6 H7 H8
I0 I1 I2 I3 I4 I5
J0 J1
看上去有许多空位,编写一个函数,使列表的元素下落,成为下面的型式,形成新的列表。
A0
B0
C0 A1
D0 B1 A2 A3 A4
E0 D1 B2 B3 B4
F0 E1 D2 D3 D4 A5
G0 F1 E2 E3 E4 B5
H0 H1 F2 F3 F4 F5 A6 A7 A8
I0 I1 H2 H3 H4 H5 F6 F7 F8
J0 J1 I2 I3 I4 I5 H6 H7 H8
即列表为:
[['A0'], ['B0'], ['C0', 'A1'], ['D0', 'B1', 'A2', 'A3', 'A4'], ['E0', 'D1', 'B2', 'B3', 'B4'], ['F0', 'E1', 'D2', 'D3', 'D4', 'A5'], ['G0', 'F1', 'E2', 'E3', 'E4', 'B5'], ['H0', 'H1', 'F2', 'F3', 'F4', 'F5', 'A6', 'A7', 'A8'], ['I0', 'I1', 'H2', 'H3', 'H4', 'H5', 'F6', 'F7', 'F8'], ['J0', 'J1', 'I2', 'I3', 'I4', 'I5', 'H6', 'H7', 'H8']]
写出函数:
drop(list1)
接收一个二维列表作为参数,返回一个二维列表。
为了随机生成原始列表,可以参考下面的程序:
import random
list1 = [[] for i in range(10)]
for i in range(10):
for j in range(random.randint(1, 9)):
list1.append(chr(65 + i) + str(j))
**** Hidden Message ***** 坐等大佬 本帖最后由 jerryxjr1220 于 2017-8-20 16:49 编辑
感觉这题用numpy的转置应该会比较简单,或者自己写个转置函数,然后上下行合并就变成同行合并就简单了,只提供思路。 jerryxjr1220 发表于 2017-8-20 16:47
感觉这题用numpy的转置应该会比较简单,或者自己写个转置函数,然后上下行合并就变成同行合并就简单了,只 ...
是的,用numpy会很方便。也可以用zip做转置。 3个步骤:碾平;分组;获取,
假设输入都是二维数组,元素都合格
import itertools as it
def drop(a):
flat_a =
new_a = ),lambda x:x)]
return [::-1]
不用工具库,自己找就比较麻烦了
不过逻辑差不多就是了
def drop2(a):
result = []
nums = sorted({i for j in a for i in j})
new_a = sorted(,key=lambda x:x)[::-1]
while new_a:
temp=[]
for i in nums:
for j in range(len(new_a)):
if i in new_a:
temp.append(new_a.pop(j))
break
result.insert(0,temp)
return result 本帖最后由 solomonxian 于 2017-8-20 19:16 编辑
看完答案······感觉被坑了{:10_258:} 看答案 本帖最后由 chunchun2017 于 2017-8-24 09:30 编辑
def drop(list1):
list2=
list3=[]
j = 0
for i in list1:
list2=len(i)
j+=1
while(len(list2)):
a=list1.pop(list2.index(min(list2)))
list3.append(a)
list2.pop(list2.index(min(list2)))
return list3
import random
list1 = [[] for i in range(10)]
for i in range(10):
for j in range(random.randint(1, 9)):
list1.append(chr(65 + i) + str(j))
print(drop(list1))
============================================
运行结果:
[['D0'], ['H0'], ['F0', 'F1', 'F2'], ['I0', 'I1', 'I2', 'I3'], ['C0', 'C1', 'C2', 'C3', 'C4'], ['G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6'], ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8'], ['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8'], ['E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8'], ['J0', 'J1', 'J2', 'J3', 'J4', 'J5', 'J6', 'J7', 'J8']]
学习了 def drop(ls):
ls_len = []
for each in ls:
ls_len.append(len(each))
if ls_len == sorted(ls_len):
return ls
for i in range(len(ls)-1,0,-1):
if len(ls) < len(ls):
j = len(ls)-len(ls)
ls += ls[-j:]
ls = ls[:-j]
return drop(ls) 本帖最后由 shigure_takimi 于 2017-12-6 09:41 编辑
def drop(lst):
a = sorted(lst, key = lambda s:len(s))
for i in a:
print(i)
a = [['A0'], ['B0'], ['C0', 'A1'], ['D0', 'B1', 'A2', 'A3', 'A4'],
['E0', 'D1', 'B2', 'B3', 'B4'], ['F0', 'E1', 'D2', 'D3', 'D4', 'A5'],
['G0', 'F1', 'E2', 'E3', 'E4', 'B5'], ['H0', 'H1', 'F2', 'F3', 'F4', 'F5', 'A6', 'A7', 'A8'],
['I0', 'I1', 'H2', 'H3', 'H4', 'H5', 'F6', 'F7', 'F8'],
['J0', 'J1', 'I2', 'I3', 'I4', 'I5', 'H6', 'H7', 'H8']]
drop(a)
## 输出:
['A0']
['B0']
['C0', 'A1']
['D0', 'B1', 'A2', 'A3', 'A4']
['E0', 'D1', 'B2', 'B3', 'B4']
['F0', 'E1', 'D2', 'D3', 'D4', 'A5']
['G0', 'F1', 'E2', 'E3', 'E4', 'B5']
['H0', 'H1', 'F2', 'F3', 'F4', 'F5', 'A6', 'A7', 'A8']
['I0', 'I1', 'H2', 'H3', 'H4', 'H5', 'F6', 'F7', 'F8']
['J0', 'J1', 'I2', 'I3', 'I4', 'I5', 'H6', 'H7', 'H8']
# 看错了,以为只是按元素长度排序呢,原来还要drop,改天再改吧。
def drop(L):
for i in range(9):
if len(L)>len(L):
n=len(L)
for j in range(len(L),len(L)):
L.append(L.pop(n))
L=[['A0','A1','A2','A3','A4','A5','A6','A7','A8'],['B0','B1','B2','B3','B4','B5'],['C0'],['D0','D1','D2','D3','D4'],['E0','E1','E2','E3','E4'],['F0','F1','F2','F3','F4','F5','F6','F7','F8'],['G0'],['H1','H2','H3','H4','H5','H6','H7','H8'],['I0','I1','I2','I3','I4','I5'],['J0','J1']]
index=0
while index!=9:
index=0
drop(L)
for i in range(9):
if len(L)<=len(L):
index += 1
print(L) ... 有些答案看不懂
i for j in a for i in j这个怎么看,i和j又是什么 思路是將原列表直行的讀取
再放進新列表中而已
def drop(list1):
list2 = [[] for i in range(10)]
for i in range(10):
k=9
for j in range(9, -1, -1):
try:
list2.append(list1)
k-=1
except:
continue
return list2 def drop(a):
for n in range(len(a)):
for i in range(len(a)-1,0,-1):
k=len(a)
if k<len(a):
a=a+a
a=a def drop(a):
candrop=True
while candrop:
candrop=False
for i in range(len(a)-1,0,-1):
k=len(a)
if k<len(a):
candrop=True
a=a+a
a=a 本帖最后由 永恒的蓝色梦想 于 2019-8-20 11:13 编辑
def drop(lis):
length=len(lis)-1;flag=1
while flag:
flag=0
for i in range(length):
while len(lis)>len(lis):lis.append(lis.pop());flag=1
return lis import random
list1 = [[] for i in range(10)]
for i in range(10):
for j in range(random.randint(1, 9)):
list1.append(chr(65 + i) + str(j))
print(list1)
def f_82(lst1: list):
lst = [[] for _ in range(9)]
lst2 = [[] for _ in range(10)]
for i in range(0, 10):
for j in range(9, -1, -1):
try:
if list1 == chr(65 + j) + str(i):
lst.insert(0, list1)
except:
continue
for i in reversed(range(10)):
for j in range(10):
try:
lst2.append(lst.pop())
except:
continue
return lst2
for i in f_82(list1):
print(i) 康康答案
页:
[1]
2