I have a list of list of coordinate with data type of float for example something like this
[[106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919], [106.4376068115234, -6.3299716218919]]
and I want to convert it so the inside bracket would be gone and replaced only by comma. and the comma inside the deepest bracket would be replaced too by space
and the final format would be something like
((106.4372634887695 -6.3303128514375, 106.4372634887695 -6.3299716218919, 106.4376068115234 -6.3299716218919))
Is there a good way to solve this?
I tried to use map and join but i didn't succeeded
You can flatten your array by using:
flatten = [elm for bracket in data for elm in bracket]
It's like:
arr = []
for bracket in data:
for x in bracket:
arr.append(x) #or arr += [x]
If you just want to sum brackets, do:
final = [a + b for a,b in arr]#b is -|b|, it's negative, then a+b is the same as 106.xxx - 6.xxx
The format you want is not the representation of any type which exists in Python. If you want it as a string, then, turn to string processing:
pairs = [[106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919], [106.4376068115234, -6.3299716218919]]
stringified = ', '.join(' '.join(map(str,pair)) for pair in pairs)
parenthesized = f'(({stringified}))'
Related
a = "a26lsdm3684"
How can I get an integer with value of 26(a[1] and a[2])? If I write int(a[1) or int (a[2]) it just gives me integer of one character. What should I write when I want integer with value of 26 and store it in variable b?
Slice out the two characters, then convert:
b = int(a[1:3]) # Slices are exclusive on the end index, so you need to go to 3 to get 1 and 2
you can get substrings out of the string and convert that to int, as long as you know the exact indexes
a = "a26lsdm3684"
substring_of_a = a[1:3]
number = int(substring_of_a)
print(number, type(number))
There is more than one way to do it.
Use Slicing, as pointed out by jasonharper and ShadowRanger.
Or use re.findall to find the first stretch of digits.
Or use re.split to split on non-digits and find the 2nd element (the first one is an empty string at the beginning).
import re
a = "a26lsdm3684"
print(int(a[1:3]))
print(int((re.findall(r'\d+', a))[0]))
print(int((re.split(r'\D+', a))[1]))
# 26
A little more sustainable if you want multiple numbers from the same string:
def get_numbers(input_string):
i = 0
buffer = ""
out_list = []
while i < len(input_string):
if input_string[i].isdigit():
buffer = buffer + input_string[i]
else:
if buffer:
out_list.append(int(buffer))
buffer = ""
i = i + 1
if buffer:
out_list.append(int(buffer))
return out_list
a = "a26lsdm3684"
print(get_numbers(a))
output:
[26, 3684]
If you want to convert all the numeric parts in your string, and say put them in a list, you may do something like:
from re import finditer
a = "a26lsdm3684"
s=[int(m.group(0)) for m in finditer(r'\d+', a)] ##[26, 3684]
In one of my dataset, I have the values
like this : [freshdesk,wordpress,mailchimp,microsoft_office_365,greenhouse,nginx]
I want to convert them back to ["freshdesk","wordpress","mailchimp","microsoft_office_365","greenhouse","nginx"]
for further processing
If the values in your dataset is clean enough, then you can loop through them with the following transformation:
value = '[freshdesk,wordpress,mailchimp,microsoft_office_365,greenhouse,nginx]'
value = value[1:-1].split(',')
>>>value
['freshdesk', 'wordpress', 'mailchimp', 'microsoft_office_365', 'greenhouse', 'nginx']
value[1:-1] slices your original string to 'freshdesk,wordpress,mailchimp,microsoft_office_365,greenhouse,nginx'
Then, when you split the string, it automatically partitions them into a list.
You can use re.findall:
s = '[freshdesk,wordpress,mailchimp,microsoft_office_365,greenhouse,nginx]'
re.findall('\w+', s)
['freshdesk',
'wordpress',
'mailchimp',
'microsoft_office_365',
'greenhouse',
'nginx']
I need to extract two numbers inside a string. They can look like this:
(0,0)
(122,158)
(1,22)
(883,8)
etc...
So I want to get the first number before the comma and the number after the comma and store them in variables. I can get the first number like this:
myString.split(',')[0][1:])
However, I can't figure out how to get the next number.
Thanks for the help everyone!
It should work with something like
myVar.split(',')[0][1:] # = 122 for the string in the second line
myVar.split(',')[1][:-1] # = 158 for the string in the second line
This should be the easiest way to do this
You could get rid of the parentheses, split the string, and convert each item to an int:
a, b = [int(x) for x in s[1:-1].split(',')]
Of course, if you absolutely sure about the string's format, and don't care about security, you could just eval the string:
a, b = eval(s)
You can use ast.literal_eval() to convert your string into a tuple. This will also take care about extra whitespace like '( 123, 158)'.
>>> from ast import literal_eval
>>> tup = literal_eval('(122,158)')
>>> tup[0]
122
>>> tup[1]
158
Or just:
>>> first, second = literal_eval('(122,158)')
Multi assignment, stripping the parentheses and splitting will do:
a, b = myString.lstrip('(').rstrip(')').split(',')
# a, b = map(int, (a, b))
myVar.split(',')[1][:-1])
will get you the second number
The simplest one-liner would be
a, b = (myString[1:-1].split(',')[0], myString[1:-1].split(',')[1])
Gets rid of the parentheses, then splits at the comma.
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
I have tried below code to split but I am unable to split
import re
s = "abcd[00451.00]"
print str(s).strip('[]')
I need output as only number or decimal format 00451.00 this value but I am able to get output as abcd[00451.00
If you know for sure that there will be one opening and closing brackets you can do
s = "abcd[00451.00]"
print s[s.index("[") + 1:s.rindex("]")]
# 00451.00
str.index is used to get the first index of the element [ in the string, where as str.rindex is used to get the last index of the element in ]. Based on those indexes, the string is sliced.
If you want to convert that to a floating point number, then you can use float function, like this
print float(s[s.index("[") + 1:s.rindex("]")])
# 451.0
You should use re.search:
import re
s = "abcd[00451.00]"
>>> print re.search(r'\[([^\]]+)\]', s).group(1)
00451.00
You can first split on the '[' and then strip the resulting list of any ']' chars:
[p.strip(']') for p in s.split('[')]