1

I ideally want to turn this 100020630 into [100,020,630] but so far i can only do this "100.020.630" into ["100","020","630"]

def fulltotriple(x):
    X=x.split(".")
    return X

print(fulltotriple("192.123.010"))

for some additionnal info my goal is no turn ip adresses into bin adresses using this as a first step =)

edit: i have not found any way of getting the list WITHOUT the " " in the list on stack overflow

Profy
  • 111
  • 15
  • 2
    Possible duplicate of [Split string every nth character?](https://stackoverflow.com/questions/9475241/split-string-every-nth-character) – R4444 May 07 '19 at 14:37

7 Answers7

4

Here's one approach using a list comprehension:

s = '100020630'
[s[i:i + 3] for i in range(0, len(s), 3)]
# ['100', '020', '630']
yatu
  • 86,083
  • 12
  • 84
  • 139
4

You could use the built-in wrap function:

In [3]: s = "100020630"                                                                                                                                                                                          
In [4]: import textwrap                                                                                                                                                                                                                                                                                                                                                                                
In [6]: textwrap.wrap(s, 3)                                                                                                                                                                                       
Out[6]: ['100', '020', '630']

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

If you want a list of ints:

[int(num) for num in textwrap.wrap(s, 3)]

Outputs:

[100, 020, 630]
AdamGold
  • 4,941
  • 4
  • 29
  • 47
4

If you want to handle IP addresses, you are doing it totally wrong.

IP address is a 24-binary digit number, not a 9-decimal digit. It is splitted for 4 sub-blocks, like: 192.168.0.1. BUT. In decimal view they all can be 3-digit, or 2-digit, or any else combination. I recommend you to use ipaddress standard module:

import ipaddress

a = '192.168.0.1'

ip = ipaddress.ip_address(a)
ip.packed

will return you the packed binary format:

b'\xc0\xa8\x00\x01'

If you want to convert your IPv4 to binary format, you can use this command:

''.join(bin(i)[2:] for i in ip.packed)

It will return you this string:

'110000001010100001'

vurmux
  • 9,420
  • 3
  • 25
  • 45
3

Use regex to find all matches of triplets \d{3}

import re

str = "100020630"

def fulltotriple(x):
    pattern = re.compile(r"\d{3}")
    return [int(found_match) for found_match in pattern.findall(x)]


print(fulltotriple(str))

Outputting:

[100, 20, 630]
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
3

You could use wrap which is a inbuilt function in python

from textwrap import wrap

def fulltotriple(x):
    x = wrap(x, 3)
    return x

print(fulltotriple("100020630"))

Outputs:

['100', '020', '630']
Hass786123
  • 666
  • 2
  • 7
  • 16
3

You can use python built-ins for this:

text = '100020630'

# using wrap

from textwrap import wrap
wrap(text, 3)
>>> ['100', '020', '630']


# using map/zip

map(''.join, zip(*[iter(text)]*3))
>>> ['100', '020', '630']
Mikey Lockwood
  • 1,258
  • 1
  • 9
  • 22
1
def fulltotriple(data):
    result = []
    for i in range(0, len(data), 3):
        result.append(int(data[i:i + 3]))
    return (result)


print(fulltotriple("192123010"))

output:

[192, 123, 10]
ncica
  • 7,015
  • 1
  • 15
  • 37