I want to assign the array item into variable directly using groovy like this:
def str = "xyz=abc"
def [name, value] = str.split("=")
but groovy doesn't like it. Is there a way to do that (not storing the array result and get the index[0], index[1] from it?).
Thanks,
You just need parenthesis instead of brackets:
def str = "xyz=abc"
def (name, value) = str.split("=")
Note that you'll need to know how many elements you're expecting or you'll have unexpected results.
def name, value
(name,value) = str.split("=")
You just need to do your definition before your multiple assignment.
Related
Consider the following function:
def Func(x):
Code
return List_A, List_B, number_C
I want to store one of the returned values as some variable "x" so how can I go about doing this? For example, I want to store List_B as x
I tried
x=Func[1]
Detailed explanation:
n=int(input("blablabla"))
def Func(n)
l_e=[]
for i in range (1,n):
if i%2==0:
l_e.append(i)
l_o=[]
for i in range (1,n):
if i%2>0:
l_o.append(i)
l=len(l_0)+len(l_e)
return l_o, L_e, l
let's say I want to store the second item returned as a variable, how can I do this. I tried x=Func[1], and x=Func()[1] but to no avail.
The following works: x = Func()[1]. The parentheses call the function, then the square brackets pick the item you want out of the returned tuple.
In order to make my code more lisible, I need to find a way to shorten this expression using a '\n'.join method :
def __repr__(self):
string = str()
for row in self._map:
string += ' '.join(row)+'\n'
return string
edit : forgot to mention self._map was a list of lists of strings
The most efficient way to do so is :
return '\n'.join([' '.join(row) for row in self._map])
I think it's clearer this way than using a map() function and I heard it should also be faster since a lambda function is declared in the map() function.
I am trying to build an input parameter which is a dictionary, but it's format is different in compare with normal dictionary,
what i already have is this
{"sort": {"-p1.m_visits_all"}}
and I am trying to change the format of the dictionary which is valid one in my rest query, the final format should be like this
{"sort": "{-p1.m_visits_all}"}
but I don't know how to do it. Any suggestions?
First, you can convert each dict value to a string with str(...).
Now your example would look like this:
{"sort": "{'-p1.m_visits_all'}"}
Next, you remove all single quotes (') in the values
. Now you have:
{"sort": '{-p1.m_visits_all}'}
Finally, you may convert it to JSON with json.dumps(), and you and up with:
'{"sort": "{-p1.m_visits_all}"}'
Here is a function that formats a dict and then returns a JSON string:
def format_for_api(params):
formatted = {}
for k, v in params.items():
params[k] = str(v).replace("'", "")
return json.dumps(formatted)
Or, using compressions:
def format_for_api(params):
return json.dumps({k: str(v).replace("'", "") for k, v in params.items()})
Happy coding :)
I want to do something like this in Groovy:
List<MyObject> list1 = getAList();
How can I assign a list to other list in groovy without having to iterate through the list I want to assign?
That will work as you have it (assuming getAList returns a List)...
Or do you mean you want a new list containing the same elements as the list returned?
If that's the case, you can do
List<MyObject> list1 = getAList().collect()
Or
List<MyObject> list1 = new ArrayList<MyObject>( getAList() )
If you only want to concatenate the elements of one array/list with another, you can do the following:
def ar1 = ["one","two"]
def ar2 = ["three","four"]
def ar3 = ar1 + ar2
return ar3
This should produce
["one","two","three","four"]
I have a list like below:
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
I want to be able to do
users = []
users = rawinput.split(",")
print(users)
How do I do this in Python 3.2? Thanks.
What you have,
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
is a list of strings already. You can just iterate through it as a list. You don't need to split anything.
If you had something like this,
rawinput = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
rawinput.split(',') would return the above list.
split() is applied on string, in return it gives you a list[] which contains the substring as elements in order of the parent string.
In your case:
input = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
input.split(',')
will return
['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']