列表操作
需求:现在有一个list ,range_data = ['m', 'M', 'h', 'c', 'X', 'Z', 'A', 'o']希望得到两个list, 一个是奇数list =['m', 'h', 'X', 'A'],另一个是偶数 list:['M', 'c', 'Z', 'o']
方法一:
range_data = ['m', 'M', 'h', 'c', 'X', 'Z', 'A', 'o']
list_a = []
list_b = []
for i in range_data:
lem_a = range_data.index(i)
if (lem_a%2) == 0:
list_a.append(i)
a = a + 1
else:
list_b.append(i)
a = a + 1
print list_a
print list_b
方法二:
range_data = ['m', 'M', 'h', 'c', 'X', 'Z', 'A', 'o']
print range_data[::2]
print range_data[1::2]
输出结果:
['m', 'h', 'X', 'A']
['M', 'c', 'Z', 'o']
将列表均分:
one_data_list=[1,2,3,4,5,6,7,8,9]变为
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
方法一:
def test1(one_data_list,colnum=3):
'''
将一维的列表转化为矩阵形式
'''
res_list=[]
for i in range(0,len(one_data_list),colnum):
res_list.append(one_data_list[i:i+colnum])
return res_list
print test1(one_data_list,colnum=3)
方法二:
def test2(one_list,c=3):
'''
将一个长度为n的列表划分 ,每个子列表中包含m个元素
'''
return [one_list[i:i+c] for i in range(len(one_list)) if i%c==0]
print test2(one_data_list,c=3)
比较列表的元素是否相同(包括顺序)
import operator
li_1 = ["a", "b", "d", "c"]
li_2 = ["a", "b", "d", "c"]
print(operator.eq(li_1,li_2))
元素和顺序都相同返回True,否则返回False
# python拆分多层嵌套列表(多维数据转化为一维数据)
方法一:
import itertools
a = [[1, 2], [3, 4], [5, 6]]
print(list(itertools.chain.from_iterable(a)))
out:[1, 2, 3, 4, 5, 6]
方法二:
import operator
from functools import reduce
a = [[1,2,3], [4,6], [7,8,9,8]]
print(reduce(operator.add, a))
out:[1, 2, 3, 4, 6, 7, 8, 9, 8]
方法三:
a = [[1,2,3], [5, 8], [7,8,9]]
l=[]
for m in range(0,3):
for i in a[m]:
l.append(i)
print(l)
out:[1, 2, 3, 5, 8, 7, 8, 9]
方法四:
from itertools import chain
b=[[1,2,3], [5,8], [7,8,9]]
c=list(chain(*b))
print(c)
out:[1, 2, 3, 5, 8, 7, 8, 9]
方法五:
a=[[1,2,3], [5,8], [7,8,9]]
a= eval('['+str(a).replace(' ','').replace('[','').replace(']','')+']')
print(a)
out:[1, 2, 3, 5, 8, 7, 8, 9]
方法六:
def flatten(a):
if not isinstance(a, (list, )):
return [a]
else:
b = []
for item in a:
b += flatten(item)
return b
if __name__ == '__main__':
a = [[[1,2],3],[4,[5,6]],[7,8,9]]
print(flatten(a))
out:[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法七:
ab = [[1,2,3], [5,8], [7,8,9]]
print([i for item in ab for i in item])
out:[1, 2, 3, 5, 8, 7, 8, 9]
列表嵌套字典,按每个字典中相同的value进行分类,并按顺序组装为所需列表
tmp_list = [
{
"num": "1",
"count": "caocao"
},
{
"num": "1",
"count": "liubei"
},
{
"num": "1",
"count": "xiangyu"
},
{
"num": "2",
"count": "caocao"
},
{
"num": "2",
"count": "liubei"
},
{
"num": "2",
"count": "xiangyu"
}
]
ret = int(len(tmp_list) / 2)
list_1 = []
j = 0
while j <= ret - 1:
for i in tmp_list:
if tmp_list[j]["count"] == i["count"]:
list_1.append(i)
j += 1
print(list_1)
out:[{'num': '1', 'count': 'caocao'}, {'num': '2', 'count': 'caocao'}, {'num': '1', 'count': 'liubei'}, {'num': '2', 'count': 'liubei'}, {'num': '1', 'count': 'xiangyu'}, {'num': '2', 'count': 'xiangyu'}]
print([list_1[i:i + 2] for i in range(len(list_1)) if i % 2 == 0])
out:[[{'num': '1', 'count': 'caocao'}, {'num': '2', 'count': 'caocao'}], [{'num': '1', 'count': 'liubei'}, {'num': '2', 'count': 'liubei'}], [{'num': '1', 'count': 'xiangyu'}, {'num': '2', 'count': 'xiangyu'}]]
列表常用数据处理
最新推荐文章于 2024-06-24 09:30:00 发布