Python Forum
Help add for loop results in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help add for loop results in a list
#1
Hi all,

I'm new to Python. I have this :
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> for x in get_xml.findall('physicaldisk'):
...     diskType =x.find('diskType').text
...     print(diskType)
...
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
HardDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
FlashDisk
M2Disk
M2Disk
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
PMEM
>>>
I want add all these values (remove duplicate) to a LIST. How to do that in python?
Reply
#2
If you want to get rid of duplicates, think "set". Just add() things to it and it will only store the unique elements.

>>> diskTypes = set()
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("FlashDisk")
>>> diskTypes.add("HardDisk")
>>> diskTypes
{'FlashDisk', 'HardDisk'}
Reply
#3
Or a Counter dictionary. That gets you all the types and a count of how many of each type.

Something like this:
Output:
>>> from collections import Counter >>> import xml.etree.ElementTree as ET >>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml') >>> get_xml = xml_to_parse1.getroot() >>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')]) >>> print(disktypes)
Reply
#4
(Mar-09-2022, 04:04 AM)deanhystad Wrote: Or a Counter dictionary. That gets you all the types and a count of how many of each type.

Something like this:
Output:
>>> from collections import Counter >>> import xml.etree.ElementTree as ET >>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml') >>> get_xml = xml_to_parse1.getroot() >>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')]) >>> print(disktypes)


When I run this I got many "Elements" words as below:

Quote:>>> from collections import Counter
>>> import xml.etree.ElementTree as ET
>>> xml_to_parse1 = ET.parse('/tmp/xmlfile.xml')
>>> get_xml = xml_to_parse1.getroot()
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physiccaldisk')])
>>> disktypes = Counter([x.find('diskType') for x in get_xml.findall('physicaldisk')])
>>> print(disktypes)
Counter({<Element 'diskType' at 0x7f62fdfd6ed0>: 1, <Element 'diskType' at 0x7f62fdfd60d0>: 1, <Element 'diskType' at 0x7f62fdfc3ed0>: 1, <Element 'diskType' at 0x7f62fe00c110>: 1, <Element 'diskType' at 0x7f62fe00c590>: 1, <Element 'diskType' at 0x7f62fdfc3190>: 1, <Element 'diskType' at 0x7f62fe0389d0>: 1, <Element 'diskType' at 0x7f62fdfd6450>: 1, <Element 'diskType' at 0x7f62fe00ca10>: 1, <Element 'diskType' at 0x7f62fe038250>: 1, <Element 'diskType' at 0x7f62fe02da90>: 1, <Element 'diskType' at 0x7f62fe02d310>: 1, <Element 'diskType' at 0x7f62fe018350>: 1, <Element 'diskType' at 0x7f62fe038d90>: 1, <Element 'diskType' at 0x7f62fdfc3b50>: 1, <Element 'diskType' at 0x7f62fe07fc10>: 1, <Element 'diskType' at 0x7f62fe018c50>: 1, <Element 'diskType' at 0x7f62fe023a10>: 1, <Element 'diskType' at 0x7f62fdfc34d0>: 1, <Element 'diskType' at 0x7f62fdfccd10>: 1, <Element 'diskType' at 0x7f62fdfcc610>: 1, <Element 'diskType' at 0x7f62fdfcc990>: 1, <Element 'diskType' at 0x7f62fe023590>: 1, <Element 'diskType' at 0x7f62fdfd67d0>: 1, <Element 'diskType' at 0x7f62fe038610>: 1, <Element 'diskType' at 0x7f62fe02de50>: 1, <Element 'diskType' at 0x7f62fe023110>: 1, <Element 'diskType' at 0x7f62fe00ce90>: 1, <Element 'diskType' at 0x7f62fe02d6d0>: 1, <Element 'diskType' at 0x7f62fdfc37d0>: 1, <Element 'diskType' at 0x7f62fdfd6b50>: 1, <Element 'diskType' at 0x7f62fdfcc290>: 1, <Element 'diskType' at 0x7f62fe023e90>: 1, <Element 'diskType' at 0x7f62fe0187d0>: 1})
Reply
#5
x.find('diskType').text  # Missed the .text
I did say "Something like this"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For Loop Returning 3 Results When There Should Be 1 knight2000 12 4,136 Sep-27-2021, 03:18 AM
Last Post: SamHobbs
  Appending to list of list in For loop nico_mnbl 2 2,373 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 2,399 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Adding loop results as rows in dataframe Shreya10o 2 2,201 May-09-2020, 11:00 AM
Last Post: Shreya10o
  How to append one function1 results to function2 results SriRajesh 5 3,177 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  loop through list or double loop 3Pinter 4 3,456 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,766 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,960 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,359 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020