Python Forum
Assigning an item from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning an item from a list
#1
Hi

I have a list that contains:

images (in this case 2) 00000000.jpg', '00000001.jpg
bounding box coordinates (example 20.5, 214.5, 8.0, 17.0)
and description to which picture do the bounding boxes belong to (in this case a bounding box is either part of the first or second image and is denoted by 0 or 1)

Now I want to make a new list that would print out

0000000.jpg, 20.5, 214.5, 8.0, 17.0
0000000.jpg, 61.5, 174.5, 10.0, 15.0
0000000.jpg 151.5, 43.5, 10.0, 20.0
....
0000001.jpg, 39.5, 13.5, 9.5, 15.0
....

['00000000.jpg', '00000001.jpg', 0, 20.5, 214.5, 8.0, 17.0, 0, 61.5, 174.5, 10.0, 15.0, 0, 151.5, 43.5, 10.0, 20.0, 0, 161.5, 231.5, 7.0, 10.0, 0, 186.5, 159.5, 8.0, 11.0, 0, 201.5, 115.5, 13.0, 9.0, 1, 15.5, 154.0, 8.5, 9.5, 1, 39.5, 13.5, 9.5, 15.0, 1, 100.5, 55.5, 7.5, 11.0, 1, 119.5, 217.5, 7.0, 9.0, 1, 166.5, 150.5, 15.0, 12.0, 1, 214.5, 223.5, 22.0, 11.0]
Does anyone have any suggestion on how to do it?
Reply
#2
better to use a dictionary for this, example:
class SimpleDictionary:
    def __init__(self):
        # dictionary only contains part of your posted values
        self.images = {
            '00000000.jpg': {
                'Coordinates': [
                    [20.5, 214.5, 8.0, 17.0],
                    [ 61.5, 174.5, 10.0, 15.0],
                    [151.5, 43.5, 10.0, 20.0]
                ]
            },
            '00000001.jpg': {
                'Coordinates': [
                    [39.5, 13.5, 9.5, 15.0]
                ]
            }
        }

    def show_coordinates(self):
        for imgname, coordinates in self.images.items():
            print(f"\nimage name: {imgname}")
            for name, boundings in coordinates.items():
                for item in boundings:
                    print(f"    {item}")

if __name__ == '__main__':
    sd = SimpleDictionary()

    sd.show_coordinates()
results:
Output:
image name: 00000000.jpg [20.5, 214.5, 8.0, 17.0] [61.5, 174.5, 10.0, 15.0] [151.5, 43.5, 10.0, 20.0] image name: 00000001.jpg [39.5, 13.5, 9.5, 15.0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Finding string in list item jesse68 8 1,880 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to easily create a list of already existing item CompleteNewb 15 3,549 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Assigning a new value to variable uriel 1 1,609 Dec-04-2021, 02:59 PM
Last Post: Underscore
  Remove an item from a list contained in another item in python CompleteNewb 19 5,744 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,478 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,881 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 2,371 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 1,982 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  assigning a variable :( gr3yali3n 0 1,323 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Ignoring a list item hank4eva 2 2,135 Aug-17-2020, 08:40 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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