Home > Software engineering >  Compare list of list from lines
Compare list of list from lines

Time:12-08

Model: ATA KINGSTON SH103S3 (scsi)
Disk /dev/sda: 120GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1024MB  1023MB  primary   ext4         boot
 2      1026MB  120GB   119GB   extended
 5      1026MB  120GB   119GB   logical                lvm


Model: ATA WDC WD10EZEX-00W (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  1000GB  1000GB  ext4         primary


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/system-root: 103GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End    Size   File system  Flags
 1      0.00B  103GB  103GB  ext4


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/system-swap: 16.4GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End     Size    File system     Flags
 1      0.00B  16.4GB  16.4GB  linux-swap(v1)

I have text like this, what i would like to do is

Create loop for disk in each lines start with "Model:"

Then inside for loop 3 time, that get 3 item, If line start with Model : Disk[i].model = line Elif line start with Disk Disk[i].size = line Elif line start with " 1" Disk[i].partition = line

So next

I can find which partition have keyword boot

For disk in disk[i]

If disk[i].partition contain "boot" DiskContainOs = disk[i].model.

Is it right using double for loop like this?

I

CodePudding user response:

with open('input.txt', 'r') as f:
    for line in f.readlines():
        if 'Model:' in line.strip():
            print(line)

Output:

Model: ATA WDC WD10EZEX-00W (scsi)
Model: Linux device-mapper (linear) (dm)
Model: Linux device-mapper (linear) (dm)

CodePudding user response:

We can use the Python map() function along with functools.reduce() function to compare the data items of two lists.

The map() method accepts a function and an iterable such as list, tuple, string, etc. as arguments.

It applies the passed function to each item of the iterable and then returns a map object i.e. an iterator as the result.

The functools.reduce() method applies the passed function to every element of the input iterable in a recursive manner. As a combination, the map() function would apply the input function to every element and the reduce() function will make sure that it applies the function in a consecutive manner.

  • Related