Python, How do I concatenate Strings and Array of arrays? [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 8 years ago.
Improve this question
The following is what I want to do (but I know it does not work).
msg["X-RECIP-ID"] = emailData['campaignId'] + "-" emailData['listId'] + "-" emailData['emailId'])
I know join(array, "-"), can do it for an array [campaignId, listId, emailId], but what I have currently have afaik is an array in an array.
What would be Pythonic way to do what I'm trying to do?

What you have with emailData isn't an array, or an array of arrays (or even a list of lists), but instead it's a Dictionary (see https://docs.python.org/2/tutorial/datastructures.html#dictionaries )
What I think you want to do is pull out certain values out of this dictionary into a new list, and then use the join method to put them all together with a "-" between them.
msgElems = [emailData[i] for i in ['campaignId', 'listId', 'emailId']]
msg["X-RECIP-ID"] = "-".join(msgElems)

Related

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/']

How to scrape data like integer [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 need this list deneme=['/n 1991','/n 1993','/n 2020']
pure_deneme=["1991","1993","2020"] like this list and all values must be integer. Thanks.
Thats easy with list comprehensions:
if by '/n' you mean '\n' then you can simply do:
new_list = [int(element) for element in old_list]
if it is indeed '/n' you may have to do:
new_list = [int(element.replace('/n ', '')) for element in old_list]
This difference is because int() already knows how to ignore spaces and new lines ('\n'), but if there are other characters in there you will need to remove them with .replace('whatever', '')

formating string and passing argument at runtime [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
eg-In this instead of using 21 (a value), I want to use a variable to generalize it
print("{:-^21}".format(".|."*(2*(i+1)-1)))
I want to use something like this
print("{:-^M}".format(".|."*(2*(i+1)-1)))
That can easily enough be done. For example:
M = 40
i = 3
print("{val:-^{width}}".format(width=M, val=".|."*(2*(i+1)-1)))
Outputs:
---------.|..|..|..|..|..|..|.----------
You could also do it with f-strings (note the outer ' because " is used on the inner expression):
print(f'{".|."*(2*(i+1)-1):-^{M}}')

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]

using split for appending to multiple lists [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 9 years ago.
Improve this question
Is it possible to combine the two statements inside 'for' loop.
num_pro=raw_input("ENTER THE NUMBER OF PRODUCTIONS: ")
right=[];left=[];
for i in range(int(num_pro)):
l,r=raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->')
right.append(r);left.append(l)
sample input: E->abc
Append tuples to one list, then split out the lists using zip():
entries = []
for i in range(int(num_pro)):
entries.append(raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->'))
left, right = zip(*entries)
zip(*iterable) transposes the nested list; columns become rows. Because you have two 'columns' (pairs of values), you end up with two rows instead.
Not without making it more complex. Each method needs to be called individually, and the only way to do that is either explicitly, as you have done, or in a loop.
If you are willing to store the whole production (which isn't necessarily a bad idea, since it keeps both sides synchronized) then just append the split result instead.
productions = []
for ...
productions.append(....split('->'))

Categories