How to remove hyphens from a list of strings [duplicate] - python

This question already has answers here:
How to delete all instances of a character in a string in python?
(6 answers)
Closed 6 years ago.
['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
How can I remove all the hyphens between the numbers?

You can just iterate through with a for loop and replace each instance of a hyphen with a blank.
hyphenlist = ['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
newlist = []
for x in hyphenlist:
newlist.append(x.replace('-', ''))
This code should give you a newlist without the hyphens.

Or as a list comprehension:
>>>l=['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
>>>[i.replace('-','') for i in l]
['000', '11020', '31015', '23020', '105', '1106', '31030', '3104']

Related

how to convert space to underscore in python list [duplicate]

This question already has answers here:
Find and replace string values in list [duplicate]
(6 answers)
How to change the list separator in python?
(2 answers)
Replacing instances of a character in a string
(17 answers)
Closed 2 years ago.
I have a list like this ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL'] and I want to convert it to ['BASE', 'BASE_xBU_xPY', 'BU_GROU_ REL', 'PY_REL'] in short convert space to underscore for value in b/w quotes
I would suggest using replace along with map
Example:
my_list = ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL']
converter = lambda x: x.replace(' ', '_')
my_list = list(map(converter, my_list))
my_list
['BASE', 'BASE_xBU_xPY', 'BU_GROUP_REL', 'PY_REL']

Python - split list element containing character (e.g. /,[]) and create new elements [duplicate]

This question already has answers here:
How to split strings inside a list by whitespace characters
(4 answers)
Split a list of strings by comma
(4 answers)
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I have a list that looks like:
test = ['bla bla','affect /affected / affecting']
print(test)
and I would like to create new elements in the list by separating the ones that are one element but separated through the "/" symbol.
E.g. desired output in this case:
test_new = ['bla bla','affect','affected','affecting']
How can I do that?
EDIT: The list can have multiple elements, and not all have the "/" symbol
You can use a list comprehension and split on '/' and strip to remove spaces (assuming you have multiple strings, being a list):
[j.strip() for i in test for j in i.split('/')]
# ['affect', 'affected', 'affecting']
testEntry=test[0]
testEntry=test.replace(' ','')
test=testEntry.split('/')

Remove a string from a list based on that string containing a certain character [duplicate]

This question already has answers here:
Removing elements from a list containing specific characters [duplicate]
(4 answers)
Closed 4 years ago.
How do you write a function that removes a string from an array if that string contains a certain character
For an example you would be removing all strings that contain an 'a'.
my_list = ["apples", "plums", "oranges", "lemons"]
You can do this with list comprehension or a simple for loop, the key is you want to check if 'a' not in something, if there is an a you don't want it
print([i for i in my_list if 'a' not in i])
Expanded:
for i in my_list:
if 'a' not in i:
print(i)
It is simple as follows:
def get_filtered_list(my_list, sub_string):
return [string for string in my_list if sub_string not in string]

Convert a list of characters in a single List , using a List comprehension [duplicate]

This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 6 years ago.
I have this list :
listOfWords = ["this","is","a","list","of","words"]
and I need to have [this is a list of words], with a List comprehension.
You mean a list with a single string item?
>>> [' '.join(["this","is","a","list","of","words"])]
['this is a list of words']

Find letters in string without using for loop [duplicate]

This question already has answers here:
Extracting only characters from a string in Python
(7 answers)
How do you filter a string to only contain letters?
(6 answers)
Closed 6 years ago.
I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.
This is how I used the for loop:
>>> a = "Today is April 1, 2016"
for i in a:
if i.isalpha():
list(i)
Any help will be appreciated!
You can use filter for this:
>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
list(set([x for x in a if x.isalpha()]))
this should do it :)

Categories