Extract every char from each chunk if index modulus 7 == 0

136 views Asked by At

Ok to I have a list of random letters and numbers that I have spit at ever period so it looks like this

text = "adkisnckindlikenskdihjgkiskdik.adkisnckindlikenskdihjgkiskdik.adkisnckindlikenskdihjgkiskdikadkisnckindlikenskdihjgkiskdi."

lines = text.split(".")
for stingssplit in lines:
        print(stingssplit)

Now I need to take it's out put:

adkisnckindlikenskdihjgkiskdik
adkisnckindlikenskdihjgkiskdik
adkisnckindlikenskdihjgkiskdik
adkisnckindlikenskdihjgkiskdik

and Extract every char from each chunk if index modulus 7 == 0

I have no idea what to do?

2

There are 2 answers

1
ettanany On BEST ANSWER

Try the following:

text = "adkisnckindlikenskdihjgkiskdik.adkisnckindlikenskdihjgkiskdik.adkisnckindlikenskdihjgkiskdikadkisnckindlikenskdihjgkiskdi."

chunks = text.split(".")

for chunk in chunks:
    print ''.join([v for i, v in enumerate(chunk) if i % 7 == 0])

Output:

>>> for chunk in chunks:
...     print ''.join([v for i, v in enumerate(chunk) if i % 7 == 0])
...
akeji
akeji
akejiniik

>>>

Or in one line:

>>> [''.join([v for i, v in enumerate(chunk) if i % 7 == 0]) for chunk in chunks]
['akeji', 'akeji', 'akejiniik', '']
0
Psidom On

You can simply index it:

"adkisnckindlikenskdihjgkiskdik"[::7]
# 'akeji'                 # gives you characters at positions 0, 7, 14, 21, 28

[line[::7] for line in text.split(".")]
# ['akeji', 'akeji', 'akejiniik', '']