How do I extend one list to the length of another?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?How to append something to an array?How to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?
GitLab account hacked and repo wiped
Given a safe domain, are subdirectories safe as well?
Has the United States ever had a non-Christian President?
What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?
In "Avengers: Endgame", what does this name refer to?
Why doesn't a particle exert force on itself?
How can I obtain and work with a Platonic dodecahedron?
How do I, as a DM, handle a party that decides to set up an ambush in a dungeon?
How to use awk to extract data from a file based on the content of another file?
How important are good looking people in a novel/story?
How to replace space with '+' symbol in a triangular array?
Can I combine SELECT TOP() with the IN operator?
Why are condenser mics so much more expensive than dynamics?
Antivirus for Ubuntu 18.04
Emergency stop in plain TeX, pdfTeX, XeTeX and LuaTeX?
Make me a minimum magic sum
What happens if I accidentally leave an app running and click "Install Now" in Software Updater?
Referring to person by surname, keep or omit "von"?
What is more safe for browsing the web: PC or smartphone?
All of my Firefox add-ons been disabled suddenly, how can I re-enable them?
Installing Debian 10, upgrade to stable later?
What does the coin flipping before dying mean?
Why can't argument be forwarded inside lambda without mutable?
Guess the number game (Python)
How do I extend one list to the length of another?
How do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?How to append something to an array?How to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Here is my code:
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
x = L3[0:difference]
L3.extend(x)
elif difference < 0:
x = L4[0:difference]
L4.extend(x)
L4 and L3 are two separate lists, and I want them to be the same length. I want list L3 to extend to the size of L4 if it is smaller, and vice versa.
Example One Input:
0;NATE;NATHAN #NATE is L3, NATHAN IS L4
Example One Output:
[78, 65, 84, 69, 78, 65] #L3
[78, 65, 84, 72, 65, 78] #L4
*Here, list L3 extends to the length of list L4.
Example Two Input:
0;NAT;DNADNANNFNDFGDFGFGF
Example Two Output:
[78, 65, 84, 78, 65, 84]
[68, 78, 65, 68, 78, 65, 78, 78, 70, 78, 68, 70, 71, 68, 70, 71, 70, 71, 70]
After testing my code multiple times, it appears that L3, the first line of outputted code, will iterate twice before coming to a stop, so if L4 is incredibly long, L3 will not extend to the same length. How do I resolve this?
python list append extend
add a comment |
Here is my code:
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
x = L3[0:difference]
L3.extend(x)
elif difference < 0:
x = L4[0:difference]
L4.extend(x)
L4 and L3 are two separate lists, and I want them to be the same length. I want list L3 to extend to the size of L4 if it is smaller, and vice versa.
Example One Input:
0;NATE;NATHAN #NATE is L3, NATHAN IS L4
Example One Output:
[78, 65, 84, 69, 78, 65] #L3
[78, 65, 84, 72, 65, 78] #L4
*Here, list L3 extends to the length of list L4.
Example Two Input:
0;NAT;DNADNANNFNDFGDFGFGF
Example Two Output:
[78, 65, 84, 78, 65, 84]
[68, 78, 65, 68, 78, 65, 78, 78, 70, 78, 68, 70, 71, 68, 70, 71, 70, 71, 70]
After testing my code multiple times, it appears that L3, the first line of outputted code, will iterate twice before coming to a stop, so if L4 is incredibly long, L3 will not extend to the same length. How do I resolve this?
python list append extend
add a comment |
Here is my code:
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
x = L3[0:difference]
L3.extend(x)
elif difference < 0:
x = L4[0:difference]
L4.extend(x)
L4 and L3 are two separate lists, and I want them to be the same length. I want list L3 to extend to the size of L4 if it is smaller, and vice versa.
Example One Input:
0;NATE;NATHAN #NATE is L3, NATHAN IS L4
Example One Output:
[78, 65, 84, 69, 78, 65] #L3
[78, 65, 84, 72, 65, 78] #L4
*Here, list L3 extends to the length of list L4.
Example Two Input:
0;NAT;DNADNANNFNDFGDFGFGF
Example Two Output:
[78, 65, 84, 78, 65, 84]
[68, 78, 65, 68, 78, 65, 78, 78, 70, 78, 68, 70, 71, 68, 70, 71, 70, 71, 70]
After testing my code multiple times, it appears that L3, the first line of outputted code, will iterate twice before coming to a stop, so if L4 is incredibly long, L3 will not extend to the same length. How do I resolve this?
python list append extend
Here is my code:
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
x = L3[0:difference]
L3.extend(x)
elif difference < 0:
x = L4[0:difference]
L4.extend(x)
L4 and L3 are two separate lists, and I want them to be the same length. I want list L3 to extend to the size of L4 if it is smaller, and vice versa.
Example One Input:
0;NATE;NATHAN #NATE is L3, NATHAN IS L4
Example One Output:
[78, 65, 84, 69, 78, 65] #L3
[78, 65, 84, 72, 65, 78] #L4
*Here, list L3 extends to the length of list L4.
Example Two Input:
0;NAT;DNADNANNFNDFGDFGFGF
Example Two Output:
[78, 65, 84, 78, 65, 84]
[68, 78, 65, 68, 78, 65, 78, 78, 70, 78, 68, 70, 71, 68, 70, 71, 70, 71, 70]
After testing my code multiple times, it appears that L3, the first line of outputted code, will iterate twice before coming to a stop, so if L4 is incredibly long, L3 will not extend to the same length. How do I resolve this?
python list append extend
python list append extend
edited Mar 24 at 4:53
Siong Thye Goh
1,81721016
1,81721016
asked Mar 23 at 4:19
Nathan Nathan
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use cycle and islice from the itertools library.
from itertools import cycle, islice
def process(shorter, aim):
#shorter is a lit of int
#aim is the targeted len
return list(islice(cycle(shorter), aim))
L3 = [ord(i) for i in 'NATE' ]
L4 = [ord(i) for i in 'NATHAN']
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
print(process(L3, len(L4)), L4)
elif difference < 0:
print(L3, process(L4, len(L3)))
produces
[78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
If you want a more primitive solution without calling function. You might like to compute how many times you have to repeat the list and also compute the remaining part explicitly.
add a comment |
If I do not change your code too much you could try something like:
Solution 1
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
num_times, remainder = divmod(len(L4), len(L3))
x = L3 * num_times + L3[:remainder]
L3 = x
elif difference < 0:
num_times, remainder = divmod(len(L3), len(L4))
x = L4 * num_times + L4[:remainder]
L4 = x
L3 = list(map(ord, L3))
L4 = list(map(ord, L4))
print(L3, 'n', L4, sep='')
But if we want to simplify, we could have something like that:
Solution 2
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
size_of_bigger = max(len(L3), len(L4))
num_times, remainder = divmod(size_of_bigger, len(L3))
L3 = L3 * num_times + L3[:remainder]
num_times, remainder = divmod(size_of_bigger, len(L4))
L4 = L4 * num_times + L4[:remainder]
output = "n".join(str(list(map(ord, x))) for x in [L3, L4])
print(output)
If we push the refactoring a little bit too far:
Solution 3
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
input_lists = [L3, L4]
size_of_bigger = max(map(len, input_lists))
def output_lists(input_lists):
for current_list in input_lists:
num_times, remainder = divmod(size_of_bigger, len(current_list))
yield current_list * num_times + current_list[:remainder]
output = "n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
print(output)
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%2f55310579%2fhow-do-i-extend-one-list-to-the-length-of-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use cycle and islice from the itertools library.
from itertools import cycle, islice
def process(shorter, aim):
#shorter is a lit of int
#aim is the targeted len
return list(islice(cycle(shorter), aim))
L3 = [ord(i) for i in 'NATE' ]
L4 = [ord(i) for i in 'NATHAN']
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
print(process(L3, len(L4)), L4)
elif difference < 0:
print(L3, process(L4, len(L3)))
produces
[78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
If you want a more primitive solution without calling function. You might like to compute how many times you have to repeat the list and also compute the remaining part explicitly.
add a comment |
You can use cycle and islice from the itertools library.
from itertools import cycle, islice
def process(shorter, aim):
#shorter is a lit of int
#aim is the targeted len
return list(islice(cycle(shorter), aim))
L3 = [ord(i) for i in 'NATE' ]
L4 = [ord(i) for i in 'NATHAN']
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
print(process(L3, len(L4)), L4)
elif difference < 0:
print(L3, process(L4, len(L3)))
produces
[78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
If you want a more primitive solution without calling function. You might like to compute how many times you have to repeat the list and also compute the remaining part explicitly.
add a comment |
You can use cycle and islice from the itertools library.
from itertools import cycle, islice
def process(shorter, aim):
#shorter is a lit of int
#aim is the targeted len
return list(islice(cycle(shorter), aim))
L3 = [ord(i) for i in 'NATE' ]
L4 = [ord(i) for i in 'NATHAN']
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
print(process(L3, len(L4)), L4)
elif difference < 0:
print(L3, process(L4, len(L3)))
produces
[78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
If you want a more primitive solution without calling function. You might like to compute how many times you have to repeat the list and also compute the remaining part explicitly.
You can use cycle and islice from the itertools library.
from itertools import cycle, islice
def process(shorter, aim):
#shorter is a lit of int
#aim is the targeted len
return list(islice(cycle(shorter), aim))
L3 = [ord(i) for i in 'NATE' ]
L4 = [ord(i) for i in 'NATHAN']
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
print(process(L3, len(L4)), L4)
elif difference < 0:
print(L3, process(L4, len(L3)))
produces
[78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
If you want a more primitive solution without calling function. You might like to compute how many times you have to repeat the list and also compute the remaining part explicitly.
answered Mar 24 at 4:05
Siong Thye GohSiong Thye Goh
1,81721016
1,81721016
add a comment |
add a comment |
If I do not change your code too much you could try something like:
Solution 1
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
num_times, remainder = divmod(len(L4), len(L3))
x = L3 * num_times + L3[:remainder]
L3 = x
elif difference < 0:
num_times, remainder = divmod(len(L3), len(L4))
x = L4 * num_times + L4[:remainder]
L4 = x
L3 = list(map(ord, L3))
L4 = list(map(ord, L4))
print(L3, 'n', L4, sep='')
But if we want to simplify, we could have something like that:
Solution 2
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
size_of_bigger = max(len(L3), len(L4))
num_times, remainder = divmod(size_of_bigger, len(L3))
L3 = L3 * num_times + L3[:remainder]
num_times, remainder = divmod(size_of_bigger, len(L4))
L4 = L4 * num_times + L4[:remainder]
output = "n".join(str(list(map(ord, x))) for x in [L3, L4])
print(output)
If we push the refactoring a little bit too far:
Solution 3
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
input_lists = [L3, L4]
size_of_bigger = max(map(len, input_lists))
def output_lists(input_lists):
for current_list in input_lists:
num_times, remainder = divmod(size_of_bigger, len(current_list))
yield current_list * num_times + current_list[:remainder]
output = "n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
print(output)
add a comment |
If I do not change your code too much you could try something like:
Solution 1
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
num_times, remainder = divmod(len(L4), len(L3))
x = L3 * num_times + L3[:remainder]
L3 = x
elif difference < 0:
num_times, remainder = divmod(len(L3), len(L4))
x = L4 * num_times + L4[:remainder]
L4 = x
L3 = list(map(ord, L3))
L4 = list(map(ord, L4))
print(L3, 'n', L4, sep='')
But if we want to simplify, we could have something like that:
Solution 2
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
size_of_bigger = max(len(L3), len(L4))
num_times, remainder = divmod(size_of_bigger, len(L3))
L3 = L3 * num_times + L3[:remainder]
num_times, remainder = divmod(size_of_bigger, len(L4))
L4 = L4 * num_times + L4[:remainder]
output = "n".join(str(list(map(ord, x))) for x in [L3, L4])
print(output)
If we push the refactoring a little bit too far:
Solution 3
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
input_lists = [L3, L4]
size_of_bigger = max(map(len, input_lists))
def output_lists(input_lists):
for current_list in input_lists:
num_times, remainder = divmod(size_of_bigger, len(current_list))
yield current_list * num_times + current_list[:remainder]
output = "n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
print(output)
add a comment |
If I do not change your code too much you could try something like:
Solution 1
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
num_times, remainder = divmod(len(L4), len(L3))
x = L3 * num_times + L3[:remainder]
L3 = x
elif difference < 0:
num_times, remainder = divmod(len(L3), len(L4))
x = L4 * num_times + L4[:remainder]
L4 = x
L3 = list(map(ord, L3))
L4 = list(map(ord, L4))
print(L3, 'n', L4, sep='')
But if we want to simplify, we could have something like that:
Solution 2
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
size_of_bigger = max(len(L3), len(L4))
num_times, remainder = divmod(size_of_bigger, len(L3))
L3 = L3 * num_times + L3[:remainder]
num_times, remainder = divmod(size_of_bigger, len(L4))
L4 = L4 * num_times + L4[:remainder]
output = "n".join(str(list(map(ord, x))) for x in [L3, L4])
print(output)
If we push the refactoring a little bit too far:
Solution 3
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
input_lists = [L3, L4]
size_of_bigger = max(map(len, input_lists))
def output_lists(input_lists):
for current_list in input_lists:
num_times, remainder = divmod(size_of_bigger, len(current_list))
yield current_list * num_times + current_list[:remainder]
output = "n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
print(output)
If I do not change your code too much you could try something like:
Solution 1
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
difference = len(L4)-len(L3)
if difference == 0:
pass
elif difference > 0:
num_times, remainder = divmod(len(L4), len(L3))
x = L3 * num_times + L3[:remainder]
L3 = x
elif difference < 0:
num_times, remainder = divmod(len(L3), len(L4))
x = L4 * num_times + L4[:remainder]
L4 = x
L3 = list(map(ord, L3))
L4 = list(map(ord, L4))
print(L3, 'n', L4, sep='')
But if we want to simplify, we could have something like that:
Solution 2
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
size_of_bigger = max(len(L3), len(L4))
num_times, remainder = divmod(size_of_bigger, len(L3))
L3 = L3 * num_times + L3[:remainder]
num_times, remainder = divmod(size_of_bigger, len(L4))
L4 = L4 * num_times + L4[:remainder]
output = "n".join(str(list(map(ord, x))) for x in [L3, L4])
print(output)
If we push the refactoring a little bit too far:
Solution 3
L3 = list('NAT')
L4 = list('DNADNANNFNDFGDFGFGF')
input_lists = [L3, L4]
size_of_bigger = max(map(len, input_lists))
def output_lists(input_lists):
for current_list in input_lists:
num_times, remainder = divmod(size_of_bigger, len(current_list))
yield current_list * num_times + current_list[:remainder]
output = "n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
print(output)
answered Mar 25 at 4:48
EvensFEvensF
682513
682513
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%2f55310579%2fhow-do-i-extend-one-list-to-the-length-of-another%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