Python Forum
extract a dictionary from a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract a dictionary from a string
#5
...
...

bot = {}     #[b]in the code snippet did not give this line, but it is[/b]
 
bot['name']   = 'bot_' + str(i) 
bot['coord']  = coord          
bot['gender'] = random.choice(gender) 
bot['healt']  = 0.99                  
bot['gormon'] = 0.5                   
bot['live']   = 0                                        
bot['age']    = 0                                       
bot['speed']  = round((random.random()), 2)             
bot['orient'] = random.randint(0, 7)
bot['cargo']  = []                                       
bot['atak']   = round((random.random()), 3)              
tag1          = bot
bot['canvas'] = canvas2.create_image(bot['coord'], image = bot['image'], tags = ('bot', bot['name'], tag1))
 
...
...
 
collide = canvas2.find_overlapping(x1r, y1r, x2r, y2r)
if len(collide) > 1:
    i = 0
    for i1 in collide:
        if str(canvas2.gettags(collide[i])[1]) != str(bot['name']):
            vis = canvas2.gettags(collide[i])
                 
            print(str(name) + '  0 vis: '+str(vis[0]))
            print(str(name) + '  1 vis: '+str(vis[1]))
            print(str(name) + '  2 vis: '+str(vis[2]))
 
            if vis[0] == 'bot':
                bott = vis[2]
                print(bott.keys())
because initially bot is a dictionary and I pass it to a tag called tag1. vis is a list of all tags retrieved by canvas2.gettags (collide [i]). vis [2] is the third tag in the list of passed tags and with 'print' it is seen as a dictionary (there are key-value pairs)

I solved this problem in a workaround way - I transfer the number of the desired bot to the function (collide_bot (number)), and then, using the bot number from the list of all bots (Bots), I extract the necessary information for a specific bot
Reply


Messages In This Thread
extract a dictionary from a string - by berc - Jul-28-2020, 05:36 PM
RE: extract a dictionary from a string - by berc - Jul-29-2020, 02:04 AM
RE: extract a dictionary from a string - by berc - Jul-30-2020, 06:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,781 May-13-2022, 03:44 PM
Last Post: deanhystad
  Extract a string between 2 words from a text file OscarBoots 2 1,827 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  How to extract specific key value pair from string? aditi06 0 2,485 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Beautify dictionary without converting to string. sharoon 6 3,297 Apr-11-2021, 08:32 AM
Last Post: buran
Question How to extract multiple text from a string? chatguy 2 2,313 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Extract continuous numeric characters from a string in Python Robotguy 2 2,582 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Extract data from large string pzig98 1 2,095 Jul-20-2020, 12:39 AM
Last Post: Larz60+
  simple f-string expressions to access a dictionary Skaperen 0 1,499 Jul-15-2020, 05:04 AM
Last Post: Skaperen
  xml.etree.ElementTree extract string values matthias100 2 4,861 Jul-12-2020, 06:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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