在python中合并列表

时间:2017-06-09 10:03:19

标签: python python-2.7 list

我有以下python代码:

import subprocess

def disk():
for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'):
    df1 = subprocess.Popen(['df','-h', i], stdout=subprocess.PIPE).communicate()[0].split()
    df1.remove("on")
    print df1

disk()

我得到以下输出:

['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']

我想合并这些列表并删除重复项。

6 个答案:

答案 0 :(得分:1)

合并列表非常简单。

a= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp']
b= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware']
c= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var']
d = ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']
remove_duplicates = list(set(a+b+c+d))

答案 1 :(得分:1)

import subprocess

def disk():
    df_set = set()
    for i in ('/tmp', '/usr/mware', '/var', '/var/mware'):
        df1 = subprocess.Popen(['df','-h', i], stdout=subprocess.PIPE).communicate()[0].split()
        df1.remove("on")
        df_set = df_set.union(set(df1))
    print(list(df_set))
disk()

上面的代码将合并您的列表并从中删除重复项。

答案 2 :(得分:0)

您可以连接并使用set

print(MyClass.get_static_field())
>>> something
obj = MyClass()
print(obj.get_static_field_with_instance())
>>> something
obj.set_static_field_with_instance("smething new")
print(obj.get_static_field_with_instance())
>>> something new

答案 3 :(得分:0)

import subprocess

# prepare a list of columns in the beginning
df_out = [['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted']]

def disk():
    for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'):
        df1 = subprocess.Popen(['df','-h', i], 
                    stdout=subprocess.PIPE).communicate()[0].split()
        df1.remove("on")

        # append the remaining columns in the list 'df1'
        df_out.append(df1[6:])

disk()

# print the final output
print df_out

这将为您输出 -

[['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted'],
 ['/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp'],
 ['/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware'],
 ['/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var'],
 ['/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']]

答案 4 :(得分:0)

您可以使用set.union方法:

list(set(df1).union(df2, df3, df4))  # assuming the invidual lists are called df1, df2, ...

set唯一对象的无序集合。您将丢失重复项,但也会丢失您的订单。如果您想要订购并且没有重复,您可以使用OrderedDict

from collections import OrderedDict
from itertools import chain

list(OrderedDict.fromkeys(chain(a, b, c, d)))

答案 5 :(得分:-1)

为什么要为每个要检查的文件系统生成单独的进程,而df能够在一次运行中检查所有这些进程?

df1 = subprocess.Popen(['df','-h', '/tmp' , '/usr/mware' , '/var' , '/var/mware'], stdout=subprocess.PIPE).communicate()[0].split()