I'm trying to determine all unique elements within a list based on the x-y-coordinates. The list looks like the following structure:
List =[[[Picture1, [X-Coordinate, Y-Coordinate]], [Picture1, [X-Coordinate, Y-Coordinate]]],
[[Picture2, [X-Coordinate, Y-Coordinate]], [Picture2, [X-Coordinate, Y-Coordinate]]],
....]
This is the actuall list:
MyList = [[['IMG_6094.jpg', [2773.0, 240.0]], ['IMG_6094.jpg', [2773.0, 240.0]]],
[['IMG_6096.jpg', [1464.0, 444.0]], ['IMG_6096.jpg', [3043.0, 2358.0]]],
[['IMG_6088.jpg', [1115.5, 371.5]]],
[['IMG_6090.jpg', [3083.0, 1982.5]], ['IMG_6090.jpg', [3083.0, 1982.5]]],
[['IMG_6093.jpg', [477.0, 481.0]], ['IMG_6093.jpg', [450.0, 487.5]]]]
As you can see, there are sometimes elements that have the same coordinates within a picture or are at least very close to each other. What I need to do is to throw out all non-unique or very close elements based on one of the coordinates (doesn't matter if its x or y).
The list should look like this:
MyList = [[['IMG_6094.jpg', [2773.0, 240.0]], --- thrown out because copy of first element ---],
[['IMG_6096.jpg', [1464.0, 444.0]], ['IMG_6096.jpg', [3043.0, 2358.0]]],
[['IMG_6088.jpg', [1115.5, 371.5]]],
[['IMG_6090.jpg', [3083.0, 1982.5]], --- thrown out because copy of first element---],
[['IMG_6093.jpg', [477.0, 481.0]], --- thrown out because e.g. abs(x-coordinates) < 30]
Could someone provide a elegant solution?
Thanks in advance!
you create for img a list of innteger that are close to it, than for the other points on that img, you check if their x or y or in the list, and if so add them to the remove list
btw when removing using the index make sure to start with largest x,y so you don't change the lower point index before removing them
remove = []
close = 30
for i in range(len(MyList)):
xList = []
yList = []
for j in range(len(MyList[i])):
if int(MyList[i][j][1][0]) in xList or int(MyList[i][j][1][1]) in yList:
remove.append([i,j])
continue
x = int(MyList[i][j][1][0])
y = int(MyList[i][j][1][1])
xList+= list(range(x-close,x+close))
yList+= list(range(y-close,y+close))
Related
the input to my function is a list of lists like the one below, and I want to divide each item in the list after the 3rd one by the third one for each list.. How do I do that?
So far I tried this:
def convert_data_percentages(data):
lst = []
for x in data:
lst.append(int(x[2:]) // int(x[2]))
return lst
convert_data_percentages(data, col_id=2)
[["'s-Gravenhage",
'GM0518',
'537833',
'266778',
'271055',
'92532',
'66099',
'162025',
'139157',
'78020',
'304766',
'162020',
'51430',
'19617'],
['Rotterdam',
'GM0599',
'644618',
'317935',
'326683',
'103680',
'86037',
'197424',
'159008',
'98469',
'367279',
'187835',
'62703',
'26801']]
Do you mean something like this?
data = [["'s-Gravenhage",
'GM0518',
'537833',
'266778',
'271055',
'92532',
'66099',
'162025',
'139157',
'78020',
'304766',
'162020',
'51430',
'19617'],
['Rotterdam',
'GM0599',
'644618',
'317935',
'326683',
'103680',
'86037',
'197424',
'159008',
'98469',
'367279',
'187835',
'62703',
'26801']]
# reformat original data, dropping first two items and converting to ints
# then extract third item and remaining subset into tuple
subsets = [(lst[:2] + to_int[:1], to_int[0], to_int[1:]) for lst in data if (to_int := [int(i) for i in lst[2:]])]
# walk through tuples generating a new list by dividing all items in the
# remaining subset by the third item
print([first_three + [i / third for i in subset] for first_three, third, subset in subsets])
returns:
[["'s-Gravenhage", 'GM0518', 537833, 0.4960238587070708, 0.5039761412929292, 0.1720459696597271, 0.1228987436620661, 0.30125522234596985, 0.2587364479308633, 0.14506361640137366, 0.5666554488103185, 0.3012459257799354, 0.09562447823023132, 0.03647414717951483],
['Rotterdam', 'GM0599', 644618, 0.49321458600287305, 0.506785413997127, 0.16083944289486177, 0.13346974487215685, 0.3062651058456326, 0.24667012090881732, 0.15275558547853146, 0.5697622467880202, 0.291389629206755, 0.09727156238268246, 0.04157656162254234]]
I need to create a function that reads the data given and creates a list that contains tuples each of which has as its first element the name of the airport and as its second and third its geographical coordinates as float numbers.
airport_data = """
Alexandroupoli 40.855869°N 25.956264°E
Athens 37.936389°N 23.947222°E
Chania 35.531667°N 24.149722°E
Chios 38.343056°N 26.140556°E
Corfu 39.601944°N 19.911667°E
Heraklion 35.339722°N 25.180278°E"""
airports = []
import re
airport_data1 = re.sub("[°N#°E]","",airport_data)
def process_airports(string):
airports_temp = list(string.split())
airports = [tuple(airports_temp[x:x+3]) for x in range(0, len(airports_temp), 3)]
return airports
print(process_airports(airport_data1))
This is my code so far but I'm new to Python, so I'm struggling to debug my code.
If you want the second and third element of the tuple to be a float, you have to convert them using the float() function.
One way to do this is creating a tuple with round brackets in your list comprehension and convert the values there:
def process_airports(string):
airports_temp = string.split()
airports = [(airports_temp[x], float(airports_temp[x+1]), float(airports_temp[x+2])) for x in range(0, len(airports_temp), 3)]
return airports
This yields a pretty unwieldy expression, so maybe this problem could be solved more readable with a classical for loop.
Also note that slpit() already returns a list.
Further remark: If you just cut off the letters from coordinates this might come back to bite you when your airports are in different quadrants.
You need to take in account N/S, W/E for longitude and latitude.
May be
def process_airports(string):
airports = []
for line in string.split('\n'):
if not line: continue
name, lon, lat = line.split()
airports.append((name,
float(lon[:-2]) * (1 if lon[-1] == "N" else -1),
float(lat[:-2]) * (-1 if lat[-1] == "E" else 1)
))
return airports
>>> process_airports(airport_data1)
[('Alexandroupoli', 40.855869, -25.956264), ('Athens', 37.936389, -23.947222), ('Chania', 35.531667, -24.149722), ('Chios', 38.343056, -26.140556), ('Corfu', 39.601944, -19.911667), ('Heraklion', 35.339722, -25.180278)]
I prefered the double split to put in evidence the differences lines/tuple elements
I'm trying to calculate the alcohol by volume (abv) of some beer by using variables from 2 separate lists (which I took from a dictionary entry). I'm having trouble getting the values from both lists to be applied to the equation that I have for abv (and it's probably not possible to have a for loop with an and statement like the one I have below). Is it possible to get variables from two separate lists to be subbed into the same equation in one for loop?
Right now it's telling me that I have a type error where 'bool' object is not iterable. Here's what I've tried so far in terms of coding:
beers = {"SG": [1.050, 1.031, 1.077, 1.032, 1.042, 1.055, 1.019, 1.089, 1.100, 1.032],
"FG": [1.010, 1.001, 1.044, 1.003, 1.003, 1.013, 1.002, 1.020, 1.056, 1.000],
"grad student 1": [5.264, 3.983, 4.101, 7.216, 2.313, 4.876, 2.255, 8.991, 5.537, 4.251],
"grad student 2": [5.211, 3.008, 4.117, 3.843, 5.168, 5.511, 3.110, 8.903, 5.538, 4.255]}
#separating the SG and FG values from the dictionary entry
SG_val = beers["SG"]
FG_val = beers['FG']
def find_abv(SG = SG_val, FG = FG_val):
abv_list = []
i = 0.0
j = 0.0
for i in SG_val and j in FG_val:
abv = (((1.05/0.79)*((i - j)/j))*100)
abv_list.append(abv)
return abv_list
find_abv()
print(abv_list)```
You cannot use and to iterate two variables in a single for loop. You can use the zip function to do that:
def find_abv(SG = SG_val, FG = FG_val):
abv_list = []
i = 0.0
j = 0.0
for i, j in zip(SG,FG):
abv = (((1.05/0.79)*((i - j)/j))*100)
abv_list.append(abv)
return abv_list
abv_list = find_abv()
print(abv_list)
You also need to assign the result of find_abv() to a variable in order to print it, which you don't, as it seems in your code.
Another thing is that the use of SG_val and FG_val in the loop of your find_abv is pointless, since you have the SG an FG parameters in your function.
You can't use a for loop to directly iterate through multiple lists. Currently, your function is trying to iterate through (SG_val and j in FG_val), which itself is a boolean and can therefore not be iterated through.
If the two lists will always have the same number of items, then you could simply iterate through the indexes:
# len(SG_val) returns the length of SG_val
for i in range(len(SG_val)):
abv = (((1.05/0.79)*((SG_val[i] - FG_val[i])/FG_val[i]))*100)
abv_list.append(abv)
# put the return outside of the for loop so that it can finish iterating before returning the value
return abv_list
If the lists aren't always going to be the same length then you can write for i in range(len(SG_val) if len(SG_val) <= len(FG_val) else len(SG_val)): instead of for i in range(len(SG_val)):so that it iterates until it reaches the end of the smallest list.
Also, to output the value returned by the function you have to assign it to something and then print it or just print it directly:
abv_list = find_abv()
print(abv_list)
# or
print(find_abv())
I have a nested_list that looks like
[
['"1"', '"Casey"', '176544.328149', '0.584286566204162', '0.415713433795838', '0.168573132408324'],
['"2"', '"Riley"', '154860.665173', '0.507639071226889', '0.492360928773111', '0.0152781424537786'],
['"3"', '"Jessie"', '136381.830656', '0.47783426831522', '0.52216573168478', '0.04433146336956'],
['"4"', '"Jackie"', '132928.78874', '0.421132601798505', '0.578867398201495', '0.15773479640299'],
['"5"', '"Avery"', '121797.419516', '0.335213073103216', '0.664786926896784', '0.329573853793568']
]
(My real nested_listis a very long list). And I tried to extract 2 data from each sublist and here is what I did
numerical_list = []
child_list = []
for l in nested_list:
child_list.append(l[1])
child_list.append(float(l[2]))
numerical_list.append(child_list)
print(numerical_list)
This gave me an list index out of range error on the line of child_list.append(l[1]). However, if I change that for l in nested_list: to for l in nested_list[:4]: or any range that is within the length of nested_list, it worked properly. This doesn't make any sense to me. Could someone help me out on finding where is wrong? Thank you~
If you are just interested in the first two elements, one way is to use try... except, other direct way is to check for the length of the list as following.
This way you only append the lists where the 1st and the 2nd element exist.
numerical_list = []
child_list = []
for l in nested_list:
if len(l>=3):
child_list.append(l[1])
child_list.append(float(l[2]))
numerical_list.append(child_list)
print(numerical_list)
I am trying to write a orientation routine for a 3-axis accelerometer. The part I am getting stuck on is, I have one dict with all my axis' listed, after taking the 'z-axis' reading, I want to remove that axis from the Availiable_axis list. Here is a portion of my code that demonstrates what I am trying to do.
import operator
Readings1 = { 0:{'x':0.1, 'y':-1, 'z':-0.1}, 1:{'x':.4, 'y':-.1, 'z':-0.1},
2:{'x':-0.4, 'y':-.8, 'z':-0.1}, 3:{'x':0.1, 'y':-.1, 'z':-0.6},
4:{'x':0.1, 'y':-.2, 'z':0.4}}
SetupValue = {'Axis':{'x-axis':'x','y-axis':'y','z-axis':'z'}}
Available_axis = [SetupValue['Axis']['x-axis'], SetupValue['Axis']['y-axis'], SetupValue['Axis']['z-axis']]
axes = Readings1[0]
print axes
for key in axes:
axes[key] = abs(axes[key])
print axes
print (max(axes.iteritems(), key = operator.itemgetter(1))[0])
Available_axis.pop(max(axes.iteritems(), key = operator.itemgetter(1))[0],0)
Any help would be appreciated.
Available_axis is a list. When popping from a list, you must specify the integer location.
You can also have a short list comprehension that removes the target variable.
Available_axis = [x for x in Available_axis
if x != max(axes.iteritems(), key = operator.itemgetter(1))[0]]