Python Forum
Rearranging elements in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rearranging elements in Python
#1
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
  mode
abc2.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

  1. So any Eth having 1 digit before " / " ( e:g Eth1/1 or Eth2/2 )should be in one file that is abc1.txt .
  1. Any Eth having 3 digit before " / " ( e:g Eth101/1/1 or Eth 102/1/1 ) should be in another file that is abc2.txt and .As these are in ranges , need to write it like Eth101/1/1-3, Eth102/1/1-3 etc

Any Idea ?
Reply


Messages In This Thread
Rearranging elements in Python - by Nirmal - Nov-22-2018, 06:26 AM
RE: Rearranging elements in Python - by wavic - Nov-22-2018, 09:04 AM
RE: Rearranging elements in Python - by Nirmal - Nov-22-2018, 09:19 AM
RE: Rearranging elements in Python - by wavic - Nov-22-2018, 09:45 AM
RE: Rearranging elements in Python - by Nirmal - Nov-22-2018, 09:53 AM
RE: Rearranging elements in Python - by wavic - Nov-22-2018, 10:12 AM
RE: Rearranging elements in Python - by Nirmal - Nov-22-2018, 11:08 AM
RE: Rearranging elements in Python - by Gribouillis - Nov-22-2018, 12:29 PM
RE: Rearranging elements in Python - by Nirmal - Nov-23-2018, 03:28 AM
RE: Rearranging elements in Python - by Gribouillis - Nov-23-2018, 07:37 AM
RE: Rearranging elements in Python - by Nirmal - Nov-23-2018, 12:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 1,214 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,170 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,621 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  How does a set in python store the elements? idknuttin 5 2,801 Jul-10-2020, 10:46 PM
Last Post: Gribouillis
  How to sum up the elements of an integer using python split/join? mohanraj1986 5 3,541 Aug-27-2018, 09:13 AM
Last Post: perfringo
  Python show the combinations of list of elements zydjohn 6 7,530 Mar-06-2018, 11:23 PM
Last Post: zydjohn
  [split] Rearranging CSV columns and rows Ivan1 1 2,463 Aug-31-2017, 01:41 AM
Last Post: Ivan1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020