Split word in list and iterate dictionary - python

I've got some code where I receive a string of languages in a text.
My goal is to turn this input into a list and iterate through this list in a dictionary to use as a key for value outputs. I send this output to a list to use later.
The output I am expecting is [57, 20, 22, 52, 60... etc] but currently, I am receiving
[57, None, None, None, None, None, None....etc]
My first output is correct but after that, It doesn't seem to find the correct value in the dict.
Code below.
l_languages = []
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"
language_list = data.split(',')
for language in language_list:
id = language_dict.get(language)
l_languages.append(id)
print(l_languages)
current output = [57, None, None, None, None, None, None....etc]

you are neglecting the white space in your language list. You should remove the leading and trailing white space and the access your dict.
if you just split the list at ',' then there is a leading white space in front of every following language. Just not on the first one, which explains your current output

Look at your language_list. It has leading whitespace. You need to call strip() on each element and you get your expected result
l_languages = []
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"
language_list = data.split(',')
print(language_list)
for language in language_list:
val = language_dict.get(language.strip())
l_languages.append(val)
print(l_languages)
['Afrikaans', ' Arabic', ' Assistive communication', ' AUSLAN', ' Bosnian', ' Burmese', ' Cantonese', ' Croation', ' Dutch'] # list with leading spaces
[57, 20, 21, 22, 52, 60, 23, 54, 50] # right result

l_languages = []
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"
language_list=[x.strip() for x in data.split(',')]
for language in language_list:
id = language_dict.get(language)
l_languages.append(id)
#output
[57, 20, 21, 22, 52, 60, 23, 54, 50]

Simplest way you can do
#Devil
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21,
'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23,
'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,
'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28,
'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"
data_list = data.split(",") #split the data
data_list = [d.strip() for d in data_list] #remove white space
l_languages = [language_dict[z] for z in data_list] #find the value using key
print(data_list)
print(l_languages)

Related

How do I convert these multiple lists into a big dictionary using python

subjects = ['Chem', 'Phy', 'Math']
students = ['Joy', 'Agatha', 'Mary', 'Frank', 'Godwin', 'Chizulum', 'Enoc', 'Chinedu', 'Kenneth', 'Lukas']
math = [76,56,78,98,88,75,59,80,45,30]
phy = [72,86,70,98,89,79,69,50,85,80]
chem = [75,66,77,45,83,75,59,40,65,90]
How do I transform the lists above to the nested dictionary below using pyhon
{
'math':{'joy':76, 'Agatha':56, 'Mary':78.....},
'phy':{'joy':72, 'Agatha':86, 'Mary':70....},
'chem':{'joy':75, 'Agatha':66, 'Mary':77....}
}
This is certainly not the most elegant way to do this, but it works:
dictionary = {}
dict_math = {}
dict_phy = {}
dict_chem = {}
for i in range(len(students)):
dict_math[students[i]] = math[i]
dict_phy[students[i]] = phy[i]
dict_chem[students[i]] = chem[i]
dictionary['math'] = dict_math
dictionary['phy'] = dict_phy
dictionary['chem'] = dict_chem
print(dictionary)
With the given lists, you could build the result dictionary this way :
result_dict = {
subject: {
name: grade for name in students for grade in globals()[subject.lower()]
}
for subject in subjects
}
This solution uses a nested dictionary comprehension and isn't meant for beginners. On top of that the use of built-in globals() is not recommanded and only suits in this particular case.
You can do something like that:
math_grades = list(zip(students, math))
phy_grades = list(zip(students, phy))
chem_grades = list(zip(students, chem))
your_dict = {
"math": {c: d for c, d in math_grades},
"phy": {c: d for c, d in phy_grades},
"chem": {c: d for c, d in chem_grades},
}
You can do it like this:
subjects = ['Chem', 'Phy', 'Math']
students = ['Joy', 'Agatha', 'Mary', 'Frank', 'Godwin', 'Chizulum', 'Enoc', 'Chinedu', 'Kenneth', 'Lukas']
math = [76, 56, 78, 98, 88, 75, 59, 80, 45, 30]
phy = [72, 86, 70, 98, 89, 79, 69, 50, 85, 80]
chem = [75, 66, 77, 45, 83, 75, 59, 40, 65, 90]
grades = {
"math": dict(zip(students, math)),
"phy": dict(zip(students, phy)),
"chem": dict(zip(students, chem)),
}

How to change the format of json to spacy/custom json format in python?

