This is a little extension to my previous problem. I have a variable known as region with values such as:
/health/blood pressure
/health/diabetes
/cmd/diagnosis
/fitness
/health
/health/type1 diabetes
Now I want to add one more variable known as region_map alongside with region variable which maps each region to a unique name such as Region1, Region2, so on as they appear sequentially in the column. So the output would look like:
/health/blood pressure Region1
/health/diabetes Region2
/cmd/diagnosis Region3
/fitness Region4
/health Region5
/health/type1 diabetes Region6
Also all the region values are unique.
Basically its a web related data, but not now its a stand alone task. In python shell I have imported the data which just consists of unique list of regions. Now first task was to remove the integer values, which I did successfully with the help of other members. Now I have around 2000+ entries for regions. Sample data as i mentioned above, you can see it. For subsequent analysis like merging it with other files, I want to add mapping variable to the existing column, which is region. So for each unique 2000+ entries I want separate mapping variable such as region 1, region 2, region 3, region 2000+. I have created a dictionary with following code:
mapping={region:index for index, region in enumerate(data.region)}
But problem is how to loop through it and replacing existing values with region 1, region 2 and so on. Basically as i explained I want to add one more column with each entry mapping to different region. Hope this explains you well!!
You can use a dictionary so you will have keys as names and the regions as values.
You can use it like this:
dict[key] = value
This will create a new key and assign a value for it.
You can put the key like this "/health/blood pressure" and then the value like this "Region 1".
When you refer to it:
dict["/health/blood pressure"]
It will return "Region 1".
To loop through the dictionary, do the following:
for i in dictionary:
# condition to see if you need to change the current value
dictionary[i] = "New Value" # The I represents the key in the dictionary.
For more information look in the python manual, cause these are just basic information for using dictionaries, and if you are doing a web service personally I would prefer using a DB for this. But again I don't know the bigger picture so its hard to say.
I assume that you already have an array list of the regions and needs new array list with the mapping of REGION and REGION MAP.
region = ["/health/blood pressure", "/health/diabetes", "/cmd/diagnosis" ,"/fitness", "/health", "/health/type1 diabetes"]
region_mapped = {}
region_count = len(region)
for region_index in range(0, region_count):
region_mapped[region[region_index]] = "Region-%s" % region_index
print(region_mapped)
As its available with LIST - DICT can not append/extend the elements.
As you will need key:value pair kind of elements then above example is best suited. Still you can make it even optimised by creating def.
Related
I am trying to calculate a distance between two locations, using their coordinates. However I don't know how I can access the coordinate values, since they are in a dictionary.
I am very new to coding, and didn't understand any of the code I found regarding this problem, since it's too advanced for me. I don't really know where to start. My main function creates the dictionary: (Edit)
def main():
filename = input("Enter the filename:\n")
file= open(filename, 'r')
rows= file.readlines()
d = {}
list = []
for x in rows:
list.append(x)
#print(list)
for elem in list:
row = elem.split(";")
d[row[3]] = {row[0], row[1]} #these are the indexes that the name and latitude & longitude have in the file
{'Location1': {'40.155444793742276', '28.950292890004903'}, 'Location2': ... }
The dictionary is like this, so the key is the name and then the coordinates are the values. Here is the function, which contains barely anything so far:
def calculate_distance(dictionary, location1, location2):
distance_x = dictionary[location1] - dictionary[location2]
# Here I don't know how I can get the values from the dictionary,
# since there are two values, longitude and latitude...
distance_y = ...
distance = ... # Here I will use the pythagorean theorem
return distance
Basically I just need to know how to work with the dictionary, since I don't know how I can get the values out so I can use them to calculate the distance.
--> How to search a key from a dictionary and get the values to my use. Thank you for answering my stupid question. :)
Well you are starting out, its normal that this makes it more difficult for you.
So lets see, you have a function that outputs a dictionary where the keys are locations and the values are coordinate pairs.
First lets talk about the data types that you use.
location_map={'Location1': {'40.155444793742276', '28.950292890004903'}, 'Location2': ... }
I think there is an issue with your values, it seems that they are sets of strings. This has 2 main advantages for your goal.
First, set objects do not support indexing, this means that you cannot access location_map['Location1'][0] to get the first coordinate. Trying this would give you a TypeError. Instead, by using tuples when creating your map would allow you to index. You can do this by defining the coordinates as tuple([longitude,latitude]) instead of {longitude,latitude}.
Second, it seems that your coordinates are strings, in order to perform arithmetic operations with your data you need a numeric type such as integers or in your case floats. If you are reading longitude and latitude values as strings you can convert them by using float(longitude) and float(latitude).
There are multiple ways to do it, few are listed below:
# option 1
for i, v in data.items(): # to get key and value from dict.
for k in v: # get each element of value (its a set)
print (k)
# option 2
for i, v in data.items(): # to get key and value from dict.
value_data = [k for k in list(v)] # convert set to list and put it in a list
print (i, value_data[0], value_data[1]) # use values from here
I would suggest you to go through the python documentations to get more in-depth knowledge.
I have a dataset given as follows:
<t>Pokemon Evolves to<\t>
<t>Pichu Pickachu<\t>
<t>Pickachu Raichu<\t>
<t>Bulbasaur Venusaur<\t>
I want the output as a list of lists giving me an inner pokemon and all the pokemon it evolves to. The output should be something like this:
[[Pichu, Pickachu, Raichu], [Bulbasaur, Venusaur]]
How do I go about it? Do I need to create a tree type structure? Or is there any other way?
This is for Python 3. I've tried looking for similar problems but couldn't find anything conclusive.
You can start by making a dictionary containing the possible transformations a Pokemon can make. So, as a key we have the Pokemon on the left-hand side of your dataset, and as its value we have the Pokemon it can evolve to, as a tuple:
evolves = {"Pichu": ("Pickachu",), "Pickachu": ("Raichu",), "Bulbasaur": ("Venusaur",)}
Let X(A) be the Pokemons that Pokemon A can evolve to.
Now, you can:
While there were changes
For every Pokemon A
Consider every Pokemon B in X(A)
Take the set X(B) consisting of the Pokemons B can evolve to
If the set X(A) + X(B) != X(A), add X(B) to X(A) and remove X(B)
Writing kmeans in python from scratch without any outside packages like numpy and scipy and ran into this issue when I am trying to assign data points to clusters.
Essentially for each data point, I find which cluster is closest to that point and then update the dictionary of clusters by adding the data point to the list of points that belong to that cluster (ie the value of the dictionary). My issue is that when I try to update on of the keys in the dictionary it turns all the other dictionary values to None, which is incorrect.
Tried separating out the steps of the process and looking at it line by line, but when I try to update one value all other values turn into None.
clusters = dict.fromkeys(k_init, [].copy())
for elem in data:
minC = (101010101, 9999999)
for cent in k_init:
#print(elem, cent)
if eucliean(elem, cent) < minC[1]:
minC = (cent, eucliean(elem, cent))
key = minC[0]
old = clusters.get(key)
clusters[key] = old.append(elem)
The problem is on the line
clusters = dict.fromkeys(k_init, [].copy())
When you create a dictionary like the above, then each key is assigned the reference of the same list. Hence whenever you add to the list of any keys, it is the same reference for all the other keys, so you see that it is appended to all keys. To avoid this issue do:
clusters = { key : list([]) for key in keys }
Usually in Python if we put lst[-1] we mean the item at the length of lst. But I would like to make it where it means "before index 0". Is such a thing possible? I've looked around but I haven't found anything useful yet.
Currently I'm working on a robot that maps out a room. The indices will be used for coordinates in the room so if my robot starts at (0, 0) in a 2d list then I need to be able to traverse the X & Y axes in both the positive and negative directions. that is why I need this I need to be able to use 0 as not the beginning of the list but rather is just a part of the list.
It sounds like your program won't know how big the room is when it's run, since it's using a robot to map it. In which case let's stop trying to use list indexes and use something better-suited to the task of handling arbitrary keys.
from collections import defaultdict
cur_map = defaultdict(dict)
This creates a dict object that, when you assign an unknown key to it, creates a new dict as a value. In other words:
cur_map[0]['foo'] = 'bar'
# cur_map now looks like
# {0: {'foo': 'bar'}}
Dictionaries can take arbitrary hashable objects as keys, so you can do
cur_map[0][0] = "START"
cur_map[0][-1] = "South x1"
cur_map[-1][-1] = "South x1, West x1"
# etc
Yes, you can. If you for example want a 2D list with x-indexes -50 to 50 and y-indexes -50 to 50, then simply make a 101x101 list and interpret negative indexes as being "before". Up to you how you think of them.
I've used this before, for example in a maze puzzle, to avoid boundary-checking in my main code. I simply added two rows and two columns in addition to the actual field and put "walls" in the cells of the two last rows and columns. Index -1 then simply represented the row above the one of index 0 and the column left of the one with index 0.
User sets values manually in all cells in a QTableWidget. The values of the first row of the table represent the values we want to store in a dictionary FirstRowDict. Second row values will be put in SecondRowDict and so on.
So from a table like one shown in the picture,
we want to end up in this example with FirstRowDict={0:10,1:20}
and SecondRowDict={0:30,1:40}
To achieve this i created a button and added an action that updates the target dictionaries when clicked:
def button_click():
for j in range(0,2):
FirstRowDict.update({i: float(str(tablewidget.itemAt(0,j).text()))})
#I put "0" in itemAt because first row is zero
#tablewidget = QTableWidget() declared earlier in the source code
SecondRowDict.update({i: float(str(tablewidget.itemAt(1,j).text()))})
print '1st_Row_Dict=%s'%1st_Row_Dict,'\n2nd_Row_Dict=%s'%2nd_Row_Dict
The output is not correct since the dictionaries are populated with same value and not the unique value of each cell.
I get FirstRowDict={0:10,1:10} and SecondRowDict={0:10,1:10}
It seems that i have to create a new item using QTableWidgetItem http://doc.qt.nokia.com/4.7-snapshot/qtablewidget.html#item
Python begginer, some advice would be appreciated.
Ah, I see the confusion. itemAt returns the item at the given position, but it's not row/column. It's the coordinate (think of it as pixel).
Yes, you should use item instead of itemAt.
You don't need a str if you're going to convert it to float. float() will work on a QString as if it was a str. And, instead of .update, you can just assign the new value to the key.
FirstRowDict[i] = float(tablewidget.item(0, j).text())