How can I compress this into even shorter (one line?) [closed] - python

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 9 days ago.
Improve this question
Is there a way to compress this whole operation into one line?
# a string containing comma separated key value pairs
long_string = "value1=hello,value2=world"
# split string into pairs
kv_pairs = long_string.split(',')
# split into actual key value dictionary
my_dict = dict(i.split('=') for i in kv_pairs)
print(my_dict)

Related

Create dictionary values automatically? [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
So i have a list with strings for path of images:
["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
I want to automatically create a dictionary to store each photo:
{"folder1": ["_D7327", "_D7527"], "folder2": ["_D7455"], "folder3": ["_D1237"]}
How can I achieve something like this automatically?
You can use dict.setdefault. For example:
lst = ["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
out = {}
for path in lst:
folder, file = path.split("/")
out.setdefault(folder, []).append(file)
print(out)
Prints:
{'folder1': ['_D7327', '_D7527'], 'folder2': ['_D7455'], 'folder3': ['_D1237']}

Python: How to get all keys in a dictionary which start with a given substring s [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 have a dictionary with names as keys and the phone numbers as values
dictionary = {"ABCD":12345 , "EFGH":6789}
I want to enter a substring say AB and print all keys which start with that substring
I also want to return there respective values seperately
Matching keys and values could be assigned to separate variables using zip on the filtered dictionary items:
matching = lambda kv:kv[0].startswith("AB")
keys,values = zip(*filter(matching,dictionary.items()))

In python how to store the values of loop in a 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 2 years ago.
Improve this question
I tried to append these values in the list but it appends only else part.
I want to store these values i printed in a list. How can i achieve this??
for i in d["Cabin new"]:
if i=="nan":
print ("na")
else:
print (i[1:])
Append in both places:
res = []
for i in d["Cabin new"]:
if i=="nan":
res.append('na')
else:
res.append(i[1:])

How to remove all characters from a string after two white spaces in python? [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 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

Parsing List of Strings in Python [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 6 years ago.
Improve this question
I have the following list stored in a variable:
names = ['a.lastname1', 'b.lastname2']
I would like to parse out the '.' in each string and return a string so it reads each last name on two lines without the 'a.' and 'b.'
Simple solution using str.split() function:
names = ['a.lastname1', 'b.lastname2']
l1, l2 = (lname.split('.')[1] for lname in names)
print(l1, l2)
The output:
lastname1 lastname2

Categories

Resources