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
Related
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)
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
I have a list of words like below
["HEllO","HI","GREAT"]
and another list of tuples
[("HELLO",123),("HI",2134),("HELLO",65)]
If all the words in first list comes in second list atleast once then I need True as outcome else False.
Python version (based on comments from
Pranav Hosangadi and matszwecja)
a = ["HEllO","HI","GREAT"]
b = [("HELLO",123),("HI",2134),("HELLO",65)]
not (set(a) - set(x[0] for x in b))
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']}
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
How can I grab the invite code from this string?
{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}
The expected output would be "111A111A".
Any help is appreciated
I tried it in a simple way, You could give more details for further improvement.
s = "{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}"
print(s[-11: -3])
This will do it with ReGex
import re
def findInvite(s):
return re.search(r"(?<=/invite\\/).*(?=\\/)",s).group()
assert findInvite("{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}") == "111A111A"
And if this isn't a string but a dict, then change the function to:
def findInvite(d):
s = d["inviteURL"]
return re.search(r"(?<=/invite\\/).*(?=\\/)",s).group()
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
Context: I want to use Python to iterate over a bunch of json documents (cosmodb) and remove the last portion of a string in the value of a key
I want to turn this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
into this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
Any help would be greatly appreciated.
You could easily implement this with this piece of code:
result = my_string.rsplit('$', 1)[0] + "$"
Which behaves like this:
>>> my_string = "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
>>> print(my_string.rsplit('_', 1)[0])
"randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"