我有两个以下类型的列表:
C=[[a1,b1,c1,a2,c2],[d1,e1,f1,d2,f2]]
我希望能够像这样生成一个列表C:
ES_READONLY
有什么想法吗?
答案 0 :(得分:2)
您可以使用zip()
函数获取列,然后使用collections.OrderedDict
保留前一个订单中的唯一元素:
>>> from collections import OrderedDict
>>> d=OrderedDict()
>>> [d.fromkeys(i+j).keys() for i,j in zip(A,B)]
[['a1', 'b1', 'c1', 'a2', 'c2'], ['d1', 'e1', 'f1', 'd2', 'f2']]
答案 1 :(得分:2)
此解决方案不需要排序也不需要索引
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
def loop():
Bit = 7
Numbers = [a, b, c, d, e, f, g, h];
Num = 128
Input = int(input("What Number do you want to convert?" ))
while Input > 0:
if Input > Num:
Input = Input - Num
Numbers[Bit] = 1
else:
Numbers[Bit] = 0
Bit = Bit - 1
Num = Num/2
Numbers = str(Numbers)
print (Numbers)
loop()
它产生
C = [ea+[e for e in eb if e not in ea] for ea,eb in zip(A,B)]
答案 2 :(得分:0)
作为列表理解:
import operator
c = [sorted(list(set(a + b)), key=operator.itemgetter(1, 0)) for a, b in zip(A, B)]
或作为循环:
import operator
results = []
for a, b in zip(A, B):
uniques = list(set(a + b))
# Here we do two sorts in one call:
results.append(sorted(uniques, key=operator.itemgetter(1, 0)))
print(results)
[['a1', 'b1', 'c1', 'a2', 'c2'], ['d1', 'e1', 'f1', 'd2', 'f2']]
答案 3 :(得分:0)
C = []
# zip the lists to be able to loop through both
# at the same time.
# make from A and B sets to be able to make a union of
# A and B. Convert the union set to list to sort it and
# to append to the main list
for i, (m, n) in enumerate(zip(A,B)):
C.append(sorted(list(set(A[i]) | set(B[i]))))
print(C)
**output**
[['a1', 'a2', 'b1', 'c1', 'c2'], ['d1', 'd2', 'e1', 'f1', 'f2']]