0

I have the following python code:

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()

I am getting the following output:

['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']

I want to merge these lists and remove duplicates.

2
  • And what is an expecting result? You want merge lists or their elements? Commented Jun 9, 2017 at 10:04
  • df1 is your output list ? Commented Jun 9, 2017 at 10:04

6 Answers 6

1

merging lists is so simple.

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))
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but I'm curious about the comparison between merging the lists first and then setting them, and setting the lists first and then taking their union...
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()

Above code will merge your list and remove duplicates from it.

Comments

0

You can concatenate and use set :

def disk():
    df = []
    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 = df + df1
    return set(df)

Comments

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

This will give you the output as -

[['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']]

Comments

0

You could use the set.union method:

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

a set is an unordered collection of unique objects. You'll lose the duplicates but also lose your order. In case you want it ordered and without duplicates you could use OrderedDict:

from collections import OrderedDict
from itertools import chain

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

Comments

-1

Why do you spawn separate processes for every filesystem to check, while df is able to check all of them in one run?

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

2 Comments

Although that's a good remark, this is not the point of the question. You should post this as a comment instead.
Thanks, It worked for me, i edited the code like: import subprocess def disk(): df = [] for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'): df1 = subprocess.Popen(['df','-h', '/tmp' , '/usr/mware' , '/var' , '/var/mware'], stdout=subprocess.PIPE).communicate()[0].split() df1.remove("on") print df1 disk()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.