With below code, I was expecting to swap two lists and was expecting all the elements from 'names' list to 'Excluded_list'using Pop() function. But it is not working expectedly, can someone please tell the issue with below code ??
names=['Ram','Sham','Mohan','John','Kuldeep']
Excluded_list=[]
print('names list',names)
print('Excluded list ',Excluded_list)
Code to swap/reprint lists
for name in names:
Excluded_list.append(names.pop())
print('names list',names)
print('Excluded list ',Excluded_list)
You are modifying "names" in the for-loop, which means that the "for name in names" does not evaluate as you expect.
Replace the for-loop with a while-loop.
while len(names):
Excluded_list.append(names.pop())
Related
I'm trying to clear an array after each iteration of a for loop in LabVIEW, but the way I've implemented it has the values not going directly to what I want, but it changes with previous values in other parts of the array.
It isn't shown, but this code is inside of a for-loop that iterates through another numeric array.
I know that if I get the array to clear properly after each loop iteration, this should work. How do I do that? I'm a beginner at Labview but have been coding for awhile - help is appreciated!!!
[![labview add to array][2]][2]
It looks as if you're not quite used to how LabVIEW passes data around yet. There's no need to use lots of value property nodes for the same control or indicator within one structure; if you want to use the same data in more than one place, just branch the wire. Perhaps you're thinking that a LabVIEW control or indicator is equivalent to a variable in text languages, and you need to use a property node to get or set it. Instead, think of the wire as the variable. If you want to pass the output of one operation to the input of another, just wire the output to the input.
The indicators with terminals inside your loop will be updated with new values every loop iteration, and the code inside the loop should execute faster than a human can read those values, so once the loop has finished all the outputs except the final values will be lost. Is that what you intended, or do you want to accumulate or store them in some way?
I can see that in each loop iteration you're reading two values from a config file, and the section is specified by the string value of one element of the numeric array Array. You're displaying the two values in the indicators PICKERING and SUBUNIT. If you can describe in words (or pseudocode, or a text language you're used to) what manipulation of data you're actually trying to do in the rest of this code, we may be able to make more specific suggestions.
First of all, I'm assuming that the desired order of operations is the following:
Putting the value of Pickering into Array 2
Extracting from Array 2 the values to put in Pickering 1 and Pickering 2
Putting Array 2 back to its original value
If this is the case, with your current code you can't be sure that operation 1 will be executed be fore operation 2. In fact, the order of these operations can't be pre-determined. You must force the dataflow, for example by creating a sequence structure. You will put the code related to 1 in the first frame, then code related to operation 2 in the second.
Then, to put Array 2 back to it's original value I would add a third frame, where you force an empty array into the Value property node of Array 2 (the tool you use for pickering, but as input and not as output).
The sequence structure has to be inside the for loop.
I have never used the property node Reinit to default, so I can't help you with that.
Unfortunately I can't run Labview on this PC but I hope my explanation was clear enough, if not tell me and I will try to be more specific.
The list starts empty. Then I want to append an value to it for each iteration in a loop if certain condition is met. I don't see append option in Variable Operation.
You can use string split for this, assuming you know of a delimiter that won't ever be in your list of values. I've used a semi-colon, and $local_joinedList$ starts off empty.
If (certain condition is met)
Variable Operation: $local_joinedList$;$local_newValue$ To $local_joinedList$
End If
String Operation: Split "$local_joinedList$" with delimiter ";" and assign output to $my-list-variable$
This overwrites $my-list-variable$.
If you need to append to an existing list, you can do it the same way by using String Join first, append your values to the string, then split it again afterward.
String Operation: Join elements of "$my-list-variable$" by delimiter ";" and assign output to $local_joinedList$
Lists are buggy in Automation Anywhere and have been buggy for several versions. I suggest not using them and instead use XML.
It it a much more versatile approach and allows you to do much more that with lists. You can search, filter, insert, delete etc.
For the example you mention, you would use the "Insert Node" command.
Throwing in my 2 cents as well - my-list-variable appears to be the only mutable in size list you can work with. From my experience with 10.7, it only grows though.
So if you made a list with 60 values, and you wanted to use my-list-variable again for 55, you'll need to clear out those remaining 5 values and create an if condition when looping over the list to ensure the values are not whatever you set those 5 values to be.
I used lime's answer as a reference (thanks lime!) to populate a list variable from some data in an Excel spreadsheet.
Here's my automation for it:
Very new to python - just started actually using it yesterday. I'm running a for loop that scans a text file and copies specific parts of it into variables that will then be put into a class "post". I want to create a new post at the bottom of the loop, named "post0", "post1", and so on, corresponding with the number of times the for loop has been run. This is what I'm trying to use:
postname = globals()['post%s' % s]
And I currently am trying to have it print the name of the post every time it creates one with a simple print(postname). 's' is the variable the for loop runs off of, if that makes sense. It starts at 0 and runs up to the number of lines in the text file, currently 424.
When I run the code, it returns "KeyError: post0". What am I doing wrong?
Also, reading around here it seems that creating variables this way is bad practice. If there is a more efficient way to do it I'd be happy to try that instead, but I'd also like to know how to make this method work just so I understand the concept. Thanks.
Edit: Problem solved! See my answer below.
I created a list postlist = [] outside of the loop, then inside the loop I append the list with a "post_" where "_" is the 's' variable. Looks like this:
postlist.append('post%s' % s)
Python is cool!
I typed the example code from the "arpackpp.doc", and got a run time error like this:
here is my code:
and the matrix is
well, I answer my question...
element of irow, which is pointer to an array of row indices of the nonzero elements contained in nzval, should start from 0 not 1.
myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
i=0
while (myRs.next()):
list= myRs.getString("I_COL")+','+myRs.getString("COL_NAME")
i have a jython code to run a sql statement i want to store all the row of the sql into a single variable. I used to list to store the value but its always storing only the single line , so is there is way to append all the rows and keep adding to single variable.
Thanks so much for your help.
With that code you overwrite the "list" variable in every iteration of the while loop (= is an assignment), try something like this (I used rs rather than list to avoid a name clash with the builtin function list()):
myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
rs=[]
i=0
while (myRs.next()):
rs.append(myRs.getString("I_COL")+','+myRs.getString("COL_NAME"))