Converting into range in Python
I want to convert a list into range.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
what i am trying :
fp = open('mode.txt' , 'w+')
for i in a:
fp.write('confi ' + i + 'n mode n')
what i am looking for :
confi Eth1/1-5
mode
confi Eth1/6-10
mode
confi Eth2/1-6
mode
confi Eth3/1-6
mode
confi Eth4/1-6
mode
Any idea how to do this ?
list python-2.6
|
show 10 more comments
I want to convert a list into range.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
what i am trying :
fp = open('mode.txt' , 'w+')
for i in a:
fp.write('confi ' + i + 'n mode n')
what i am looking for :
confi Eth1/1-5
mode
confi Eth1/6-10
mode
confi Eth2/1-6
mode
confi Eth3/1-6
mode
confi Eth4/1-6
mode
Any idea how to do this ?
list python-2.6
1
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11
|
show 10 more comments
I want to convert a list into range.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
what i am trying :
fp = open('mode.txt' , 'w+')
for i in a:
fp.write('confi ' + i + 'n mode n')
what i am looking for :
confi Eth1/1-5
mode
confi Eth1/6-10
mode
confi Eth2/1-6
mode
confi Eth3/1-6
mode
confi Eth4/1-6
mode
Any idea how to do this ?
list python-2.6
I want to convert a list into range.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
what i am trying :
fp = open('mode.txt' , 'w+')
for i in a:
fp.write('confi ' + i + 'n mode n')
what i am looking for :
confi Eth1/1-5
mode
confi Eth1/6-10
mode
confi Eth2/1-6
mode
confi Eth3/1-6
mode
confi Eth4/1-6
mode
Any idea how to do this ?
list python-2.6
list python-2.6
edited Nov 15 '18 at 7:32
Jinni
asked Nov 13 '18 at 6:59
JinniJinni
166
166
1
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11
|
show 10 more comments
1
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11
1
1
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11
|
show 10 more comments
1 Answer
1
active
oldest
votes
You could create a loop that checks the current element as the start. If it starts with Eth1 then get the 4th element after as the end. Otherwise, keep the starting Eth_, iterate through the list until you get the last Eth_ element or until the list end. Assign the last element as the end.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
i = 0
while i < len(a):
start = a[i].split('/')
if (start[0] == 'Eth1'):
i += 5
else:
key = start[0]
i += 1
while i < len(a) and a[i].split('/')[0] == key:
i += 1
end = a[i-1].split('/')
print('confi ' + start[0] + '/' + start[1] + '-' + end[1] + 'n moden')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275471%2fconverting-into-range-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could create a loop that checks the current element as the start. If it starts with Eth1 then get the 4th element after as the end. Otherwise, keep the starting Eth_, iterate through the list until you get the last Eth_ element or until the list end. Assign the last element as the end.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
i = 0
while i < len(a):
start = a[i].split('/')
if (start[0] == 'Eth1'):
i += 5
else:
key = start[0]
i += 1
while i < len(a) and a[i].split('/')[0] == key:
i += 1
end = a[i-1].split('/')
print('confi ' + start[0] + '/' + start[1] + '-' + end[1] + 'n moden')
add a comment |
You could create a loop that checks the current element as the start. If it starts with Eth1 then get the 4th element after as the end. Otherwise, keep the starting Eth_, iterate through the list until you get the last Eth_ element or until the list end. Assign the last element as the end.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
i = 0
while i < len(a):
start = a[i].split('/')
if (start[0] == 'Eth1'):
i += 5
else:
key = start[0]
i += 1
while i < len(a) and a[i].split('/')[0] == key:
i += 1
end = a[i-1].split('/')
print('confi ' + start[0] + '/' + start[1] + '-' + end[1] + 'n moden')
add a comment |
You could create a loop that checks the current element as the start. If it starts with Eth1 then get the 4th element after as the end. Otherwise, keep the starting Eth_, iterate through the list until you get the last Eth_ element or until the list end. Assign the last element as the end.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
i = 0
while i < len(a):
start = a[i].split('/')
if (start[0] == 'Eth1'):
i += 5
else:
key = start[0]
i += 1
while i < len(a) and a[i].split('/')[0] == key:
i += 1
end = a[i-1].split('/')
print('confi ' + start[0] + '/' + start[1] + '-' + end[1] + 'n moden')
You could create a loop that checks the current element as the start. If it starts with Eth1 then get the 4th element after as the end. Otherwise, keep the starting Eth_, iterate through the list until you get the last Eth_ element or until the list end. Assign the last element as the end.
a = ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10','Eth2/1', 'Eth2/2', 'Eth2/3', 'Eth2/4', 'Eth2/5', 'Eth2/6','Eth3/1', 'Eth3/2', 'Eth3/3', 'Eth3/4', 'Eth3/5', 'Eth3/6','Eth4/1', 'Eth4/2', 'Eth4/3', 'Eth4/4', 'Eth4/5', 'Eth4/6']
i = 0
while i < len(a):
start = a[i].split('/')
if (start[0] == 'Eth1'):
i += 5
else:
key = start[0]
i += 1
while i < len(a) and a[i].split('/')[0] == key:
i += 1
end = a[i-1].split('/')
print('confi ' + start[0] + '/' + start[1] + '-' + end[1] + 'n moden')
edited Nov 16 '18 at 5:59
answered Nov 13 '18 at 7:13
AndreasAndreas
1,97631018
1,97631018
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275471%2fconverting-into-range-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
So, why does the range divided in 1-5 and 6-10? Any criteria for that?
– Andreas
Nov 13 '18 at 7:03
@Andreas looking for group of 5
– Jinni
Nov 13 '18 at 7:08
Let 'a" be like that ..
– Jinni
Nov 13 '18 at 7:15
@Andreas i updaed the question ..can we get the output i mentioned ? Logic :divide line in chunk of 5 where Eth1 exist and for line consist Eth2 , take everything ?
– Jinni
Nov 15 '18 at 6:24
@Andreas what if i have to do thing for Eth3 same to what we do for Eth2..
– Jinni
Nov 15 '18 at 7:11