Delete key from python dictionary having specific format [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Key column of my dictionary contain words such as
1. Performance_appraisal
2. Continuous_feedback etc
3. *_appraisal
I need to delete words which are similar to the third example. i.e containing *_ in it
How do I do that?

You can use:
def clean_dict(my_dict):
for key in my_dict.keys():
if '*_' in key:
del my_dict[key]
return clean_dict(my_dict)

Related

how i can organize and use reverse in a dictionary python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
this is dictionary I don't know how to use sort and reverse.
for some reason don't print the dictionary
disc: {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
I get that the 'disc' is a variable holding the dictionary.
In this case just sort and reverse the values that are lists.
disc = {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
for l in disc.values():
l.reverse()
for l in disc.values():
l.sort()

How can I sort the a list of strings which contain date as substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']

What is the correct Python syntax for this? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to get only the first match if key is in dict. Else I would like to get a NaN. I'm struggling with the correct syntax to fit my needs. The match doesn't have to be in a list, I just need the number. I tried tons of variations...:
'Temperatur':[d2.get(key) for key in temperatur if key in d2][0] else None
'Temperatur':d2.get(key) for key in temperatur if key in d2 else None
You probably want next with a default:
'Temperatur': next((d2.get(key) for key in temperatur if key in d2), None)

data=data={...} How to delete the first data= from such strings? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I got data=.... where I've done splitting so text.split("=") and chose the second list element.
But with my new data in this way it deletes even the data= and the "{" "}" Elements
Any suggestions?
You should choose the second element through the end of the list, then rejoin them.
text = "=".join(text.split("=")[1:])
Or you could do a string replacement:
text = text.replace("data=data=", "data=")

How do you convert a single entity list into a string python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to convert a tuple:
('Cobra',)
to a string, which when printed yields:
Cobra
#Assuming you have a list of tuples
sample = [('cobra',),('Cat',),('Dog',),('hello',),('Cobra',)]
#For each tuple in the list, Get the first element of each tuple
x = [i[0] for i in sample]

Categories