Efficient way of parsing string [closed] - python

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 3 years ago.
Improve this question
How would you turn a string that looks like this
7.11,8,9:00,9:15,14:30,15:00
into this dictionary entry
{7.11 : [8, (9:00, 9:15), (14:30, 15:00)]}?
Suppose that the number of time pairs (such as 9:00,9:15 and 14:30,15:00 is unknown and you want to have them all as tuple pairs.

First split the string at the commas, then zip cluster starting from the 3rd element and put it into a dictionary:
s = "7.11,8,9:00,9:15,14:30,15:00"
ss = s.split(',')
d = {ss[0]: [ss[1]] + list(zip(*[iter(ss[2:])]*2))}
Output:
{'7.11': ['8', ('9:00', '9:15'), ('14:30', '15:00')]}
If you need to convert it from string to appropiate data types (you'll have to adapt it according to your needs), then after getting the ss list:
time_list = [datetime.datetime.strptime(t,'%H:%M').time() for t in ss[2:]]
d = {float(ss[0]): [int(ss[1])] + list(zip(*[iter(time_list)]*2))}

Related

Remove first 5 from numbers in a list? [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 last month.
Improve this question
I have a list with numbers like this :-
s = [5542, 5654, 7545]
The goal is to remove the first 5 from the number such that the resultant list is like this
s = [542, 654, 745]
What's the best way to achieve the following without using any external libraries?
Try this with str.replace(old, new, count) -
[int(str(i).replace('5','',1)) for i in s]
[542, 654, 745]
The str.replace(old, new, count) in this case has 3 parameters, where the count set to 1 will only replace the first instance of 5 it finds in the string.
Then you can convert it back to an integer.
Another solution:
s = [int(str(x)[:str(x).index("5")] + str(x)[str(x).index("5") + 1:]) for x in s]

How to count frequency of multiple items in a list and print relative frequencies [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
Given two lists, I need to count the frequency of the items in one list as they are found in the other list; and place the relative frequencies of each item inside frequencyList (where the
frequency of searchFor[0] is stored in frequencyList[0])
I am unable to import anything
textList=['a','b','a','c',...]
searchFor=['a','b']
frequencyList=[2,1]
Try:
[textList.count(i) for i in searchFor]
Or?
list(map(textList.count, searchFor))
The other answer is quite compact and very pythonic but this is an alternate solution that is slightly more efficient as it only requires one pass over the input list.
textList=['a','b','a','c']
output_dict = {}
for i in textList:
try:
output_dict[i] = d[i] + 1
except:
output_dict[i] = 1
print(output_dict['a'])

Extraction of data from a delimited string in Python [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
I have a string variable which has some data as shown below:
'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
The data basically has two columns 'From' and 'To'. How do I extract the entries from the 'To' column in python?
You can use split, and then extract the data from the odd indexes, like so:
data = 'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
print(data)
data = data.split()
to = [data[i] for i in range(3, len(data), 2)]
print(to)
In python you could split a string at specific chars, in your case \n delimits the row and \t delimits the column
something like this should work:
string='From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
f=[]
t=[]
for row in string.split("\n")[1:]:
fr,to=row.split("\t")
f.append(fr)
t.append(to)
print(f,t)

how to transform a flattened json data to a taxonomy string? [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 2 years ago.
Improve this question
I have a data like this, aka input data:
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
And I want to transform that to the taxonomy string like this, aka output data:
root a b
a a-aa
a-aa a-aa-aab a-aa-aaa
a-aa-aab a-aa-aab-aaba a-aa-aab-aabb
b b-ba
I think there is a recursive solution in this sample, but I don't know how to achive this target. If you happen to know the answer, please tell me, god bless you!
from collections import defaultdict
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
result = defaultdict(set)
for string in data:
parts = string.split('-')
for i in range(len(parts)):
key = '-'.join(parts[:i])
val = '-'.join(parts[:i+1])
result[key].add(val)
print(result)
for prefix, children in result.items():
print(prefix or 'root', *children)

Return part of string between specific character pair, in string with multiple character pairs [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 3 years ago.
Improve this question
Text file or string:
SomeText1/SomeText2/SomeText3/SomeText4/SomeText5
#What I am looking for:
split_func(3, "/")
>>> SomeText3
Try:
s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5"
# s.split("/") returns a list of strings, split at the "/"
# I.e. ["SomeText1", "SomeText2", "SomeText3", "SomeText4", "SomeText5"]
# Then take the second element (remembering that the count starts at 0
result = s.split("/")[2]

Categories