Python Forum
Arrange list of tuple using loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrange list of tuple using loop
#4
You're thinking to complicated.

Your structure is following:

EVENT_FRAME = [int, str, str, int, int, List[float]]
The last element is already a nested list, unpacking nested lists is easy with itertools.chain.fromiterable


Example code:
If you're working on linux:
# save data in a file, to test the reading
echo "[(10, ['cp5'], ['ERROR'], 201905021800.0, 201905021900.0, [[1.2], [1.5]]), (11, ['cp3'], ['ERROR'], 201905021800.0, 201905021900.0, [[1.2], [1.5]]), (12, ['cp7', 'cp4'], ['ERROR'], 201905021800.0, 201905021900.0, [[1.2], [1.5]])]" > nvm.txt
# now use the script to read and evaluate the data in the file
python nvm.py nvm.txt
Output:
To test, if the format is ok, you can use json.tool
python nvm.py nvm.txt | python -m json.tool
You can see a big difference in indentation. This is because the tool uses by default indent=4
You can use this also with json.dumps(your_object, indent=4)

What you need to investigate is:
  • for-loops, iteration over nested datatypes:
    data = [(1,2),(3,4),(5,6)]
    for row in data:
        print(row)
        # to see what happens
  • Argument unpacking:
    data = [(1,2),(3,4),(5,6)]
    one, two, three = data
    # is very strict, wrong number of elements -> ValueError
  • Argument unpacking with *:
    data = [(1,2),(3,4),(5,6)]
    first_element1, *rest = data
    *elements, last_element1 = data
    first_element2, *between, last_element2 = data
    Everything with a star in front, is a list and consumes as much as it can do without an Exception.
  • Sometimes you have the bad situation of nested results, but if you need each of them in one variable, you can use nested argument unpacking:
    silly_sequence = ['ok', (200, 'GET'), 'foo']
    # now we want to unpack all of them
    ok, (status, get), foo = silly_sequence
  • itertools.chain.fromiterable is used to flatten an iteable:
    data = [[1,2,3], [4,5,6], [7,8,9],[10,11,12]]
    flatten = list(itertools.chain.fromiterable(data)
  • (nested) (star) argument unpacking works also in a for-loop:
    positions = [(1,4,2), (3,2,1), (4,5,3)]
    for x, y, z in positions:
        print(x, y, z)
    
    # if you have nested iterables and know you have minimum one element or more, you can use
    # the star unpacking
    minium_length_1 = [(1,2,3,4,5,6), (16,1), [1,1.3], [1]]
    for first, *rest in minium_length_1:
        print(first, rest)
    # this will fail, if there is a non-iterable element or an empty sequence
  • Generators are good, if you have a process chain. For example parsing data line by line,
    next generator parses the line and so on. With generators you can also process memory efficient infinite sequences.
    Introduction to Python Generators
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Arrange list of tuple using loop - by batchenr - Jun-16-2019, 08:05 AM
RE: Arrange list of tuple using loop - by batchenr - Jun-16-2019, 10:27 AM
RE: Arrange list of tuple using loop - by DeaD_EyE - Jun-16-2019, 11:19 AM
RE: Arrange list of tuple using loop - by batchenr - Jun-16-2019, 02:37 PM
RE: Arrange list of tuple using loop - by Yoriz - Jun-16-2019, 03:03 PM
RE: Arrange list of tuple using loop - by Abdullah - Jun-16-2019, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 533 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 527 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,760 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 2,025 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,320 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 103,649 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,863 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,508 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,908 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Appending to list of list in For loop nico_mnbl 2 2,408 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl

Forum Jump:

User Panel Messages

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