I do have a json format which is generated from docanno annotation tool. I want to convert the json into another format. Please check below for the format
Docanno json format :
{"id": 2, "data": "My name is Nithin Reddy and i'm working as a Data Scientist.", "label": [[3, 8, "Misc"], [11, 23, "Person"], [32, 39, "Activity"], [45, 59, "Designation"]]}
{"id": 3, "data": "I live in Hyderabad.", "label": [[2, 6, "Misc"], [10, 19, "Location"]]}
{"id": 4, "data": "I'm pusring my master's from Bits Pilani.", "label": [[15, 24, "Education"], [29, 40, "Organization"]]}
Required json format :
("My name is Nithin Reddy and i'm working as a Data Scientist.", {"entities": [(3, 8, "Misc"), (11, 23, "Person"), (32, 39, "Activity"), (45, 59, "Designation")]}),
("I live in Hyderabad.", {"entities": [(2, 6, "Misc"), (10, 19, "Location")]}),
("I'm pusring my master's from Bits Pilani.", {"entities": [(15, 24, "Education"), (29, 40, "Organization")]})
I tried the below code, but it's not working
import json
with open('data.json') as f:
data = json.load(f)
new_data = []
for i in data:
new_data.append((i['data'], {"entities": i['label']}))
with open('data_new.json', 'w') as f:
json.dump(new_data, f)
Can anyone help me with the python code which will change the json to required format?

How to create multiple dictionaries from a single dictionary based on the almost same key values?

