The following shows a sample of a list of lists that I have:
[[['point_5', [5, 6, 7, 8], 11.0],
['point_5', [6, 7, 8, 9],12.57]],
[['point_18', [3, 4, 5, 6],6.25],
['point_18', [3, 5, 6, 7],7.2],
['point_18', [4, 5, 6, 7],7.55],
['point_18', [6, 7, 8, 9],14.0],
['point_19', [3, 5, 6, 7],8.166],
['point_19', [5, 6, 7, 8],9.285],
['point_19', [6, 7, 8, 9],11.0]]]
I need to define a loop which searches through each element of this list of lists and returns the maximum value of the last element of each list. What I mean is that for example for [6,7,8,9] we have:
['point_5', [6, 7, 8, 9], 12.57]
['point_18', [6, 7, 8, 9],14.0]
['point_19', [6, 7, 8, 9],11.0]
Since max(12.57,14.0,11.0) = 14.0 then what I am looking for is a list that one of its element is [point_18,[6,7,8,9],14.0].
Another example is that since the only element that has [3, 4, 5, 6] is ['point_18', [3, 4, 5, 6],6.25] then another element of the new list should be [point_18,[3,4,5,6],6.25].
In fact, the new list of lists that I am trying to create should be like the following list:
New_list = [['point_5',[5,6,7,8],11.0],['point_18',[6,7,8,9],14.0],['point_18', [3, 4, 5, 6],6.25],['point_19', [3, 5, 6, 7],8.166],['point_18', [4, 5, 6, 7],7.55]].
I am not sure if it is a good idea or not but what I have done is that first I tried to extract each unique [x,y,i,j] in a list through the following code:
A = []
for i in bob:
for j in i:
A.append(j[1])
import itertools
A.sort()
B = list(A for A,_ in itertools.groupby(A))
Now B is:
[[3, 4, 5, 6],
[3, 5, 6, 7],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9]]
Then I want to search for each element of this list in the main lists of list and find the max value.
Any help would be appreciated.
I think you can try breaking the problems into parts.
I don't know if you had a list that already done the first step, so I am assuming no
This is the list that I am working with:
your_list = [['point_5', [5, 6, 7, 8], 11.0],
['point_5', [6, 7, 8, 9],12.57],
['point_18', [3, 4, 5, 6],6.25],
['point_18', [3, 5, 6, 7],7.2],
['point_18', [4, 5, 6, 7],7.55],
['point_18', [6, 7, 8, 9],14.0],
['point_19', [3, 5, 6, 7],8.166],
['point_19', [5, 6, 7, 8],9.285],
['point_19', [6, 7, 8, 9],11.0]]
First, try sorting the list in a dict.
sorted_dict = {}
for i in your_list:
if tuple(i[1]) in sorted_dict:
sorted_dict[tuple(i[1])].append(i)
else:
sorted_dict[tuple(i[1])] = [i]
Then, select the max and put it in a list.
return_list = []
for key, values in sorted_dict.items():
print(values)
return_list.append(sorted(values, key=lambda x: float(x[2]))[-1]) # sort the list according to the third value
Return list now should have the value you're looking for
I am not sure if this is what you're looking for so comment if there's any problem
Related
Lists are used to store multiple items in a single variable.
Lists are created using square brackets.
I want to reverse each item in the list of a list.
I tried reversed and [::1] method but it did not gave me desired output.
Here is my list
list1 = [1, 2, 3, [4, 5, 6], 6, 7, [8, 9, 10]]
I tried print(list1[::-1]) and reversed(list1) and got this output
output: [[8, 9, 10], 7, 6, [4, 5, 6], 3, 2, 1]
How could I get this output?
output: [[10, 9, 8], 7, 6, [6, 5, 4], 3, 2, 1]
You can use a recursive function.
def revall(l):
return [revall(x) if isinstance(x, list) else x for x in l[::-1]]
print(revall([1, 2, 3, [4, 5, 6], 6, 7, [8, 9, 10]]))
Try this one:
list1 = [1, 2, 3, [4, 5, 6], 6, 7, [8, 9, 10]]
list1.reverse()
for sub_list in list1:
if type(sub_list) is list:
sub_list.reverse()
print(list1)
I have an array with elements [2, 3, 4, 5, 6, 7, 8, 9, 10, ...]. I wish to split this up as follows: [[2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], ...]. I am not sure how to do this because the elements in each split must be repeated, and I am unsure of how to create these repeated elements. Any help will be much appreciated.
You can use zip() to create 3-tuples, and then use list() to transform the resulting tuples into lists.
data = [2, 3, 4, 5, 6, 7, 8, 9, 10]
# Prints [[2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]
print(list(list(item) for item in zip(data, data[1:], data[2:])))
Use a comprehension:
N = 3
l = [2, 3, 4, 5, 6, 7, 8, 9, 10]
ll = [l[i:i+N] for i in range(len(l)-N+1)]
Output:
>>> ll
[[2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]
There's a module "more_itertools" that has method that creates triples from a list:
import more_itertools
out = list(more_itertools.triplewise(lst))
Output:
[(2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10)]
It's not a built-in module, so you'll have to install it (pip or conda or whatever you use) beforehand however.
The zip idea generalised for varying chunk size:
lst = [2, 3, 4, 5, 6, 7, 8, 9, 10]
result = list(zip(*(lst[i:] for i in range(3))))
zip() and comprehension, suggested above, are probably the ways to go in "real life", but in order to better understand the problem, consider this very simple approach:
data = [2, 3, 4, 5, 6, 7, 8, 9, 10]
result = []
for i in range(0,len(data)-2):
result.append([data[i],data[i+1],data[i+2]])
print(result)
I have a list like this
list = [[1, 2, 3, 4], [1, 9, 12], [9], [8], [7, 8, 9, 10, 12, 16], [7, 8, 9, 10], [4, 5, 6, 7], [6, 7, 8, 9, 10, 11]]
I need to find the k-th largest sublist by length of the list from the above-nested list, a small twist is there:
If K =2 the answer should be [4,5,6,7] as it comes later in the
processing
If K = 1 the answer should be [6, 7, 8, 9, 10, 11] as it comes later
in the processing
I initially sorted the nested sublist by length, which I think will be useful to find kth largest sublist, as it also preserves the order for list in which they were processed earlier
sorted_list = [[9], [8], [1, 9, 12], [1, 2, 3, 4], [7, 8, 9, 10], [4, 5, 6, 7], [7, 8, 9, 10, 12, 16], [6, 7, 8, 9, 10, 11]]
Not able to identify the correct way to find the kth largest element from here,
returning sorted_list[-K] will not work in most of the cases where the last two sublists are of same length.
Don't confuse lists elements with sorting, sorting is done on the basis of the length of sub-lists and order is preserved in sorted_list
You can do this with Python's itertools.groupby applied to your sorted list: then accessing index -k of the grouped list gives you all lists of the kth largest length, of which you wanted the last one:
import itertools
nums = [[1, 2, 3, 4], [1, 9, 12], [9], [8], [7, 8, 9, 10, 12, 16], [7, 8, 9, 10], [4, 5, 6, 7], [6, 7, 8, 9, 10, 11]]
sorted_list = sorted(nums, key=len)
grouped_list = [list(g) for k, g in itertools.groupby(sorted_list, len)]
def kth_largest(k: int):
return grouped_list[-k][-1]
print(kth_largest(k=2))
print(kth_largest(k=1))
gives:
[4, 5, 6, 7]
[6, 7, 8, 9, 10, 11]
You could use a dictionary to find the unique elements by length and then sort the values to find the corresponding kth element:
lst = [[1, 2, 3, 4], [1, 9, 12], [9], [8], [7, 8, 9, 10, 12, 16], [7, 8, 9, 10], [4, 5, 6, 7], [6, 7, 8, 9, 10, 11]]
# the dictionary will store the last appearing element of the corresponding key (the later one in the processing)
lookup = {len(e): e for e in lst}
# sort the values of the lookup dictionary, reverse by len
res = sorted(lookup.values(), key=len, reverse=True)
k = 2
print(res[k - 1])
k = 1
print(res[k - 1])
Output
[4, 5, 6, 7]
[6, 7, 8, 9, 10, 11]
Suppose I start with a list as initial_list = [None] * 4. By setting depth = D, how can I define a routine to create a nested list of arbitrary depth in such way that each entry of the first list admits x-1 levels, being each level itself a list of other 4 elements. Something that afterwards would allow to slice data as for example myPrecious[0][0][3][0],myPrecious[3][2][1][0],... ?
You can use list comprehensions in a loop:
import copy
def build_list(depth, nb_per_level, fill=0):
l = [fill for _ in range(nb_per_level)]
for _ in range(depth):
l = [copy.deepcopy(l) for _ in range(nb_per_level)]
return l
>>> build_list(2, 3, 7)
[[[7, 7, 7], [7, 7, 7], [7, 7, 7]], [[7, 7, 7], [7, 7, 7], [7, 7, 7]], [[7, 7, 7], [7, 7, 7], [7, 7, 7]]]
>>> build_list(1, 5, 7)
[[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
>>> build_list(0, 5, 7)
[7, 7, 7, 7, 7]
>>> my_precious = build_list(3, 4, 7)
>>> my_precious[3][2][1][0]
7
Note the importance of copy.deepcopy(l) in the list comprehension, rather than just l, which makes a copy of the list instead of reusing the same list. Related question about the danger of not making a deepcopy: List of lists changes reflected across all lists unexpectedly?
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I am trying to print a list, delete item 0 and then print the list again for all items in the list. This is very simple but it's not working as I expected.It stops short of completing the for loop for all of the list items.
I tried using the range function inside a list first with both the del and pop() methods. That did not work so I have been using a list and get the same results.
seals = [1,2,3,4,5,6,7,8,9]
for seal in seals:
print(seals)
seals.pop(0)
Expected Result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[6, 7, 8, 9]
[7, 8, 9]
[8, 9]
[9]
Actual Result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
Python is funny here, so you have to be very clear to him :D :
seals = [1,2,3,4,5,6,7,8,9]
for seal in seals[:]:
print(seals)
seals.pop(0)
Or use range:
seals = [1,2,3,4,5,6,7,8,9]
for seal in range(len(seals)):
print(seals)
seals.pop(0)
Both reproduce:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[6, 7, 8, 9]
[7, 8, 9]
[8, 9]
[9]
If your intention is to pop values off until there are no more, it's probably clearer to write that intention into the code with something like:
seals = [1,2,3,4,5,6,7,8,9]
while seals:
print(seals)
seals.pop(0)
This avoids the problem of modifying the list while iterating the same list.
If you don't want to modify seals as you go, then you could do something like:
seals = [1,2,3,4,5,6,7,8,9]
for i, v in enumerate(seals):
print(seals[i:])
seals remains unaltered at the end of this.