Python Forum
Rearranging elements in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rearranging elements in Python
#3
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
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,356 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,248 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,651 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  How does a set in python store the elements? idknuttin 5 2,840 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,577 Aug-27-2018, 09:13 AM
Last Post: perfringo
  Python show the combinations of list of elements zydjohn 6 7,561 Mar-06-2018, 11:23 PM
Last Post: zydjohn
  [split] Rearranging CSV columns and rows Ivan1 1 2,477 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