Rearranging elements in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Rearranging elements in Python (/thread-14271.html) Pages:
1
2
|
Rearranging elements in Python - Nirmal - Nov-22-2018 i am new to Python and i cant get this.I have a List and i want to take the input from there and write those in files . p = ['Eth1/1', 'Eth1/5','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/2', 'Eth101/1/3','Eth102/1/1', 'Eth102/1/2', 'Eth102/1/3','Eth103/1/1', 'Eth103/1/2', 'Eth103/1/3','Eth103/1/4','Eth104/1/1', 'Eth104/1/2', 'Eth104/1/3','Eth104/1/4']What i am trying : with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2: for i in p: if len(i.partition("/")[0]) == 4: fw1.write('int ' + i + '\n mode\n') else: i = 0 while i < len(p): start = p[i].split('/') if (start[0] == 'Eth101'): i += 3 key = start[0] i += 1 while i < len(p) and p[i].split('/')[0] == key: i += 1 end = p[i-1].split('/') fw2.write('confi ' + start[0] + '/' + start[1] + '-' + end[1] + '\n mode\n')What i am looking for : abc1.txt should have int Eth1/1 mode int Eth1/5 mode int Eth2/1 mode int Eth 2/4 modeabc2.txt should have : int Eth101/1/1-3 mode int Eth102/1/1-3 mode int Eth103/1/1-4 mode int Eth104/1/1-4 mode
Any Idea ? RE: Rearranging elements in Python - wavic - Nov-22-2018 You should get an error because of line 2. The indentation is wrong. with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2: for element in p: if len(element.strip('Eth').split('/')[0]) == 1: # if you have one digit before the first / # write to abc1.txt else: # if you a sure that the input is correct else should work if you have three digits before / # write to abc2.txtOr you can use regular expressions. I am not good at it. Never needed it so far :D RE: Rearranging elements in Python - Nirmal - Nov-22-2018 Hey , i tried below p = ['Eth1/1', 'Eth1/48','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/16', 'Eth101/1/3','Eth102/1/1', 'Eth102/1/2', 'Eth102/1/3','Eth103/1/1', 'Eth103/1/2', 'Eth103/1/3','Eth103/1/4','Eth104/1/1', 'Eth104/1/2', 'Eth104/1/3','Eth104/1/4'] with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2: for element in p: if len(element.strip('Eth').split('/')[0]) == 1: # if you have one digit before the first / fw1.write('int ' + i + '\n mode\n') # write to abc1.txt else: # if you a sure that the input is correct else should work if you have three digits before / # write to abc2.txt fw2.write('int ' + i + '\n mode\n')both the file abc1.txt and abc2.txt contains int Eth104/1/4 mode int Eth104/1/4 mode int Eth104/1/4 mode int Eth104/1/4 mode RE: Rearranging elements in Python - wavic - Nov-22-2018 Are you sure that the files are not empty? This is what I am trying - Python 3.5.2: In [1]: cd tmp/ /home/victor/tmp In [2]: p = ['Eth1/1', 'Eth1/5','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/2', 'Eth101/1/3','Eth102/1/1', 'Eth102/1/2', 'Eth102/1/3','Eth103/1/1', 'Eth103/1/2', 'Eth103/1/3','Eth103/1/4','Eth104/1/1', 'Eth104/1/ ...: 2', 'Eth104/1/3','Eth104/1/4'] In [3]: with open('abc1.txt', 'w+') as fw1, open('abc2.txt', 'w+') as fw2: ...: for element in p: ...: if len(element.strip('Eth').split('/')[0]) == 1: ...: fw1.write('int {}\n mode\n'.format(element)) ...: else: ...: fw2.write('int {}\n mode\n'.format(element)) ...: In [4]:This is what is in the files: In [4]: cat abc1.txt int Eth1/1 mode int Eth1/5 mode int Eth2/1 mode int Eth2/4 mode In [5]: cat abc2.txt int Eth101/1/1 mode int Eth101/1/2 mode int Eth101/1/3 mode int Eth102/1/1 mode int Eth102/1/2 mode int Eth102/1/3 mode int Eth103/1/1 mode int Eth103/1/2 mode int Eth103/1/3 mode int Eth103/1/4 mode int Eth104/1/1 mode int Eth104/1/2 mode int Eth104/1/3 mode int Eth104/1/4 modeI don't know how you determine the ranges. RE: Rearranging elements in Python - Nirmal - Nov-22-2018 This one i am also getting but i need to define the range in abc2.txt 101/1/1-3 102/1/1-3 103/1/1-4 104/1/1-4 RE: Rearranging elements in Python - wavic - Nov-22-2018 Any rules? RE: Rearranging elements in Python - Nirmal - Nov-22-2018 i have a script and i think we can achieve the objective but i am missing somewhere ? with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2: for i in p: if len(i.partition("/")[0]) == 4: fw1.write('int ' + i + '\n mode\n') else: i = 0 while i < len(p): start = p[i].split('/') print start if (start[0] == 'Eth1'): i += 3 else: key = start[0] #print key i += 1 while i < len(p) and p[i].split('/')[0] == key: i += 1 end = p[i-1].split('/') fw2.write('confi ' + start[0] + '/' + start[1] + '-' + end[1] + '\n mode\n') RE: Rearranging elements in Python - Gribouillis - Nov-22-2018 Itertools can help for this task import io import itertools as itt def make_ranges(ints): ii = iter(sorted(ints)) a = b = next(ii) for k in ii: if k == b + 1: b = k else: yield a, b a = b = k yield a, b p = ['Eth1/1', 'Eth1/48','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/16', 'Eth101/1/3','Eth102/1/1', 'Eth102/1/2', 'Eth102/1/3','Eth103/1/1', 'Eth103/1/2', 'Eth103/1/3','Eth103/1/4','Eth104/1/1', 'Eth104/1/2', 'Eth104/1/3','Eth104/1/4'] p = [x.split('/') for x in p] p = [(x[0],) + tuple(int(y) for y in x[1:]) for x in p] q, r = [sorted(g) for k, g in itt.groupby(sorted(p, key=len), key=len)] fw1 = io.StringIO() for rec in q: print('/'.join(str(x) for x in rec), file=fw1) fw2 = io.StringIO() for k, g in itt.groupby(r, key=lambda x: x[:-1]): for a, b in make_ranges(int(x[-1]) for x in g): kk = k + (a if a == b else '{}-{}'.format(a, b),) print('/'.join(str(x) for x in kk), file=fw2) print(fw1.getvalue()) print(fw2.getvalue())
RE: Rearranging elements in Python - Nirmal - Nov-23-2018 its saying "invalid Syntax " -- print('/'.join(str(x) for x in rec), file=fw1) RE: Rearranging elements in Python - Gribouillis - Nov-23-2018 Nirmal Wrote:its saying "invalid Syntax " --When I run the exact code I posted above with python 3.5, it runs without error. I now always write python 3 code. Are you still using python 2.7? In that case, add this as the very top of your script # -*-coding: utf8-*- from __future__ import (absolute_import, division, print_function, unicode_literals) |