Replace String only if it is not present - python

Let's say I have a dictionary, like this {"view": object.get("view")}. Let's say the value to be returned is "Table".
I want to add "_string" to it only if it is not already present. So Table should be Table_string, but if it is already Table_string, then let it be.
For example {"view": object.get(f"{self.view}_string")}, but only if "_string" is not already present. How can I do that using python?

you can do it using when otherwise and regexp_replcae function in pyspark
df.withColumn("value",when(value=="Table",regexp_replace(col(value),"Table","table_String")).otherwise(col(value))

Related

Is there a Python pandas function for retrieving a specific value of a dataframe based on its content?

I've got multiple excels and I need a specific value but in each excel, the cell with the value changes position slightly. However, this value is always preceded by a generic description of it which remains constant in all excels.
I was wondering if there was a way to ask Python to grab the value to the right of the element containing the string "xxx".
try iterating over the excel files (I guess you loaded each as a separate pandas object?)
somehting like for df in [dataframe1, dataframe2...dataframeN].
Then you could pick the column you need (if the column stays constant), e.g. - df['columnX'] and find which index it has:
df.index[df['columnX']=="xxx"]. Maybe will make sense to add .tolist() at the end, so that if "xxx" is a value that repeats more than once, you get all occurances in alist.
The last step would be too take the index+1 to get the value you want.
Hope it was helpful.
In general I would highly suggest to be more specific in your questions and provide code / examples.

UpdateExpression DynamoBD to Append Items to a List only if they are Unique (avoid duplicates)

The basic setup is to have a Key Value Pair where the Key is an ElementID and the Values is a list of all the locations in which that Element is present.
using boto3 libraries, with the table.update_item(), but the important part is the update expression.
The farthest I got was
"SET locations = list_append(if_not_exists(locations, :newlocations), :newlocations)"
Which doesn't seem to work.
The goal is that if you have an Element that already exists in DynamoDB, with a location list of {file0,file1,file2}, that if you update with {file2,file3}, it will just add file3 and not duplicate file2
Your code doesn't work, because if the attribute "locations" already exists, if_not_exists just takes to it, and the list_append will append to it :newlocations, without any de-duplication happening.
If you want de-duplication, you should not be using a list - you should be using a set, which similar to a list but different, and just like you wanted, eliminates duplicate values. To add items to a set, just use the update expression ADD locations :newlocations.
That's it - you don't need list_append (which only works for lists, not sets), nor if_not_exists (the ADD operation already works correctly for a non-existent item or attribute and creates it).

Python get function [duplicate]

This question already has answers here:
Why dict.get(key) instead of dict[key]?
(14 answers)
Closed 3 years ago.
monthConversions={
"Jan":"January", #key:value
"Feb":'February', #make sure keys are unique
'Mar':'March', #can also use integers
'Apr':"April",
'May':'May',
'Jun':'June',
}
print(monthConversions['Jan']) #gives value associated with the key
print(monthConversions.get('Luv','not a valid key'))
Hi, I am currently learning python through freebootcamp on youtube, and the person mentioned we can use the get function to pass in a value for a key that is not in the dictionary. I kind of understand that, but I fail to see what the use of this would be. I thought it would add Luv in when I put in
print(monthConversions['Luv'])
but it instead gives a error. What would be the purpose of the get function in this situation anyway? It feels like extra work and I don't get how it would be useful. Any explanations would be greatly appreciated, thank you.
It does not change the dict. The second argument is simply what get() returns when key is not found. If you don't specify it, it will simply return None.

Check an object in a list of values

First, I would like to mention I am to automated testing.
I want to add a test case to Robot Framework which will let me choose from a list (which can change).
I have problem with creating keywords to do this.
Can somebody give me a tips to do this?
I have a list, but the values can change. I need to check values inside the list the list.
There is a value in the list that I would like to change.
Edit: The questions applies the list on web page.
In your test case you'll need to import the library Collections.
To check a value in a list you can use the keyword
list should contain value #{MyList} Value
If you want to set a value in the list to something else you can use the keyword
set list value #{MyList} 1 Value
If you want to learn more about these keywords you can find them here.
http://robotframework.org/robotframework/latest/libraries/Collections.html

How to set value in python dictionary while looking it up from another dictionary?

I have two dictionaries. The first is mapping_dictionary, it has several key-value pairs. It will serve as a reference. The second dictionary only has two key-value pairs. I would like to look up the value that should be assigned to the second dictionary in the mapping_dictionary and set it to one of the values. I tried doing it a few different ways but no success.
Please let me know if the syntax is wrong or if this is not the way to do something like this in Python? Thank you in advance for any help.
Example 1:
mapping_dictionary={'TK_VAR_DEC':1, 'TK_ID':2, 'TK_COMMA':3}
token_dictionary={'TK_TYPE', 'TK_VALUE'}
tk_v=mapping_dictionary.get("TK_VAR_DEC")
token_dictionary['TK_TYPE']=tk_v
token_dictionary['TK_VALUE']="VAR_DEC"
Example 2:
token_dictionary['TK_TYPE']=mapping_dictionary.get("TK_VAR_DEC")
token_dictionary['TK_VALUE']="VAR_DEC"
With the definition of the token_dictionary, you're not defining a dictionary at all -- you've written the literal syntax for a set. You need to specify values for it to be a dictionary. I expect that if you change to using token_dictionary = {'TK_TYPE': None, 'TK_VALUE': None} you'll have more luck.
Also note that using .get() is unnecessary for retrieving a value from the dictionary. Just use [].

Categories