Program in to generate chained list in python - python-3.x

I am developing a program in python and as part of it I have to link all the lists that have an element in common in a certain position, that is, there is an input element and an output element and I want to gather all those that follow the chain. For example, we have as input a list :
list_array = [[n_element, l_input, l_ouput], .....]
A concrete example would be
list_array = [[1,a,b],[2,c,d],[3,e,f],[4,b,e],[5,d,f],[6,a,e],[7,b,c]
The result of the program should be a list where the elements are linked by input and output.
res_array = [[1,4,3],[1,7,2,5],[6,3]]
The result of the program should be a list where the elements are linked by input and output. If there is one element included in another, the element with greater length prevails. My first thought was to use a tree structure, a search in depth or length. I need ideas.

You could use a graph representation of this problem:
For each element [n_element, l_input, l_output], you add vertices l_input and l_output to your graph (if not already present) and add an edge labelled n_element (from l_output to l_input).
Then, you look for paths through that graph. The resulting list is then given by the concatenation of edge labels.

Related

Problem With SPLIT FUNCTION And RANDOM Module

[](https://i.stack.imgur.com/AWJQV.png)
i get input from user and created the list using split function. and while using random.randint()
it should produce random intger from the list but it prints the entire list,
but it works properly when i created my own list . what is the problem here.
you can see my problem in the image

How to remove an element from LinkedList in stable Rust?

I have a linked list of values and I want to remove elements from there either by value or by index. I tried to use the remove function, but the compiler says that this function is not stable. Is it possible to remove elements from an arbitrary location in a linked list using only stable Rust?
You can split the array using split_off then join them back together without the element. See this comment on github
// Split the list in 2
let split_list = original_list.split_off(index_to_remove);
// Remove the first element of the second half
split_list.pop_front();
// Join the 2 halves back together, except for the middle element
original_list.append(split_list);

iterating over list in list; python

I have a simple problem. I'm new to python and programming so I think i miss something.
The variable "account_info" is assigned earlier and is a list of lists with 4 elements each. The variable current is a user input value, which (should) appear as the first element of the lists in the list account_info.
I want to iterate over the lists in the list and compare if the first element is equal to "current".
This is the code:
for i in account_info:
if current == account_info[i][0]:
email = account_info[i][1]
additional = account_info[i][2]
pw = account_info[i][3]
print(email)
I get an error in pycharm, when running that code. It seems that I can't iterate over the lists like that, can please someone explain and show a different solution?
Thank you
As #ForceBru commented, your issue is due to how for loops in Python work. The value you get from the loop is not an index into the iterable object you're looping on, rather, it's a value from the iterable. That makes your indexing with it later almost certainly wrong (though in certain contexts it might make sense, if you have a list that contains indexes into itself).
In your case, you probably want to do something more like this:
for account in accounts_info:
if current == account[0]: # note, only the inner indexing is needed
email = account[1]
additional = account[2]
pw = account[3]
Since you're expecting the inner lists to contain four values, you could even unpack the account values that you get from iterating directly into the inner variables. Though this would happen unconditionally, so it might not do what you want. Here's what that would look like, with the print call you were doing after the loop instead moved inside the conditional (so you only print the one email address that corresponds to the value in current):
for account_id, email, additional, pw in account_info: # unpack unconditionally
if account_id == current: # use the convenient name here
print(email) # print only in the conditional
In the rare case where you really do need to iterate over indexes, you can use the range type, which behaves like a sequence of integers (starting at zero by default). So you could replace your loop with this version and the body would work as you had intended (though this is less idiomatic Python than the previous versions).
for i in range(len(accounts_info)):
If you need both the index and the current value, you can use the enumerate function, which yields 2-tuples of index and value as you iterate over it. This is often handy when you need to reassign values in a list some times:
for i, account in enumerate(accounts_info):
if account[0] == current:
accounts_info[i] = new_value # replace the whole account entry

How to find most similar to an array in gensim

I know the most_similar method works when entering a previously added string, but how do you reverse search a numpy array of some word?
modelw2v = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz',binary=True)
differenceArr = modelw2v["King"] - modelw2v["Queen"]
# This line does not work
modelw2v.most_similar(differenceArr)
The most_similar() method can take vectors as the origin of a search, but you should explicitly specify them as one member of a list provided to the method's positive parameter, so that its logic for handling more simple origins (like a string or list of strings) isn't confused.
Specifically, this should work with your other code:
model23v.most_similar(positive=[differenceArr,])
More generally, you can supply lists of vectors (or word-keys for looking up vectors) to both the positive and negative parameters of this method, and the method will combine them (according to the exact logic you can see in the source code). So for example the prominent word2vec example...
wv('king') - wv('man') + wv('woman') = ?
...can be effected with the most_similar() method without doing your own other vector-arithmetic:
sims = modelw2v.most_similar(positive=['king', 'woman'], negative=['man'])

Placing "markers" in MathJax math expressions

Suppose I have some equation, say:
$$\underbrace{ \frac{a}{b} }_{c}$$
And, I want to get the location of the $c$ in the HTML/CSS/SVG output of MathJax.
Is there a way to do this? I.e. I'd like to do something like:
$$\underbrace{ \frac{a}{b} }_{c\invisiblemarkerXYZ}$$
then be able to do a query to get the DOM element corresponding with invisiblemarkerXYZ
Thanks!
EDIT this is what I want to do:
Equation 1 = $$\underbrace{\frac{a}{b}}{A}$$
Equation 2 = $$A = \sum_{i=1}^n i$$
Now, I want to draw a line (via SVG) of the two A's. Thus, I need some way to obtain the location of the MathJax elements.
You can use \cssId{XYZ}{c} to set the id="XYZ" on the element used for the c, and can then use document.getElementById("XYZ") to obtain that DOM element. But the output from MathJax's HTML-CSS and SVG processors is not designed to be manipulated after the fact. For example, in general you will not be able to get the dimensions of the element from the HTML-CSS output as the offsetHeight and offsetWidth may not be what you expect them to be. (The height is frequently set to 0, for example.)
Can you say something more about what you are trying to accomplish?

Resources