替换列表列表内容的pythonic方法?

时间:2019-11-27 12:09:33

标签: python list

下面的代码生成唯一的组合:

from itertools import permutations 

comb3 = permutations([1,1,1,0,0,0] , 3) 

def removeDuplicates(listofElements):

    # Create an empty list to store unique elements
    uniqueList = []

    # Iterate over the original list and for each element
    # add it to uniqueList, if its not already there.
    for elem in listofElements:        
        elif elem not in uniqueList:
            uniqueList.append(elem)

    # Return the list of unique elements        
    return uniqueList

comb3 = removeDuplicates(comb3)

for i in list(comb3):     
    print(i)

中间输出

结果输出是一个元组列表。它将被解释为A, B, C1 = EXIST0 = NOT EXIST

(1, 1, 1)
(1, 1, 0)
(1, 0, 1)
(1, 0, 0)
(0, 1, 1)
(0, 1, 0)
(0, 0, 1)
(0, 0, 0)

转换为列表列表

将元组列表转换为列表列表并替换其内容

res = [list(ele) for ele in comb3] 

for i in list(res):   

    if(i[0] == 1):
        i[0] = 'A Exist'

    if(i[0] == 0):
        i[0] = 'A Not Exist'

    if(i[1] == 1):
        i[1] = 'B Exist'

    if(i[1] == 0):
        i[1] = 'B Not Exist'

    if(i[2] == 1):
        i[2] = 'C Exist'

    if(i[2] == 0):
        i[2] = 'C Not Exist'

显示结果

for i in list(res):   
    print(i)

最终输出

['A Exist', 'B Exist', 'C Exist']
['A Exist', 'B Exist', 'C Not Exist']
['A Exist', 'B Not Exist', 'C Exist']
['A Exist', 'B Not Exist', 'C Not Exist']
['A Not Exist', 'B Exist', 'C Exist']
['A Not Exist', 'B Exist', 'C Not Exist']
['A Not Exist', 'B Not Exist', 'C Exist']
['A Not Exist', 'B Not Exist', 'C Not Exist']

是否有一种更优雅或更好的方法来替换列表列表的内容?

5 个答案:

答案 0 :(得分:2)

>>> names = ['A', 'B', 'C']
>>> verbs = [' Not Exist', ' Exist']
>>> [[names[n] + verbs[v] for n, v in enumerate(c)] for c in comb3]
[['A Exist', 'B Exist', 'C Exist'],
 ['A Exist', 'B Exist', 'C Not Exist'],
 ['A Exist', 'B Not Exist', 'C Exist'],
 ['A Exist', 'B Not Exist', 'C Not Exist'],
 ['A Not Exist', 'B Exist', 'C Exist'],
 ['A Not Exist', 'B Exist', 'C Not Exist'],
 ['A Not Exist', 'B Not Exist', 'C Exist'],
 ['A Not Exist', 'B Not Exist', 'C Not Exist']]]

答案 1 :(得分:1)

您可以执行以下操作:

a = ("A", "B", "C")
res = [["{} {}Exist".format(x, '' if y else 'NOT ') for x, y in zip(a, sub)] for sub in comb3]

或类似的话:

a = ("A {}Exist", "B {}Exist", "C {}Exist")

res = [[x.format('' if sub[i] else 'NOT ') for i, x in enumerate(a)] for sub in lst]

或最优雅的东西:

a = [("A Not Exist", "B Not Exist", "C Not Exist"), ("A Exist", "B Exist", "C Exist")]

res = [[a[x][i] for i, x in enumerate(sub)] for sub in lst]

它们都返回:

print(res) # -> [['A Exist', 'B Exist', 'C Exist'], 
               # ['A Exist', 'B Exist', 'C NOT Exist'], 
               # ['A Exist', 'B NOT Exist', 'C Exist'], 
               # ['A Exist', 'B NOT Exist', 'C NOT Exist'], 
               # ['A NOT Exist', 'B Exist', 'C Exist'], 
               # ['A NOT Exist', 'B Exist', 'C NOT Exist'], 
               # ['A NOT Exist', 'B NOT Exist', 'C Exist'], 
               # ['A NOT Exist', 'B NOT Exist', 'C NOT Exist']]

答案 2 :(得分:1)

您可以使用set从列表中删除重复项。

然后map个他们到list

from itertools import permutations
import string
from pprint import pprint
alphabet = string.ascii_uppercase
comb3 = permutations([1,1,1,0,0,0] , 3)


comb3 = list(map(list,set(comb3)))


for i in comb3:
    for index, value in enumerate(i):
        i[index] = f'{alphabet[index]}{ " Not " if value>0 else " "}Exists'

pprint(comb3)

输出

 [['A Not Exists', 'B Not Exists', 'C Exists'],
 ['A Exists', 'B Not Exists', 'C Not Exists'],
 ['A Exists', 'B Not Exists', 'C Exists'],
 ['A Not Exists', 'B Exists', 'C Exists'],
 ['A Exists', 'B Exists', 'C Not Exists'],
 ['A Not Exists', 'B Exists', 'C Not Exists'],
 ['A Exists', 'B Exists', 'C Exists'],
 ['A Not Exists', 'B Not Exists', 'C Not Exists']]

答案 3 :(得分:1)

您只需两行即可完成所有这些操作:

comb3 = list(set(permutations([1,1,1,0,0,0] , 3))) # set will remove duplicates automatically
result = [[f"{i} {'' if j else 'NOT '}Exist" for i, j in zip(["A", "B", "C"], k)] for k in comb3]

结果将是:

[['A Exist', 'B Exist', 'C NOT Exist'],
 ['A NOT Exist', 'B Exist', 'C Exist'],
 ['A NOT Exist', 'B Exist', 'C NOT Exist'],
 ['A Exist', 'B NOT Exist', 'C NOT Exist'],
 ['A NOT Exist', 'B NOT Exist', 'C Exist'],
 ['A Exist', 'B NOT Exist', 'C Exist'],
 ['A NOT Exist', 'B NOT Exist', 'C NOT Exist'],
 ['A Exist', 'B Exist', 'C Exist']]

请注意:

  

f''适用于python3.6或更高版本。

答案 4 :(得分:1)

首先,使用排列然后将其过滤掉是低效率的。您正在寻找的是笛卡尔积。将itertools.product与重复参数一起使用,可以获得所需的中间输出。

from itertools import product
comb3 = list(product([1,0], repeat=3))
#Output:
[(1, 1, 1),
 (1, 1, 0),
 (1, 0, 1),
 (1, 0, 0),
 (0, 1, 1),
 (0, 1, 0),
 (0, 0, 1),
 (0, 0, 0)]

从这一点出发:您可以使用迭代和映射来干净地获取所需的输出,如下所示。

column_names = 'ABC' #To map all names with the number of items. We can think of these as column names.
code_mapping = {0: 'Not Exist', 1: 'Exist'} #For mapping the codes to meanings.

output = []
for item in comb3:
    row = [f"{name} {code_mapping[code]}" for name, code in zip(column_names, item)]
    output.append(row)
print(output)

输出:

[['A Exist', 'B Exist', 'C Exist'],
 ['A Exist', 'B Exist', 'C Not Exist'],
 ['A Exist', 'B Not Exist', 'C Exist'],
 ['A Exist', 'B Not Exist', 'C Not Exist'],
 ['A Not Exist', 'B Exist', 'C Exist'],
 ['A Not Exist', 'B Exist', 'C Not Exist'],
 ['A Not Exist', 'B Not Exist', 'C Exist'],
 ['A Not Exist', 'B Not Exist', 'C Not Exist']]