dkfr.net
当前位置:首页 >> python list 嵌套 >>

python list 嵌套

def iseven(l): for i in l: if i%2: return False return True def only_evens(ll): r=[] for i in ll: if iseven(i): r.append(i) return r print only_evens([[1, 2, 4], [4, 0, 6], [22, 4, 3], [2]])Python 2.7 执行结果 [[4, 0, 6], [2]] ...

import itertoolslst_all = [['A','Blue'],['A','Green'],['A','Yellow'],['B','Red'],['C','Red'],['C','White']]collector = []for k, lstgroup in itertools.groupby(sorted(lst_all), lambda x: x[0]): collector.append([k, ','.join([c[1]...

递归就好了吧。 temp_list1 = [['a','1'],['b','2'],['c','11'],['d', '22']] list_out = [] def printInfoList(temp_list1): for info in temp_list1: # print info if type(info) == types.ListType : printInfoList(info) else: list_out.app...

在确定list里的所有元素都是字符串的前提下,用字符串中的join()函数就可以实现: str = "".join(lst) #""即空字符串

def translist(l): result=[] for element in l: if type(element)==type([]): result.extend(translist(element)) else: result.append(element) return result

# -*- coding: utf-8 -*-import operatortarget_list = [["A",3],["B",2],["C",1]]'''sorted默认为从小到大排序,如果从大到小,请使用:target_list.sort(key=operator.itemgetter(1), reverse=True) '''target_list.sort(key=operator.itemgett...

循环读取每个元素,然后建立一个空list list.append()进行添加元素

def to_list(slist): dlist = list() for item in slist: if isinstance(item, list): dlist += to_list(item) else: dlist.append(item) return dlist

>>> def flat_list(data):result = []for i in data:if isinstance(i, list):result.extend(flat_list(i))else:result.append(i)return result>>> flat_list([1,[2,3,[4,5,6],7,8],9])[1, 2, 3, 4, 5, 6, 7, 8, 9]

import itertools a = [[1,2,3],[4,5,6], [7], [8,9]] out = list(itertools.chain.from_iterable(a))

网站首页 | 网站地图
All rights reserved Powered by www.dkfr.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com