Ok, I know there are several questions similar to this but not had any luck. What I have is (2) for loops with a while statement inside the inner for loop and 2 dictionaries with list as values. What I wish to accomplish is get a new key and do some calculations, enter second loop, count the number of items in the second dictionary value list, loop thru those list values using info from first loop to perform some calculations, once all values have been read for that key break back to the first loop get the next key and so on. The number of keys for each dictionary will always be equal. The v length for the seconddict will vary, for instance the first time thru the second for loop, the counterlist[1] value should be 9 but as shown in the results below it's only counting 4 which is the number of values in the first for loop. Actually the second For loop is only counting 4 for each time thru instead of the true length value of v. If I include the key in the second for loop such as for k, v in seconddict.items(), it does count correctly, but then I get the problem where on each entry into the second for loop it starts at the first k and duplicates all my results with each pass thru. Can I get some advice on making this work?
def opti(self, firstdict, seconddict):
self.firstdict=firstdict
self.seconddict=seconddict
for key, value in self.firstdict.items():
print('****NEW KEY AND DO OTHER STUFF****)
for v in self.seconddict[key]:
print(---INSDIE SECOND DICT---')
x=0
counterlist=[key, len(list(filter(None, v)))]
while x < couterlist[1]:
x+=1
print(x)
if x >= counterlist[1]:
if key in finaldict:
print('====SAVE HERE===')
else:
print('SOMETHING ELSE')
break
else:
continue
break
**CURRENT RESULTS:**
*********************NEW RUN*******************
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 3
1
2
3
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 3
1
2
3
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 3
1
2
3
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 3
1
2
3
======SOMETHING ELSE======
**NEEDED RESULTS:**
*********************NEW RUN*******************
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 9
1
2
3
4
5
6
7
8
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 2
1
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 5
1
2
3
4
======SOMETHING ELSE======
*********NEW KEY*********** SIZE 4
RAW DICT SIZE 4
------INSIDE RAWDICT--------- COUNTERLIST SIZE 3
1
2
3
======SOMETHING ELSE======
Well after little more looking, I was using the counterlist that I had been using in another section of code. By changing this to the following I was able to make it work. This might be useful for others using list as key value pairs in dictionaries to count the number of values inside each.
length_dict = {key: len(v) for key, v in self.rawdict.items()}
length_key = length_dict[key]
counterlist=length_key
Related
I know how to reshape a list into a table. But how do I turn a table into a list or uni- dimensional array.
my_list=:3 4 $i.12
0 1 2 3
4 5 6 7
8 9 10 11
And is it better to perform operations on lists or tables or is there no difference (in terms of performance)
, y (ravel) is what you need:
, my_list
0 1 2 3 4 5 6 7 8 9 10 11
There is no performance difference for operations where the shape of the data does not matter, f.e. 1 + my_list and 1 + , my_list. Also reshaping is free (if no padding is involved), because internally the atoms are always saved as a flat list with its corresponding shape. my_list could be understood as the tuple of the lists data: 0…11 and shape: 3 4, while , my_list would be data: 0…11 and shape: 12.
When I try to do average images input a vector asynchronously(for example, concurrency::concurrent_vector<cv::Mat>), How Can I parallelize sum about points or batches(1 row or 1 col or 3*3 array) of the same coordinates or Area?
I would appreciate it if you could tell me how to calculate the values in vector in columns or batch rather than in single units(ex. nested for).
(Edit)
For example
If I have 3 thread for image processing, and Each result are
thread 1
1 1 1
1 1 1
1 1 1
and thread 2
2 2 2
2 2 2
2 2 2
thread 3
6 6 6
6 6 6
6 6 6
then, just I want is
3 3 3
3 3 3
3 3 3
I thought two way for calculate average all thread's image.
1. just sum each thread result derivered to main thread and
count how much result derivered.
If thread1&2 result derivered to main thread.
(1) sum
3 3 3
3 3 3
3 3 3
(2) save count of sum and coordinate
In this example, save value will
1 - count of sum
Rect(0, 0, 2, 2) - coordinate to nested area
(3) If all thread's result coming, do average about nested area
9 9 9
9 9 9
9 9 9
if count of sum value is 2, find nested area and do average.
2(count of sum) / Rect(0, 0, 2, 2)
result will be
3 3 3
3 3 3
3 3 3
2. Just wait all thread's result derivered and do average in batches.
like
1|1 1
1|1 1
1|1 1
2|2 2
2|2 2
2|2 2
6|6 6
6|6 6
6|6 6
|
9
9
9
|
3
3
3
But, I don't know how to access and calculate by Memory References each thread's images. If thread 1 image address (in this case, 0,0 pixel data address in image) is 100, and thread 2 image address start is 200. then (0,0) pixel data in result images will calculate *100+*200.
Of course, before doing this operation, I will have to check that the memory matching the coordinates has the correct value.
And, Who told If I use std::reduce, will easy to implementation about this.
But, I have no idea how to apply that function in this way.
If you am only allowed to move the first element of an array, how many insertions does it take to fully sort the array?
In the output, give the number of insertions necessary as well as how many positions each element moves back.
For example:
Input:
6
1 4 2 5 3 6
Output:
4
3 4 2 4
Explanation:
This is the order of insertions:
4 2 5 1 3 6
2 5 1 3 4 6
5 1 2 3 4 6
1 2 3 4 5 6
I can do this in O(n2) since the problem simplifies to finding the position where the first element lies in the increasing suffix of the array.
How can I solve this in O(nlogn)?
My understanding as far as data frame in R is that it has to be rectangular. It is not possible to have a data frame with unequal column lengths. Can I use the lists in R to achieve this? What are he pros and cons for such an approach?
You can use lists to store whatever you want, even dataframes or other lists! You can indeed assign different length vectors, or even completely different objects. It gives you the same functionality as dataframes in that you can index using the dollar sign:
> fooList <- list(a=1:12, b=1:11, c=1:10)
> fooList$a
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> fooDF <- data.frame(a=1:10, b=1:10, c=1:10)
> fooDF$a
[1] 1 2 3 4 5 6 7 8 9 10
But numeric indexing is different:
> fooList[[1]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> fooDF[,1]
[1] 1 2 3 4 5 6 7 8 9 10
as well as the structure and printing method:
> fooList
$a
[1] 1 2 3 4 5 6 7 8 9 10 11 12
$b
[1] 1 2 3 4 5 6 7 8 9 10 11
$c
[1] 1 2 3 4 5 6 7 8 9 10
> fooDF
a b c
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
Simply said a dataframe is a matrix and a list more of a container.
A list is meant to keep all sorts of stuff together, and a dataframe is the usual data format (a subject/case for each row and a variable for each column). It is used in a lot of analyses, allows to index the scores of a subject, can be more easilly transformed and other things.
However if you have unequal length columns then I doubt each row resembles a subject/case in your data. In that case I guess you don't need much of the functionality of dataframes.
If each row does resemble a subject/case, then you should use NA for any missing values and use a data frame.
I'm having problems removing a vector from a "multidimensional vector"
I would like to achieve this:
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 4 4 4 4
4 4 4 4
for example
vector<vector<int>>vec;
for i...//give vec values...
vec[3].erase(vec.begin(),vec.end());
It seems like using vector.erase() or vector.clear() leaves an empty vector at the "third row"
Is there a way to completetly remove that vector so that
vec[3]=4 4 4 4
Thanx for a great forum...
/Bux
The following line removes the third element of vec. If it had four elements, it will have three after the line is executed.
vec.erase(vec.begin() + 2);
The following line, on the other hand, will leave the third vector empty.
vec[2].clear();