赞
踩
list_1 = [1, 2, 3, 4, 5]
list_2 = [5, 8, 7, 1, 3, 6]
#配对
result = [(i, j) for i in list_1 for j in list_2 if i != j]
print(result)
如果运行上面的代码,则将得到以下结果。
输出结果
[(1, 5), (1, 8), (1, 7), (1, 3), (1, 6), (2, 5), (2, 8), (2, 7), (2, 1),
(2, 3), (2, 6), (3, 5), (3, 8), (3, 7), (3, 1), (3, 6), (4, 5), (4, 8),
(4, 7), (4, 1), (4, 3), (4, 6), (5, 8), (5, 7), (5, 1), (5, 3), (5, 6)]
list1 = [1,2,3]
list2 = ['a', 'b', 'c']
new_list1 = [(x,y) for x in list2 for y in list1] #组合元组列表
print new_list1
new_list2 = ["%s%d"%(x,y) for x in list2 for y in list1] #字符串组合拼接
print new_list2
输出:
[(‘a’, 1), (‘a’, 2), (‘a’, 3), (‘b’, 1), (‘b’, 2), (‘b’, 3), (‘c’, 1), (‘c’, 2), (‘c’, 3)]
[‘a1’, ‘a2’, ‘a3’, ‘b1’, ‘b2’, ‘b3’, ‘c1’, ‘c2’, ‘c3’]
它提供了一种称为product的方法,该方法将所有元素配对。找到对后,我们可以过滤对。
#导入模块
import itertools
#初始化列表
list_1 = [1, 2, 3, 4, 5]
list_2 = [5, 8, 7, 1, 3, 6]
# 配对
pairs = itertools.product(list_1, list_2)
print(pairs)
#过滤配对
result = [pair for pair in pairs if pair[0] != pair[1]]
print(result)
如果运行上面的代码,则将得到以下结果。
输出结果:
Out[0]: <itertools.product at 0x20a19b0b980> # pairs
Out[1]:
[(1, 5), (1, 8), (1, 7), (1, 3), (1, 6), (2, 5), (2, 8), (2, 7), (2, 1),
(2, 3), (2, 6), (3, 5), (3, 8), (3, 7), (3, 1), (3, 6), (4, 5), (4, 8),
(4, 7), (4, 1), (4, 3), (4, 6), (5, 8), (5, 7), (5, 1), (5, 3), (5, 6)]
ws_list
Out[1]: ['WS_150m_thies', 'WS_100m_thies', 'WS_30m_NRG']
ws_sum = list(itertools.combinations(ws_list, 2))
ws_sum
Out[2]:
[('WS_150m_thies', 'WS_100m_thies'),
('WS_150m_thies', 'WS_30m_NRG'),
('WS_100m_thies', 'WS_30m_NRG')]
equal_ws = [57.5, 60, 80, 95, 110, 130, 150, 170, 190, 210, 222.5]
a = map(lambda x,y: (x,y), equal_ws[0:-1],equal_ws[1::])
list(a)
Out[30]:
[(57.5, 60),
(60, 80),
(80, 95),
(95, 110),
(110, 130),
(130, 150),
(150, 170),
(170, 190),
(190, 210),
(210, 222.5)]
list1 = [1,2,3,4,5,6,7,8,9]
new_list = [x for x in list1 if x % 2 == 0]
print new_list
输出:
[2, 4, 6, 8]
a = [1, 2, 3, 4]
b = [4, 5, 6, 7]
print(set(a).intersection(set(b)))
输出:
Out[3]: {4}
v1 = [21, 34, 45]
v2 = [55, 25, 77]
v = list(map(lambda x: x[0]-x[1], zip(v2, v1)))
v
Out[0]: [34, -9, 32]
https://blog.csdn.net/helloxiaozhe/article/details/90722705
a = [1,2,3,4,5]
b = [5,4,3,2,1]
c = [a[i]+b[i] for i in range(0,len(a))] #range后还可以加if条件筛选
print(c)
结果为:
[-4, -2, 0, 2, 4]
>>> import numpy
>>> a = [1, 2, 3, 4]
>>> b = [5, 6, 7, 8]
>>> a_array = numpy.array(a)
>>> b_array = numpy.array(b)
>>> c_array = a_array + b_array
>>> d_array = a_array - b_array
>>> c_array
array([ 6, 8, 10, 12])
>>> d_array
array([-4, -4, -4, -4])
>>> v1 = [21, 34, 45]
>>> v2 = [55, 25, 77]
>>> v = list(map(lambda x: x[0]-x[1], zip(v2, v1)))
>>> v
[34, -9, 32]
>>> print("%s\n%s\n%s" %(v1, v2, v))
[21, 34, 45]
[55, 25, 77]
[34, -9, 32]
实现打包和解压的例子:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> zipped = zip(a,b) # 打包为元组的列表
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 与 zip 相反,可理解为解压
[(1, 2, 3), (4, 5, 6)]
a = [1,2,3]
b = [4,5,6]
c = a+b
c
输出结果:
[1,2,3,4,5,6]
a = [1,2,3]
b = [4,5,6]
a.extend(b)
a
Out[0]: [1,2,3,4,5,6]
结果是一样的,但是+号生成的是一个新的对象,而extend则是在原地的修改a对象。
例如:
>>> L1 = [1, 2, 3, 4, 5]
>>> L2 = [20, 30, 40]
>>> L1[len(L1):len(L1)] = L2
>>>
>>> L1
[1, 2, 3, 4, 5, 20, 30, 40]
用切片方法的好处在于灵活,可以在任意节点进行插入,例如从头部插入:
>>> L1 = [1, 2, 3, 4, 5]
>>> L2 = [20, 30, 40]
>>> L1[0:0] = L2
>>> L1
[20, 30, 40, 1, 2, 3, 4, 5]
添加到中间:
>>> L1 = [1, 2, 3, 4, 5]
>>> L2 = [20, 30, 40]
>>>
>>> L1[1:1] = L2
>>> L1
[1, 20, 30, 40, 2, 3, 4, 5]
zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。
利用 * 号操作符,与zip相反,进行解压。
list1 = [2,3,4]
list2 = [4,5,6]
for x,y in zip(list1,list2):
print(x, y, '--', x*y)
Out[2]
2 4 -- 8
3 5 -- 15
4 6 -- 24
v1 = {1:11, 2:22} #此处可迭代对象为字典
v2 = {3:33, 4:44}
v3 = {5:55, 6:66}
list(zip(v1,v2,v3)) # 压缩
Out[28]: [(1, 3, 5), (2, 4, 6)]
list(zip(*zip(v1,v2,v3))) # 解压
Out[27]: [(1, 2), (3, 4), (5, 6)]
height = [50, 70, 90, 100, 120, 140, 160, 180, 200, 220, 240, 260]
dataSet是一个列表,其**元素**为很多个与height等长的**列表**
dataSet2 = [list(zip(x, height)) for x in dataSet]
*是python函数可变参数的一种表示方式,加 * 的表示传入一个元祖对象进行解包。
a = [1, 2, 3, 4, 5, 6]
b = zip(*([iter(a)] * 2))
for i in b:
print(i)
Out[36]:
(1, 2)
(3, 4)
(5, 6)
list(zip([iter(a)]))
Out[35]: [(<list_iterator at 0x198cfdaea00>,)]
list(zip(*[iter(a)]))
Out[36]: [(1,), (2,), (3,), (4,), (5,), (6,)]
list(zip(*([iter(a)] * 2)))
Out[37]: [(1, 2), (3, 4), (5, 6)]
list(zip(*([iter(a)] * 3)))
Out[38]: [(1, 2, 3), (4, 5, 6)]
list([iter(a)])
Out[39]: [<list_iterator at 0x198cfdcc8e0>]
for i in [iter(a)] * 2:
print(i)
<list_iterator object at 0x00000198CFD1C9A0>
<list_iterator object at 0x00000198CFD1C9A0>
demo = [1, 2, 3, 4, 5]
list(zip(demo[:-1],demo[1:]))
Out[60]: [(1, 2), (2, 3), (3, 4), (4, 5)]
demo = [1, 2, 3, 4, 5]
list(map(lambda x: x[1] - x[0], zip(demo[:-1],demo[1:])))
Out[67]: [1, 1, 1, 1]
参考链接:
[1] python使用for生成列表 2020.9
[2] python两个列表匹配_Python-从两个列表中配对,以使元素成对不相同 2021.2
[3] Python 匹配两组(或多组)数据 2021.8
[4] python 实现两个list列表逐元素相减,实现合并两个列表,以及zip() 函数介绍 2019.5
[5] Python合并两个List 2017.4
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。