Python列表清单,删除重复项

时间:2018-04-12 04:31:46

标签: python list

尝试删除列表列表中的重复项并打印相同内容,不重复。

原始列表

a = [['country',['America_1','America_2','America_3','America_4','England_5','England_6'],['apple_1_more','orange_1_more']],['country',['Brazil_2','Brazil_3','Brazil_1','Brazil_4','Mexico_1','Mexico_3','Mexico_2'],['grapes_1_less','banana_1_more']]]

寻找输出:

[['country', ['America', 'England'], ['orange_more', 'apple_more']], ['country', ['Mexico', 'Brazil'], ['grapes_less', 'banana_more']]]

但获得:

[['country', ['America', 'England'], ['orange_more', 'apple_more']], ['country', ['America', 'England', 'Mexico', 'Brazil'], ['orange_more', 'grapes_less', 'banana_more', 'apple_more']]]

code ::

 a = [['country',['America_1','America_2','America_3','America_4','England_5','England_6'],['apple_1_more','orange_1_more']],['country',['Brazil_2','Brazil_3','Brazil_1','Brazil_4','Mexico_1','Mexico_3','Mexico_2'],['grapes_1_less','banana_1_more']]]
aa ={}
aaa=[]
aaaa=[]
aaaaa=[]
for i in a:
    for j in i[1]:
        j=j.split('_',1)[0]
        aaa.append(j)
    for k in i[2]:
        k=k.split('_',2)[0]+'_'+k.split('_',2)[2]
        aaaa.append(k)
    aa['country'] = [i[0],list(set(aaa)),list(set(aaaa))]
    aaaaa.append(aa['country'])
print (aaaaa)

4 个答案:

答案 0 :(得分:4)

使用列表推导,将每个子列表中的第二项与set()进行转换:

a = [['country',['America','America','America','America','England','England']],['country',['Brazil','Brazil','Brazil','Brazil','Mexico','Mexico','Mexico']]]

a = [[i, list(set(j))] for i, j in a]
print(a)

输出:

[['country', ['England', 'America']], ['country', ['Brazil', 'Mexico']]]

这可能无法保留内部列表的顺序,因为集合是无序的,因此您可能需要考虑到这一点。

答案 1 :(得分:0)

您可以尝试这种方法:

import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(["Dehli","Ahmadabad","Kolkata",'Bangalore','Pune','Maduraj'])
G.add_edge('Dehli', 'Ahmadabad', length =890)
G.add_edge('Dehli', 'Kolkata', length =1422)
G.add_edge('Dehli', 'Bangalore', length =2168)
G.add_edge('Dehli', 'Pune', length =1450)
G.add_edge('Dehli', 'Maduraj', length = 2603)

G.add_edge('Ahmadabad', 'Kolkata', length =1902)
G.add_edge('Ahmadabad', 'Bangalore', length =1403)
G.add_edge('Ahmadabad', 'Pune', length =624)
G.add_edge('Ahmadabad', 'Maduraj', length =1807)
G.add_edge('Kolkata', 'Bangalore', length =1778)
G.add_edge('Kolkata', 'Pune', length =1782)
G.add_edge('Kolkata', 'Maduraj', length =2043)

G.add_edge('Bangalore', 'Pune', length =824)
G.add_edge('Bangalore', 'Maduraj', length =404)
G.add_edge('Pune', 'Maduraj', length =1195)



nx.draw(G,pos,node_color='k')

path = nx.shortest_path(G,source='Dehli',target='Pune')
path_edges = zip(path,path[1:])
nx.draw_networkx_nodes(G,pos,nodelist=path,node_color='g')
nx.draw_networkx_edges(G,pos,edgelist=path_edges,edge_color='g',width=7)
print(nx.shortest_path_length(G,source='Dehli',target='Pune'))
plt.show()

输出:

a = [['country',['America','America','America','America','England','England']],['country',['Brazil','Brazil','Brazil','Brazil','Mexico','Mexico','Mexico']]]



print(list(map(lambda x:[x[0],list(set(x[1:][0]))],a)))

你的变量名称非常令人困惑,我尝试了新的方法,你可以试试这个:

[['country', ['England', 'America']], ['country', ['Mexico', 'Brazil']]]

输出:

a = [['country',['America_1','America_2','America_3','America_4','England_5','England_6'],['apple_1_more','orange_1_more']],['country',['Brazil_2','Brazil_3','Brazil_1','Brazil_4','Mexico_1','Mexico_3','Mexico_2'],['grapes_1_less','banana_1_more']]]


final_data=[]
for i in a:
    sub_data=[]

    for j in i[1:]:
        d = {}

        for m in j:
            data=m.split('_')[0]
            d[data]=data

        sub_data.append(list(d.keys()))
    final_data.append(['country',*sub_data])
print(final_data)

如果您的数据格式总是如此,那么您可以尝试:

更新

[['country', ['America', 'England'], ['orange', 'apple']], ['country', ['Brazil', 'Mexico'], ['banana', 'grapes']]]

输出:

a = [['country',['America_1','America_2','America_3','America_4','England_5','England_6'],['apple_1_more','orange_1_more']],['country',['Brazil_2','Brazil_3','Brazil_1','Brazil_4','Mexico_1','Mexico_3','Mexico_2'],['grapes_1_less','banana_1_more']]]


final_data=[]
for i in a:
    sub_data=[]
    sub_extra=[]

    for j in i[1:2]:
        sub_extra.append(i[2])
        d = {}

        for m in j:
            data=m.split('_')[0]
            d[data]=data

        sub_data.extend([list(d.keys()),*sub_extra])
    final_data.append(['country',*sub_data])
print(final_data)

答案 2 :(得分:0)

使用此递归函数删除多级数组中的重复项:

def dup(input_):
    if isinstance(input_, list):
        try:
            input_ = list(set([i.split('_')[0] if not isinstance(i, list) else i for i in input_]))
        except TypeError:
            pass
        for child in input_:
            input_[input_.index(child)] = dup(child)

    return input_

答案 3 :(得分:0)

我就是这样做的。

country_list1 = [a[0[0]]]
country_list2 = [a[1[0]]]
duplicates = [country for country in country_list1 in country_list2]
non_duplicates = [country for country in country_list1 not in country_list2]

这将为您提供重复的和非重复的 这是考虑两者中名称的区分大小写