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', '')
Related
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 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.
res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.
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 want to delete Last And first element of List of elements
Ex:
List = ['SGAAG_Anhaenger_', '_Anhaenger_', '_Test_Data']
From the above list i want to delete in first element last character and First and Last character for 2nd Element and first character for 3rd element if last/first character is "_".
It looks like what you actually want to do is strip off leading and trailing '_' characters.
>>> lis = ['SGAAG_Anhaenger_', '_Anhaenger_', '_Test_Data']
>>> [s.strip('_') for s in lis]
['SGAAG_Anhaenger', 'Anhaenger', 'Test_Data']
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 4 years ago.
Improve this question
I have a list output:
['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
and I want to clean it up in order to output:
[40RAAC34,40RAAC33]
If you have a string:
'hello (world)'
and want the text between the brackets, you can either use a regex:
import re
re.findall('\((.*?)\)', s)[0]
#'world'
or, if you are sure that there is only one set of brackets (i.e. no leading ) chars) then you can just use slicing:
s[s.index('(')+1:s.index(')')]
#'world'
So then you just need to throw this into a list-comprehension or similar.
l = ['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
[s[s.index('(')+1:s.index(')')] for s in l]
#['40RAAC34', '40RAAC33']
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]
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)