How to combine a string list with a list of lists of integers
up vote
1
down vote
favorite
I would like to combine the following string list and integer list of lists:
lst = ['A',
'A',
'B',
'C',
'C',
'D',
'D',
'D',....]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30], ....]
such that a list of tuples is returned:
lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
('A', 54), ('A', 55), ('A', 56),
('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
('C', 98), ('C', 99), ('C', 100),
('D', 13), ('D', 14),
('D', 21), ('D', 22), ('D', 23),
('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]
The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst
.
I have tried the following, which only works for the first element of each sublist in lst_of_lst
and repeats for each string in lst
:
empty_test_combo =
for x in helix_chain_id:
for y in helix_seq_res_num_ranges:
empty_test_combo += (zip(x, y))
I have also tried:
lst_tups =
for x in lst:
for y in lst_of_lst:
for z in y:
lst_tups.append(zip(x, [z]))
This seems the most promising option. It returns a list of tuples that combines the lst
strings and lst_of_lst
integer lists correctly, but only partially.
python list zip tuples
add a comment |
up vote
1
down vote
favorite
I would like to combine the following string list and integer list of lists:
lst = ['A',
'A',
'B',
'C',
'C',
'D',
'D',
'D',....]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30], ....]
such that a list of tuples is returned:
lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
('A', 54), ('A', 55), ('A', 56),
('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
('C', 98), ('C', 99), ('C', 100),
('D', 13), ('D', 14),
('D', 21), ('D', 22), ('D', 23),
('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]
The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst
.
I have tried the following, which only works for the first element of each sublist in lst_of_lst
and repeats for each string in lst
:
empty_test_combo =
for x in helix_chain_id:
for y in helix_seq_res_num_ranges:
empty_test_combo += (zip(x, y))
I have also tried:
lst_tups =
for x in lst:
for y in lst_of_lst:
for z in y:
lst_tups.append(zip(x, [z]))
This seems the most promising option. It returns a list of tuples that combines the lst
strings and lst_of_lst
integer lists correctly, but only partially.
python list zip tuples
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would like to combine the following string list and integer list of lists:
lst = ['A',
'A',
'B',
'C',
'C',
'D',
'D',
'D',....]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30], ....]
such that a list of tuples is returned:
lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
('A', 54), ('A', 55), ('A', 56),
('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
('C', 98), ('C', 99), ('C', 100),
('D', 13), ('D', 14),
('D', 21), ('D', 22), ('D', 23),
('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]
The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst
.
I have tried the following, which only works for the first element of each sublist in lst_of_lst
and repeats for each string in lst
:
empty_test_combo =
for x in helix_chain_id:
for y in helix_seq_res_num_ranges:
empty_test_combo += (zip(x, y))
I have also tried:
lst_tups =
for x in lst:
for y in lst_of_lst:
for z in y:
lst_tups.append(zip(x, [z]))
This seems the most promising option. It returns a list of tuples that combines the lst
strings and lst_of_lst
integer lists correctly, but only partially.
python list zip tuples
I would like to combine the following string list and integer list of lists:
lst = ['A',
'A',
'B',
'C',
'C',
'D',
'D',
'D',....]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30], ....]
such that a list of tuples is returned:
lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
('A', 54), ('A', 55), ('A', 56),
('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
('C', 98), ('C', 99), ('C', 100),
('D', 13), ('D', 14),
('D', 21), ('D', 22), ('D', 23),
('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]
The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst
.
I have tried the following, which only works for the first element of each sublist in lst_of_lst
and repeats for each string in lst
:
empty_test_combo =
for x in helix_chain_id:
for y in helix_seq_res_num_ranges:
empty_test_combo += (zip(x, y))
I have also tried:
lst_tups =
for x in lst:
for y in lst_of_lst:
for z in y:
lst_tups.append(zip(x, [z]))
This seems the most promising option. It returns a list of tuples that combines the lst
strings and lst_of_lst
integer lists correctly, but only partially.
python list zip tuples
python list zip tuples
edited Nov 11 at 17:36
Thierry Lathuille
7,12182630
7,12182630
asked Nov 11 at 14:57
Alex M
438
438
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It seems that there is a misunderstanding in the way you try to use zip
.
zip(list1, list2)
is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1
and the first item of list2
, and so on.
What you want to do here is to zip(lst, lst_of_lst)
in order to pair each element of lst
to the corresponding sublist of lst_of_lst
. From each pair, you can generate the output you want.
You can do that with a list comprehension:
lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30],]
out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]
print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
# ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
# ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
# ('D', 27), ('D', 28), ('D', 29), ('D', 30)]
Or, written with loops, as you tried:
out =
for item1, sublist in zip(lst, lst_of_lst):
for item2 in sublist:
out.append((item1, item2))
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It seems that there is a misunderstanding in the way you try to use zip
.
zip(list1, list2)
is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1
and the first item of list2
, and so on.
What you want to do here is to zip(lst, lst_of_lst)
in order to pair each element of lst
to the corresponding sublist of lst_of_lst
. From each pair, you can generate the output you want.
You can do that with a list comprehension:
lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30],]
out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]
print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
# ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
# ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
# ('D', 27), ('D', 28), ('D', 29), ('D', 30)]
Or, written with loops, as you tried:
out =
for item1, sublist in zip(lst, lst_of_lst):
for item2 in sublist:
out.append((item1, item2))
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
add a comment |
up vote
1
down vote
accepted
It seems that there is a misunderstanding in the way you try to use zip
.
zip(list1, list2)
is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1
and the first item of list2
, and so on.
What you want to do here is to zip(lst, lst_of_lst)
in order to pair each element of lst
to the corresponding sublist of lst_of_lst
. From each pair, you can generate the output you want.
You can do that with a list comprehension:
lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30],]
out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]
print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
# ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
# ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
# ('D', 27), ('D', 28), ('D', 29), ('D', 30)]
Or, written with loops, as you tried:
out =
for item1, sublist in zip(lst, lst_of_lst):
for item2 in sublist:
out.append((item1, item2))
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It seems that there is a misunderstanding in the way you try to use zip
.
zip(list1, list2)
is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1
and the first item of list2
, and so on.
What you want to do here is to zip(lst, lst_of_lst)
in order to pair each element of lst
to the corresponding sublist of lst_of_lst
. From each pair, you can generate the output you want.
You can do that with a list comprehension:
lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30],]
out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]
print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
# ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
# ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
# ('D', 27), ('D', 28), ('D', 29), ('D', 30)]
Or, written with loops, as you tried:
out =
for item1, sublist in zip(lst, lst_of_lst):
for item2 in sublist:
out.append((item1, item2))
It seems that there is a misunderstanding in the way you try to use zip
.
zip(list1, list2)
is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1
and the first item of list2
, and so on.
What you want to do here is to zip(lst, lst_of_lst)
in order to pair each element of lst
to the corresponding sublist of lst_of_lst
. From each pair, you can generate the output you want.
You can do that with a list comprehension:
lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]
lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30],]
out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]
print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
# ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
# ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
# ('D', 27), ('D', 28), ('D', 29), ('D', 30)]
Or, written with loops, as you tried:
out =
for item1, sublist in zip(lst, lst_of_lst):
for item2 in sublist:
out.append((item1, item2))
edited Nov 11 at 17:34
answered Nov 11 at 17:21
Thierry Lathuille
7,12182630
7,12182630
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
add a comment |
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
– Alex M
Nov 11 at 18:42
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53249949%2fhow-to-combine-a-string-list-with-a-list-of-lists-of-integers%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