So, I am a newbie to Python. I need small help with programming in python.
I have a dictionary as shown
dict = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 ,
'data2' : 45, 'cache2' : 67, 'option2' : 33,
'data3': 56, 'cache3': 47, 'option3' : 25}
I have to create a 3 dictionaries as shown below:
dict1 = {'data1':50,'data2' : 45,'data3': 56}
dict2 = {'cache1' : 30,'cache2' : 67,'cache3':47}
dict3 = {'option1' : 90 ,'option2' : 33,'option3' :25}
Can anyone please help me with python to get this output.
You could use a dict comprehension to create each of the desired dictionaries. I demonstrated data below, but you could use 'cache' and 'option' similarly.
>>> source = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 ,
'data2' : 45, 'cache2' : 67, 'option2' : 33,
'data3': 56, 'cache3': 47, 'option3' : 25}
>>> {k: v for k, v in source.items() if 'data' in k}
{'data1': 50, 'data2': 45, 'data3': 56}
There are multiple ways you could do this. You can try:
dict1 = {key:value for key,value in dict.items() if 'data' in key}
dict2 = {key:value for key,value in dict.items() if 'cache' in key}
dict3 = {key:value for key,value in dict.items() if 'option' in key}
You could also do it in one go:
dict1, dict2, dict3 = {}, {}, {}
for key, value in dict.items():
if 'data' in key:
dict1[key] = value
elif 'cache' in key:
dict2[key] = value
elif 'option' in key:
dict3[key] = value
You could try this:
def Collect(column,dictionary):
result = {}
for key in dictionary:
if column in key:
result[key] = dictionary[key]
return result
dict_ = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 ,
'data2' : 45, 'cache2' : 67, 'option2' : 33,
'data3': 56, 'cache3': 47, 'option3' : 25}
dataDict = Collect("data",dict_)
cacheDict = Collect("cache",dict_)
optionDict = Collect("option",dict_)
print(dataDict)
print(cacheDict)
print(optionDict)
This will give you a result like the following:
dict1 = {}
dict2 = {}
dict3 = {}
for k, v in dict.items():
if k.startswith("data"):
dict1[k] = v
elif k.startswith("cache"):
dict2[k] = v
else:
dict3[k] = v
A general solution for any number of cases:
mydict = {'data1' : 50 , 'cache1' : 30, 'option1' : 90 ,
'data2' : 45, 'cache2' : 67, 'option2' : 33,
'data3': 56, 'cache3': 47, 'option3' : 25}
nameset = set()
for d in mydict.keys():
nameset.add(''.join(a for a in d if a.isalpha()))
print(nameset)
all_dicts = {}
for n in nameset:
all_dicts[n] = {}
for d in mydict.keys():
for n in nameset:
if n in d:
all_dicts[n][d] = mydict[d]
print(all_dicts)
Out:
{'option', 'data', 'cache'}
{'option': {'option1': 90, 'option3': 25, 'option2': 33}, 'data': {'data2'
: 45, 'data1': 50, 'data3': 56}, 'cache': {'cache2': 67, 'cache3': 47, 'ca
che1': 30}}

How to extract numbers from JSON API

I want to extract numbers and calculate the sum of these numbers from JSON API. The format is
{
comments: [
{
name: "Matthias"
count: 97
},
{
name: "Geomer"
count: 97
}
...
]
}
And my code is
import json
import urllib
url = 'http://python-data.dr-chuck.net/comments_204529.json'
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
result = json.loads(url)
print result
I can get the result of how many characters in this data but cannot continue with the code because it's said JSON object cannot be decoded.
Does anyone know how to finish this code? Much appreciated!
First of all, I suggest you study the built-in Python Data Structures to get a better understanding about what you are dealing with.
result is a dictionary, result["comments"] is a list of dictionaries - you can make a list comprehension to get all the comments counts:
>>> import json
>>> import urllib
>>>
>>> url = 'http://python-data.dr-chuck.net/comments_204529.json'
>>> uh = urllib.urlopen(url)
>>> result = json.load(uh)
>>>
>>> [comment["count"] for comment in result["comments"]]
[100, 96, 95, 93, 85, 85, 77, 73, 73, 70, 65, 65, 65, 62, 62, 62, 61, 57, 50, 49, 46, 46, 43, 42, 39, 38, 37, 36, 34, 33, 31, 28, 28, 26, 26, 25, 22, 20, 20, 18, 17, 15, 14, 12, 10, 9, 8, 6, 5, 3]

How to stream bot sensors with pyserial?

I am trying to stream an iRobot Create's sensors with pyserial. I import openinterface.py, setup the bot variable with the CreateBot function, and then call
bot.stream_sensors(6)
Then I receive this error - "Streaming thread error! tuple index out of range" The only reason I am calling the function with 6 is because thats what the example I am looking at used. I have also tried stream_sensors(0), stream_sensors(1), all the way up to 6. With any number less than 6, I get the same error plus "Illegal sensor id!". What is the parameter based on? Is it the specific sensor I want to stream (and if so, how do I get the number)? Any help would be appreciated.
Looking through the openinterface.py source, it looks like your getting the "Illegal sensor id" error because the given ID value you use when you call stream_sensors() doesn't match against a dictionary with known sensor ID's. The sensor ID dictionary is specified in the class SensorPacketDecoderAPI:
class SensorPacketDecoderApi:
"""
Transform sensor data in the form of bytes (from a serial stream)
into a dictionary of sensor values.
"""
names = {'left-velocity' : 42,
'right-velocity' : 41,
'radius' : 40,
'velocity' : 39,
'n-stream-packets' : 38,
'song-playing' : 37,
'song-number' : 36,
'oi-mode' : 35,
'home-base?' : 34,
'internal-charger?' : 34,
'user-analog-in-0' : 33,
'baud-rate-change?' : 32,
'user-digital-in-3' : 32,
'user-digital-in-2' : 32,
'user-digital-in-1' : 32,
'user-digital-in-0' : 32,
'cliff-right-signal' : 31,
'cliff-right-front-signal' : 30,
'cliff-left-front-signal' : 29,
'cliff-left-signal' : 28,
'wall-signal' : 27,
'capacity' : 26,
'charge' : 25,
'temperature' : 24,
'current' : 23,
'voltage' : 22,
'charging-state' : 21,
'angle' : 20,
'distance' : 19,
'advance?' : 18,
'play?' : 18,
'infrared-byte' : 17,
'left-wheel-overcurrent?' : 14,
'right-wheel-overcurrent?' : 14,
'low-side-driver-2-overcurent?' : 14,
'low-side-driver-0-overcurent?' : 14,
'low-side-driver-1-overcurent?' : 14,
'virtual-wall?' : 13,
'cliff-right?' : 12,
'cliff-front-right?' : 11,
'cliff-front-left?' : 10,
'cliff-left?' : 9,
'wall?' : 8,
'wheel-drop-caster?' : 7,
'wheel-drop-left?' : 7,
'wheel-drop-right?' : 7,
'bump-left?' : 7,
'bump-right?' : 7,
'all' : 6}
As to the reason why you're getting the "Streaming thread error!...", I'm not sure, all I can tell from my glance through the code is that it's originating in a function called _stream_sensors_worker inside the CreateBot class. There's also a function called _test_sensor_streaming that you could also try to get some debug info from _stream_sensors_worker.

Categories

Resources