Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to seperate dict value?
#1
How can i seperate ip address and mac addresses??


['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
Reply
#2
Here is one way

for string in ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']:
    new_string = string
    split_string = new_string.split(',')

print(f'ip -> {split_string[0]}')
print(f'mac -> {split_string[1]}')
Output:
ip -> 192.168.1.240 mac -> fe80::350e:d28d:14a5:5cbb
On a side note, this is a list. Not a dict.
Gribouillis likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You have list with one string item in it. Obvious way would be splitting this item at comma and unpack:

>>> line = ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
>>> ip_addr, mac_addr = line[0].split(',')
>>> ip_addr
'192.168.1.240'
>>> mac_addr
'fe80::350e:d28d:14a5:5cbb'
If the result should be dictionary then one could do this:

>>> data = dict(zip(('ip_addr', 'mac_addr'), line[0].split(',')))
>>> data
{'ip_addr': '192.168.1.240', 'mac_addr': 'fe80::350e:d28d:14a5:5cbb'}
>>> data['ip_addr']
'192.168.1.240'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Two separate dataframes, two seperate programs stylingpat 2 2,005 Apr-28-2021, 07:56 PM
Last Post: stylingpat
  Seperate output buffer matt_the_hall 2 2,371 Mar-15-2021, 08:44 PM
Last Post: matt_the_hall
  Sort a dict in dict cherry_cherry 4 75,806 Apr-08-2020, 12:25 PM
Last Post: perfringo
  how to seperate words from string zarize 2 2,080 Aug-29-2019, 08:23 AM
Last Post: zarize
  SQL connection in seperate file? Tommy 1 2,075 Jul-12-2019, 12:42 PM
Last Post: ichabod801
  Seperate entities in text file bigdazza 1 2,840 Jun-02-2018, 03:15 PM
Last Post: snippsat
  how to start a seperate thread for every item in a list lravikumarvsp 1 2,920 May-31-2018, 08:07 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

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