-1

Hi there i have a list:

lst = ['Bedroom,24', 'Bedroom,26', 'Kitchen,22', 'Kitchen,23', 'Living Room,23', 'Living Room,24', 'Living Room,25']

Now i can't think of any way that i can change this to dictionary:

lst = {'temp':24,.....}

I tried replacing the comma but it didn't work out since its a list. I used for loops but didnt seem to work, plus i am a begginer and i didnt know how to change the number into integers.The example below makes the list:

fname = open("temps1.txt")     
lst = []    

for line in fname:
  line = line.rstrip()      
  words = line.splitlines() 

    for word in words:
    if word in lst: continue
    lst = lst + words     
  lst.sort() 
print(lst)

>>>>>>['temp,24', 'bedroom,26', 'class,23'] #output

At the end, Is it possible to covert the list into the dictionary?
My output should be:

 Average temp:
 bedroom: #value
 living room:#value

and so on

Dev Patel
  • 35
  • 1
  • 8

2 Answers2

0

You should loop through the list and then you should split it between using comma. You can do something like this.

lst = ['temp,24','bedroom,26','class,23']

dict_obj = dict()
for element in lst:
    dict_obj[element.split(',')[0]] = int(element.split(',')[1])

print(dict_obj)

Update Since the OP's requirement is to find average of the data, the approach would change

lst = ['Bedroom,24', 'Bedroom,26', 'Kitchen,22', 'Kitchen,23', 'Living Room,23', 'Living Room,24', 'Living Room,25']

dict_obj = dict()
for element in lst:
    if element.split(',')[0] in dict_obj:
        dict_obj[element.split(',')[0]]['temperature_reading'].append(int(element.split(',')[1]))
        dict_obj[element.split(',')[0]]['average_temperature'] = sum(dict_obj[element.split(',')[0]]['temperature_reading']) / len(dict_obj[element.split(',')[0]]['temperature_reading'])
    else:
        dict_obj[element.split(',')[0]] = {'temperature_reading' : [int(element.split(',')[1])],
                                           'average_temperature' : element.split(',')[1]}

print(dict_obj)

Output:

{
    'Bedroom': {'average_temperature': 25.0, 'temperature_reading': [24, 26]},
    'Kitchen': {'average_temperature': 22.5, 'temperature_reading': [22, 23]},
    'Living Room': {'average_temperature': 24.0,
                 'temperature_reading': [23, 24, 25]}
}
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • how do i convert that into an integer? – Dev Patel Aug 26 '18 at 06:56
  • @agro what if my list = ['Bedroom,24', 'Bedroom,26', 'Kitchen,22', 'Kitchen,23', 'Living Room,23', 'Living Room,24', 'Living Room,25'] – Dev Patel Aug 26 '18 at 07:05
  • @DevPatel do you want to save it as an list? i.e `{'Bedroom':[24,26] , 'Kitchen': [22,23]}` – Arghya Saha Aug 26 '18 at 07:07
  • @agro so basically, for example i have different temp for living room which are recorded 6 hours day and i have to calculate average of them. – Dev Patel Aug 26 '18 at 07:10
  • @DevPatel Check my updated answer – Arghya Saha Aug 26 '18 at 07:24
  • @agro would you be able to format it in my output style, i have updated it in my answer. – Dev Patel Aug 26 '18 at 07:31
  • @DevPatel SO is a platform for asking help, clearing doubts and getting guidance. I think I have cleared your doubt and guided you on the right direction. Now you should be able to solve it yourself from here on. Let me know if you are stuck somewhere. But otherwise we can't code for you. – Arghya Saha Aug 26 '18 at 07:34
  • what if i dont want the temp to be in dicionary and print them normally, line by line, I am sorry but your codes are written in one line and it gets really complicated if i move a single thing. – Dev Patel Aug 26 '18 at 07:44
  • @ argo your solution creates a problem, when the temp for kitchen is suppossed to be 23.33* but the progrom shows 23.5 and also you temp reading for kitchen only shows two reading instead of three readings. My point is that your program is only counting the average of unique temp reading not the same temp avg. – Dev Patel Aug 26 '18 at 08:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178783/discussion-between-argo-and-dev-patel). – Arghya Saha Aug 26 '18 at 08:17
0

The easiest:

print(dict([(i.split(',')[0],int(i.split(',')[1])) for i in lst]))

Output:

{'temp': 24, 'bedroom': 26, 'class': 23}
U13-Forward
  • 69,221
  • 14
  • 89
  • 114