![]() |
|
[Tut] Python | Split String and Count Results - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String and Count Results (/thread-100292.html) |
[Tut] Python | Split String and Count Results - xSicKxBot - 11-26-2022 Python | Split String and Count Results Rate this post Summary: Split the string using split and then use len to count the results.Minimal Example Code: print("Result Count: ", len('one,two,three'.split(',')))
# Result Count: 3Problem Formulation Problem: Given a string. How will you split the string and find the number of split strings? Can you store the split strings into different variables?[b]Example[/b] Code: # Given String
text = '366NX-BQ62X-PQT9G-GPX4H-VT7TX'
# Expected Output:
Number of split strings: 5
key_1 = 366NX
key_2 = BQ62X
key_3 = PQT9G
key_4 = GPX4H
key_5 = VT7TXIn the above problem, the delimiter used to split the string is “-“. After splitting five substrings can be extracted. Therefore, you need five variables to store the five substrings. Can you solve it? [b]Solution[/b] Splitting the string and counting the number of results is a cakewalk. All you have to do is split the string using the Code: split()Code: lenCode: Code: text = '366NX-BQ62X-PQT9G-GPX4H-VT7TX'
# splitting the string using - as separator
res = text.split('-')
# length of split string list
x = len(res)
print("Number of split strings: ", x)![]()
Code: Code: text = '366NX-BQ62X-PQT9G-GPX4H-VT7TX'
# splitting the string using - as separator
res = text.split('-')
# length of split string list
x = len(res) # Naming and storing variables and values
name = []
for i in range(1, x+1): name.append('key_'+str(i)) d = dict(zip(name, res))
for key, value in d.items(): print(key, "=", value)Almost all modules have a special attribute known as Code: __dict__So, you can create a class and then go ahead create an instance of this class which can be used to set different attributes. Once you split the given string and also create the list containing the variable names (as done in the previous solution), you can go ahead and zip the two lists and use the Code: setattr()Code: __dict__Code: object_name.__dict__Code: Code: text = '366NX-BQ62X-PQT9G-GPX4H-VT7TX'
# splitting the string using - as separator
res = text.split('-')
# length of split string list
x = len(res)
print("Number of split strings: ", x) # variable creation and value assignment
name = []
for i in range(1, x + 1): name.append('key_' + str(i)) class Record(): pass r = Record() for name, value in zip(name, res): setattr(r, name, value)
print(r.__dict__) for key, value in r.__dict__.items(): print(key, "=", value)Caution: This solution is not recommended unless this is the only option left. I have mentioned this just because it solves the purpose. However, it is certainly not the best way to approach the given problem. Code: Code: text = '366NX-BQ62X-PQT9G-GPX4H-VT7TX'
# splitting the string using - as separator
res = text.split('-')
# length of split string list
x = len(res)
print("Number of split strings: ", x)
name = []
for i in range(1, x + 1): name.append('key_' + str(i)) for idx, value in enumerate(res): globals()["key_" + str(idx + 1)] = value
print(globals())
x = 0
for i in reversed(globals()): print(i, "=", globals()[i]) x = x+1 if x == 5: breakExplanation: Code: globals()Code: globals()['a'] = 'b'Code: aCode: "b"Since global returns a dictionary containing all the variables in the global scope, a workaround to get only the variables we assigned is to extract the last “N” key-value pairs from this dictionary where “N” is the length of the split string list. Conclusion I hope the solutions mentioned in this tutorial have helped you. Please stay tuned and subscribe for more interesting reads and solutions in the future. Happy coding! |