victor.xu 发表于 2018-3-5 15:41:18

学习一下

victor.xu 发表于 2018-3-5 15:42:39

定义一个函数,遇到整型元素,直接插入结果。遇到列表元素,调用这个函数。
result = []
def nd_to_1d(lst):
    for i in lst:      
      if type(i) == int:
            result.append(i)
      elif type(i) == list:
            nd_to_1d(i)
    return result
list1 = , , [, 7], , 11], 12], 13]]
result1 = nd_to_1d(list1)
print(result1)
            

topscien 发表于 2018-3-7 12:41:03

抄作业!

Charonnnnn 发表于 2018-3-27 19:56:35

dd

jiazhiyu 发表于 2018-3-29 14:38:40

学习一下

大头目 发表于 2018-3-30 12:53:03

import re

list1 = , , [, 7], , 11], 12], 13]]
list_old = str(list1)
p = r'\d+'
list_new = re.findall(p, list_old)
list_new1 = []
for each in list_new:
        list_new1.append(int(each))
       
print(list_new1)

Even_138 发表于 2018-4-2 10:08:46

学习

凌九霄 发表于 2018-4-10 21:01:47

def uncompress(lst, newlst=[]):
    for i in lst:
      if isinstance(i, list):
            uncompress(i)
      else:
            newlst.append(i)
    return newlst

tsembrace 发表于 2018-4-11 16:24:52

'''
把一个嵌套的多维列表变为一维列表,如:
list1 = , , [, 7], , 11], 12], 13]]
变成:
'''
def funlist(l):
    listx=[]
    for x in l:
      if isinstance(x,int):
            listx.append(x)
      else:
            listx+=funlist(x)
    return listx

list1 = , , [, 7], , 11], 12], 13]]
print(funlist(list1))

小强工作室 发表于 2018-5-5 20:22:19

学习中

jrro452 发表于 2018-5-7 16:04:05

lists=, , [, 7], , 11], 12], 13]]
def delist(Lists):
    while True:
      i=0
      list2 = []
      for each in Lists:
            if isinstance(each,list):
                list2.extend(each)
                i+=1
            else:
                list2.append(each)
      Lists=list2[:]
      if i==0:
            return Lists
print(delist(lists))
结果:

不够简单···不过实在想不出更简单的了···

小庆子 发表于 2018-5-16 22:20:33

list1 = , , [, 7], , 11], 12], 13]]
str1 = str(list1).replace('[','').replace(']','').replace(',','').replace(' ',',')
print('[%s]'%str1)

萧丹夜 发表于 2018-5-17 11:23:32

list1 = , , [, 7], , 11], 12], 13]]
y = []
def decom(x):
    global y
    for each in x:
      if isinstance(each,int):
            y.append(each)
      else:
            decom(each)
    return y
      
result = decom(list1)
print(result)

CirtusJ 发表于 2018-5-18 10:57:17

入门级别来学习

insane丶 发表于 2018-5-27 15:10:16

想看大佬们的解答

z1446773686 发表于 2018-6-1 19:11:29

list1 = , , [, 7], , 11], 12], 13]]
list2=[]
str1=str(list1)
for each in str1:
    if each.isdigit():
      list2.append(each)

print(list2)

DavidCowboy 发表于 2018-8-9 14:19:35

跑得出結果,但要怎麼縮短啊,不知如何用def來做重複工作


list1 = , , [, 7], , 11], 12], 13]]
list2 = []

for i in range(len(list1)):
    temp = list1
    if type(temp) == list:
      list2.extend(temp)
    else:
      list2.append(temp)

list3 = []

for i in range(len(list2)):
    temp = list2
    if type(temp) == list:
      list3.extend(temp)
    else:
      list3.append(temp)

list4 = []

for i in range(len(list3)):
    temp = list3
    if type(temp) == list:
      list4.extend(temp)
    else:
      list4.append(temp)

list5 = []

for i in range(len(list4)):
    temp = list4
    if type(temp) == list:
      list5.extend(temp)
    else:
      list5.append(temp)

print(list5)

Akihisa 发表于 2018-8-17 08:49:16

list1 = , , [, 7], , 11], 12], 13]]
list2 = []

def get_all(list1):
    for each in list1:
      if isinstance(each,int) == True:
            list2.append(each)
      else:
            get_all(each)
    return list2
            
print(get_all(list1))

我不会啊 发表于 2018-9-6 16:35:36

萌面楠先森 发表于 2018-9-6 22:22:25

xuexi
页: 1 2 [3] 4 5
查看完整版本: Python:每日一题 23(答题领鱼币)