Since you want to iterate in an unusual way, a generator is a good way to abstract that:
def chunks(s, n):
"""Produce `n`-character chunks from `s`."""
for start in range(0, len(s), n):
yield s[start:start+n]
nums = "1.012345e0070.123414e-004-0.1234567891.21423"
for chunk in chunks(nums, 12):
print chunk
produces:
1.012345e007
0.123414e-00
4-0.12345678
91.21423
(which doesn't look right, but those are the 12-char chunks)
Answer from Ned Batchelder on Stack OverflowSince you want to iterate in an unusual way, a generator is a good way to abstract that:
def chunks(s, n):
"""Produce `n`-character chunks from `s`."""
for start in range(0, len(s), n):
yield s[start:start+n]
nums = "1.012345e0070.123414e-004-0.1234567891.21423"
for chunk in chunks(nums, 12):
print chunk
produces:
1.012345e007
0.123414e-00
4-0.12345678
91.21423
(which doesn't look right, but those are the 12-char chunks)
You're looking for string slicing.
>>> x = "1.012345e0070.123414e-004-0.1234567891.21423"
>>> x[2:10]
'012345e0'
How can I split a string in Python after a specific character count? - Stack Overflow
Example of Character Count in a String
How does `split()` work?
Split Words and Count Characters at Each Position
You are looking for slicing:
MyText1, MyText2 = MyText[:max_char], MyText[max_char:]
Python strings are sequences, to select the first max_char characters, simply use a slice to select those, and for the second half select everything starting at max_char until the end.
This is covered in the Python tutorial as well.
Just to improve on your final solution: you can use string.find(' ', n) to find the index of the first space after character n. If you want to split after that space (so that string1 ends with a space, rather than string2 beginning with one), just add one to it:
>>> print string
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ullamcorper, eros sed porta dapibus, nunc nibh iaculis tortor, in rhoncus quam orci sed ante. Sed
>>> space_location = string.find(' ', 36)+1
>>> print string[:space_location]
Lorem ipsum dolor sit amet, consectetur
>>> print string[space_location:]
adipiscing elit. Sed ullamcorper, eros sed porta dapibus, nunc nibh iaculis tortor, in rhoncus quam orci sed ante. Sed
I have a list of 5 letter words (Wordle) and I'm trying to find the most frequent character at each of the 5 positions.
The goal is to be able to see at the 5th position, T is the most frequent letter with 40 occurrences, R is next with 38, etc. Ideally the output would be something like the example below that I manually did in Excel.
I want to learn how to do this programmatically but I'm not sure how to get started. Would I use pandas to import and manipulate the data and then count it somehow? I currently have this in an excel or csv file.
If you can point me in the right direction I would appreciate it. Thank you!
Letter First Second Third Fourth Fifth A 44 81 85 45 19 B 45 3 12 4 3 C 54 10 14 31 11 D 21 3 20 15 32 E 20 52 48 77 120 F 35 0 5 8 7 G 28 7 18 23 9 H 23 48 4 8 30 I 13 41 62 42 3 J 4 1 1 0 0 K 8 3 5 17 30 L 22 60 22 39 36 M 32 10 11 22 10 N 8 32 38 44 22 O 5 83 76 30 21 P 32 14 19 11 17 Q 6 3 0 0 0 R 23 64 42 50 53 S 84 5 14 42 9 T 43 23 27 41 75 U 14 35 40 22 2 V 10 3 12 14 0 W 22 10 6 5 6 X 0 6 2 2 2 Y 3 3 14 1 81 Z 1 0 3 7 2
NEVER DO THIS.
Okay, now that we have that out of the way, here's how you do that.
stringvar = "one;two;three;four"
lst = stringvar.split(";")
stringcount = len(lst)
for idx, value in enumerate(lst):
globals()["string"+str(idx+1)] = value
# This is the ugliest code I've ever had to write
# please never do this. Please never ever do this.
globals() returns a dictionary containing every variable in the global scope with the variable name as a string for keys and the value as, well, values.
For instance:
>>> foo = "bar"
>>> baz = "eggs"
>>> spam = "have fun stormin' the castle"
>>> globals()
{'baz': 'eggs', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__builtins__': <module 'builtins' (built-in)>, 'foo': 'bar', '__doc__': None, '__package__': None, 'spam': "have fun stormin' the castle", '__name__': '__main__'}
You can reference this dictionary to add new variables by string name (globals()['a'] = 'b' sets variable a equal to "b"), but this is generally a terrible thing to do. Think of how you could possibly USE this data! You'd have to bind the new variable name to ANOTHER variable, then use that inside globals()[NEW_VARIABLE] every time! Let's use a list instead, shall we?
stringvar_list= stringvar.split(';')
string_count = len(stringvar_list)
stringvar_list would then have the individual strings