I have a List of Strings in Python and I want to check if the string "EVN" does not exist in the range segmentList[x][0:3], so I can create it in a specific place in the list.
I'm trying to check if the string in this range (segmentType = segmentList[x][0:3]) in the list and if it doesn't I want to call a method create_EVN() and insert the "EVN" string into the second position of the list moving up the other elements rather that deleting them.
I'm fairly new to python and I'm trying to find the most efficient way possible to do this. I have tried looping through the list with no avail.
Is this what you are looking for?
if 'EVN' not in segmentList[x][0:3]:
create_EVN()
How about
if 'EVN' not in segmentList[x][0:3] :
create_EVN()
Related
I'm trying to work with a custom group node that has a bunch of EvalString_Knobs and I need to get a list of items in them but when I try using node['knobName'].values()
I get an attribute error as values isn't an attirbute of EvalString_Knob.
Anyone have a way to get the values of EvalString_Knob?
Thanks.
Let's create two EvalString_Knob inside a new tab:
import nuke
write = nuke.createNode('Write', inpanel=False)
tab = nuke.Tab_Knob("Parameters")
write.addKnob(tab)
write.addKnob(nuke.EvalString_Knob('prefix','Prefix','render'))
write.addKnob(nuke.EvalString_Knob('suffix','Suffix','7'))
In UI it looks like that:
For listing all the dictionary's knobs of a Write node use the following command:
nuke.toNode("Write1").knobs().keys()
To get a value of any known knob use this command:
nuke.toNode("Write1").knob('prefix').getValue()
To list all the properties with their corresponding values use this approach:
nuke.selectedNode().writeKnobs()
P.S.
TabKnobs have no names to reach them. Only empty strings.
You can use
node['knobName'].getValue() to get a string returned from an EvalString_Knob,
node['knobName'].values() to get a list returned from an Enumeration_Knob,
and you can use node['knobName'].Class() to find out what is the type of the knob.
After reading some similar questions I understand that you cannot .strip() a list of strings, only individual strings. I have a list statesData that is made of strings, and I want to strip and split each individual element. I've been able to get it to work by using two lines, but my instructor used one line for an example problem and I'm trying to figure out why I can't get the same result. What I'm confused about is that I thought that the index location [i] should have selected an individual string element within the list which could be stripped?
statesData = ['State,Population,ElectoralVotes,HighwayMiles,SquareMiles\n',
'Alabama,4802982,9,213068,52419.02\n', 'Alaska,721523,3,31618,663267.26\n']
#etc
for i in range(len(statesData)):
statesData[i] = statesData[i].strip().split(',')
statesData = ['State,Population,ElectoralVotes,HighwayMiles,SquareMiles\n',
'Alabama,4802982,9,213068,52419.02\n', 'Alaska,721523,3,31618,663267.26\n']
#etc
new_statesData = [i.strip().split(',') for i in statesData]
List comprehension is one line and easy to understand and very fast.
I am learning python and am currently working on a battleship game (not codecademy) for a computer science class. I was wondering what would be the best way to replace a item in a list based on its location in the list, with another item.
For example
board_row_1 = ['0','0','0','0','0']
# Would want to replace item 2 with an 'x'
Cant really find an answer to this anywhere on the internet. Unlike replacing a certain string/integer, I am replacing it based on the location of the item.
if you want to replace items in the same list the best way is this :
board_row_1[x],board_row_1[y]=board_row_1[y],board_row_1[x]
I have two arrays, where if an element exists in an array received from a client then it should delete the matching array in the other array. This works when the client array has just a single element but not when it has more than one.
This is the code:
projects = ['xmas','easter','mayday','newyear','vacation']
for i in self.get_arguments('del[]'):
try:
if i in projects:
print 'PROJECTS', projects
print 'DEL', self.get_arguments('del[]')
projects.remove(i)
except ValueError:
pass
self.get_arguments('del[]'), returns an array from the client side in the format:
[u'xmas , newyear, mayday']
So it reads as one element not 3 elements, as only one unicode present.
How can I get this to delete multiple elements?
EDIT: I've had to make the list into one with several individual elements.
How about filter?
projects = filter(lambda a: a not in self.get_arguments('del[]'), projects)
Could try something uber pythonic like a list comprehension:
new_list = [i for i in projects if i not in array_two]
You'd have to write-over your original projects, which isn't the most elegant, but this should work.
The reason this doesn't work is that remove just removes the first element that matches. You could fix that by just repeatedly calling remove until it doesn't exist anymore—e.g., by changing your if to a while, like this:
while i in projects:
print 'PROJECTS', projects
print 'DEL', self.get_arguments('del[]')
projects.remove(i)
But in general, using remove is a bad idea—especially when you already searched for the element. Now you're just repeating the search so you can remove it. Besides the obvious inefficiency, there are many cases where you're going to end up trying to delete the third instance of i (because that's the one you found) but actually deleting the first instead. It just makes your code harder to reason about. You can improve both the complexity and the efficiency by just iterating over the list once and removing as you go.
But even this is overly complicated—and still inefficient, because every time you delete from a list, you're moving all the other elements of the list. It's almost always simpler to just build a new list from the values you want to keep, using filter or a list comprehension:
arguments = set(self.get_arguments('del[]'))
projects = [project for project in projects if project not in arguments]
Making arguments into a set isn't essential here, but it's conceptually cleaner—you don't care about the order of the arguments, or need to retain any duplicates—and it's more efficient—sets can test membership instantly instead of by comparing to each element.
I have a class containing a list of strings. Say:
ClassName:
- list_of_strings
I need to enforce that this list of strings contains unique elements. Unfortunately, I can't change this list_of_strings to another type, like a set.
In the addToList(str_to_add) function, I want to guarantee string uniqueness. How can I best do this? Would it be practical to add the string being added to the list, convert to a set, then back to a list, and then reassign that to the object?
Here's the method I need to update:
def addToList(self, str_to_add):
self.list_of_strings.append(str_to_add)
Thanks!
def addToList(self, str_to_add):
if str_to_add not in self.list_of_strings:
self.list_of_strings.append(str_to_add)
Either check for the presence of the string in the list with in, or use a set in parallel that you can check and add to.
You indeed could do the list-to-set-to-list operation you described, but you could also use the in operator to check if the element is already in the list before appending it.
One possible way to do this would be to create a hash set and iterate through the list, adding the elements to the set; a second iteration could be used to remove any duplicates.
Perhaps we can do like this:
def addToList(self, str_to_add):
try:
self.list_of_strings.index(str_to_add)
except:
self.list_of_strings.append(str_to_add)
Well, I don't know whether it's the same mechanism with if/else yet.