I have a string.
s = '1989, 1990'
I want to convert that to list using python & i want output as,
s = ['1989', '1990']
Is there any fastest one liner way for the same?
Use list comprehensions:
s = '1989, 1990'
[x.strip() for x in s.split(',')]
Short and easy.
Additionally, this has been asked many times!
Use the split method:
>>> '1989, 1990'.split(', ')
['1989', '1990']
But you might want to:
remove spaces using replace
split by ','
As such:
>>> '1989, 1990,1991'.replace(' ', '').split(',')
['1989', '1990', '1991']
This will work better if your string comes from user input, as the user may forget to hit space after a comma.
Call the split function:
myList = s.split(', ')
print s.replace(' ','').split(',')
First removes spaces, then splits by comma.
Or you can use regular expressions:
>>> import re
>>> re.split(r"\s*,\s*", "1999,2000, 1999 ,1998 , 2001")
['1999', '2000', '1999', '1998', '2001']
The expression \s*,\s* matches zero or more whitespace characters, a comma and zero or more whitespace characters again.
i created generic method for this :
def convertToList(v):
'''
#return: input is converted to a list if needed
'''
if type(v) is list:
return v
elif v == None:
return []
else:
return [v]
Maybe it is useful for your project.
converToList(s)
Related
currently I can have many dynamic separators in string like
new_123_12313131
new$123$12313131
new#123#12313131
etc etc . I just want to check if there is a special character in string then just get value after last separator like in this example just want 12313131
This is a good use case for isdigit():
l = [
'new_123_12313131',
'new$123$12313131',
'new#123#12313131',
]
output = []
for s in l:
temp = ''
for char in s:
if char.isdigit():
temp += char
output.append(temp)
print(output)
Result: ['12312313131', '12312313131', '12312313131']
Assuming you define 'special character' as anything thats not alphanumeric, you can use the str.isalnum() function to determine the first special character and leverage it something like this:
def split_non_special(input) -> str:
"""
Find first special character starting from the end and get the last piece
"""
for i in reversed(input):
if not i.isalnum():
return input.split(i)[-1] # return as soon as a separator is found
return '' # no separator found
# inputs = ['new_123_12313131', 'new$123$12313131', 'new#123#12313131', 'eefwfwrfwfwf3243']
# outputs = [split_non_special(input) for input in inputs]
# ['12313131', '12313131', '12313131', ''] # outputs
just get value after last separator
the more obvious way is using re.findall:
from re import findall
findall(r'\d+$',text) # ['12313131']
Python supplies what seems to be what you consider "special" characters using the string library as string.punctuation. Which are these characters:
!"#$%&'()*+,-./:;<=>?#[\]^_`{|}~
Using that in conjunction with the re module you can do this:
from string import punctuation
import re
re.split(f"[{punctuation}]", my_string)
my_string being the string you want to split.
Results for your examples
['new', '123', '12313131']
To get just digits you can use:
re.split("\d", my_string)
Results:
['123', '12313131']
I have a list l.
l = ["This is","'the first 'string","and 'it is 'good"]
I want to replace all the whitespaces with "|space|" in strings that are within 's.
print (l)
# ["This is","'the|space|first|space|'string","and 'it|space|is|space|'good"]
I can't use a for loop inside a for loop and directly use .replace() as strings are not mutable
TypeError: 'str' object does not support item assignment
I have seen the below questions and none of them have helped me.
Replacing string element in for loop Python (3 answers)
Running replace() method in a for loop? (3 answers)
Replace strings using List Comprehensions (7 answers)
I have considered using re.sub but can't think of a suitable regular expression that does the job.
This works for me:
>>> def replace_spaces(str) :
... parts = str.split("'")
... for i in range(1,len(parts),2) :
... parts[i] = parts[i].replace(' ', '|')
... return "'".join( parts )
...
>>> [replace_spaces(s) for s in l]
['This is', "'the|first|'string", "and 'it|is|'good"]
>>>
I think I have solved your replacing problem with regex. You might have to polish the given code snippet a bit more to suit your need.
If I understood the question correctly, the trick was to use a regular expression to find the right space to be replaced.
match = re.findall(r"\'(.+?)\'", k) #here k is an element in list.
Placing skeleton code for your reference:
import re
l = ["This is","'the first 'string","and 'it is 'good"]
#declare output
for k in l:
match = re.findall(r"\'(.+?)\'", k)
if not match:
#append k itself to your output
else:
p = (str(match).replace(' ', '|space|'))
#append p to your output
I haven't tested it yet, but it should work. Let me know if you face any issues with this.
Using regex text-munging :
import re
l = ["This is","'the first 'string","and 'it is 'good"]
def repl(m):
return m.group(0).replace(r' ', '|space|')
l_new = []
for item in l:
quote_str = r"'.+'"
l_new.append(re.sub(quote_str, repl, item))
print(l_new)
Output:
['This is', "'the|space|first|space|'string", "and 'it|space|is|space|'g
ood"]
Full logic is basically:
Loop through elements of l.
Find the string between single quotes. Pass that to repl function.
repl function I'm using simple replace to replace spaces with |space| .
Reference for text-munging => https://docs.python.org/3/library/re.html#text-munging
I want to remove [' from start and '] characters from the end of a string.
This is my text:
"['45453656565']"
I need to have this text:
"45453656565"
I've tried to use str.replace
text = text.replace("['","");
but it does not work.
You need to strip your text by passing the unwanted characters to str.strip() method:
>>> s = "['45453656565']"
>>>
>>> s.strip("[']")
'45453656565'
Or if you want to convert it to integer you can simply pass the striped result to int function:
>>> try:
... val = int(s.strip("[']"))
... except ValueError:
... print("Invalid string")
...
>>> val
45453656565
Using re.sub:
>>> my_str = "['45453656565']"
>>> import re
>>> re.sub("['\]\[]","",my_str)
'45453656565'
You could loop over the character filtering if the element is a digit:
>>> number_array = "['34325235235']"
>>> int(''.join(c for c in number_array if c.isdigit()))
34325235235
This solution works even for both "['34325235235']" and '["34325235235"]' and whatever other combination of number and characters.
You also can import a package and use a regular expresion to get it:
>>> import re
>>> theString = "['34325235235']"
>>> int(re.sub(r'\D', '', theString)) # Optionally parse to int
Instead of hacking your data by stripping brackets, you should edit the script that created it to print out just the numbers. E.g., instead of lazily doing
output.write(str(mylist))
you can write
for elt in mylist:
output.write(elt + "\n")
Then when you read your data back in, it'll contain the numbers (as strings) without any quotes, commas or brackets.
I have string value like:
a='[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
I want to transform "a" into dictionary: the strings with preceding "-" character should be keys and what goes after the space should be values of the key preceding it.
If we are working with "a", then what I want is the resulting dictionary like:
dict_a={'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
This would be simple if not the last value('[[s dfdsf sdf]]'), it contains the spaces. Otherwise I would just strip the external brackets and split the "a", then convert the resulting list into dict_a, but alas the reality is not on my side.
Even if I get the list like:
list_a=['-sfdfj', 'aidjf', '-dugs', 'jfdsif', '-usda', '[[s dfdsf sdf]']
this would be enough.
Any help will be appreciated.
You can split the string by '-' and then add the '-' back.
a = '[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
a = a[1:-1] # get ride of the start and end []
sections = a.split('-')
dict_a = {}
for s in sections:
s = s.strip()
if len(s) == 0:
continue
key_value = s.split(' ') # split key value by space
key = '-' + key_value[0] # the first element is key
value = ' '.join(key_value[1:]) # the lefe are value
dict_a[key] = value
I can tell you a way to go about it.
Strip the quotes and the outer brackets. Then split the string using spaces. Iterate over the list obtained and check for any opening brackets. Keep a count of the number of opening brackets, join all the list items as a string with spaces between each such item until you encounter an equal number of closing brackets. The remaining items remain as is. You could try implementing it. If you face any issues, I'll help you with the code.
#chong's answer is a neater way to go about it.
Using a regular expression:
>>> import re
>>> dict(re.findall('(-\S+) ([^-]+)', a[:-1].replace(' -', '-')))
{'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
Using #ChongTang's idea:
>>> dict(('-' + b).strip().split(maxsplit=1) for b in a[1:-1].split('-') if b)
{'-sfdfj': 'aidjf', '-dugs': 'jfdsif', '-usda': '[[s dfdsf sdf]]'}
You can try this:
import re
a='[-sfdfj aidjf -dugs jfdsif -usda [[s dfdsf sdf]]]'
pattern_key=re.compile(r'(?P<key>-\S+)\s+')
pattern_val=re.compile(r' (?P<val>[^-].*?)( -|\Z)')
d={}
matches=pattern_key.finditer(a)
matches1=pattern_val.finditer(a)
for m,n in zip(matches, matches1):
d[m.group('key')]= n.group('val')
print d
How do I add a dot into a Python list?
For example
groups = [0.122, 0.1212, 0.2112]
If I want to output this data, how would I make it so it is like
122, 1212, 2112
I tried write(groups...[0]) and further research but didn't get far. Thanks.
Thankyou
[str(g).split(".")[1] for g in groups]
results in
['122', '1212', '2112']
Edit:
Use it like this:
groups = [0.122, 0.1212, 0.2112]
decimals = [str(g).split(".")[1] for g in groups]
You could use a list comprehension and return a list of strings
groups = [0.122, 0.1212, 0.2112]
[str(x).split(".")[1] for x in groups]
Result
['122', '1212', '2112']
The list comprehension is doing the following:
Turn each list element into a string
Split the string about the "." character
Return the substring to the right of the split
Return a list based on the above logic
This should do it:
groups = [0.122, 0.1212, 0.2112]
import re
groups_str = ", ".join([str(x) for x in groups])
re.sub('[0-9]*[.]', "", groups_str)
[str(x) for x in groups] will make strings of the items.
", ".join will connect the items, as a string.
import re allows you to replace regular expressions:
using re.sub, the regular expression is used by replacing any numbers followed by a dot by nothing.
EDIT (no extra modules):
Working with Lutz' answer, this will also work in the case there is an integer (no dot):
decimals = [str(g).split("0.") for g in groups]
decimals = decimals = [i for x in decimals for i in x if i != '']
It won't work though when you have numbers like 11.11, where there is a part you don't want to ignore in front of the dot.