Extend multiple elements in one line iteration [duplicate]
This question already has an answer here:
List comprehension version of “extend” [duplicate]
1 answer
For
A=[1,2,3]
I would like to get
B=['r1','t1','r2','t2','r3','t3']
I know it is easy to get ['r1','r2','r3']
by
['r'+str(k) for k in A]
How could I get B by one line loop as I showed above?
Many thanks.
python list loops iteration
marked as duplicate by Andras Deak, jpp, smci, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
List comprehension version of “extend” [duplicate]
1 answer
For
A=[1,2,3]
I would like to get
B=['r1','t1','r2','t2','r3','t3']
I know it is easy to get ['r1','r2','r3']
by
['r'+str(k) for k in A]
How could I get B by one line loop as I showed above?
Many thanks.
python list loops iteration
marked as duplicate by Andras Deak, jpp, smci, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12
add a comment |
This question already has an answer here:
List comprehension version of “extend” [duplicate]
1 answer
For
A=[1,2,3]
I would like to get
B=['r1','t1','r2','t2','r3','t3']
I know it is easy to get ['r1','r2','r3']
by
['r'+str(k) for k in A]
How could I get B by one line loop as I showed above?
Many thanks.
python list loops iteration
This question already has an answer here:
List comprehension version of “extend” [duplicate]
1 answer
For
A=[1,2,3]
I would like to get
B=['r1','t1','r2','t2','r3','t3']
I know it is easy to get ['r1','r2','r3']
by
['r'+str(k) for k in A]
How could I get B by one line loop as I showed above?
Many thanks.
This question already has an answer here:
List comprehension version of “extend” [duplicate]
1 answer
python list loops iteration
python list loops iteration
asked Nov 12 at 15:08
Xiaojian Chen
425
425
marked as duplicate by Andras Deak, jpp, smci, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Andras Deak, jpp, smci, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12
add a comment |
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12
add a comment |
3 Answers
3
active
oldest
votes
You can use a nested list comprehension.
>>> A=[1,2,3]
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Use a nested list comprehension:
A=[1,2,3]
B = [prefix + str(a) for a in A for prefix in 'rt']
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Using itertools.product
import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']
I think you probably wanty + str(x)
but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a nested list comprehension.
>>> A=[1,2,3]
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
You can use a nested list comprehension.
>>> A=[1,2,3]
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
You can use a nested list comprehension.
>>> A=[1,2,3]
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']
You can use a nested list comprehension.
>>> A=[1,2,3]
>>> [fmt.format(n) for n in A for fmt in ('r{}', 't{}')]
['r1', 't1', 'r2', 't2', 'r3', 't3']
answered Nov 12 at 15:10
timgeb
48.8k116390
48.8k116390
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
1
1
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
I like the answer best! It is so clear! Thanks you so much!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Use a nested list comprehension:
A=[1,2,3]
B = [prefix + str(a) for a in A for prefix in 'rt']
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Use a nested list comprehension:
A=[1,2,3]
B = [prefix + str(a) for a in A for prefix in 'rt']
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Use a nested list comprehension:
A=[1,2,3]
B = [prefix + str(a) for a in A for prefix in 'rt']
Use a nested list comprehension:
A=[1,2,3]
B = [prefix + str(a) for a in A for prefix in 'rt']
answered Nov 12 at 15:09
Daniel Mesejo
12.1k1924
12.1k1924
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
1
1
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
It seems like two loops in a line! It works! Thanks a lot!
– Xiaojian Chen
Nov 12 at 15:20
add a comment |
Using itertools.product
import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']
I think you probably wanty + str(x)
but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
add a comment |
Using itertools.product
import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']
I think you probably wanty + str(x)
but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
add a comment |
Using itertools.product
import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']
Using itertools.product
import itertools
list(itertools.product(*[[1,2,3],['r','t']]))
Out[20]: [(1, 'r'), (1, 't'), (2, 'r'), (2, 't'), (3, 'r'), (3, 't')]
[y +str(x) for x, y in list(itertools.product(*[[1, 2, 3], ['r', 't']]))]
Out[22]: ['r1', 't1', 'r2', 't2', 'r3', 't3']
edited Nov 12 at 15:13
answered Nov 12 at 15:11
W-B
99.8k73163
99.8k73163
I think you probably wanty + str(x)
but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
add a comment |
I think you probably wanty + str(x)
but otherwise, good use of itertools
– Woody1193
Nov 12 at 15:12
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
I think you probably want
y + str(x)
but otherwise, good use of itertools– Woody1193
Nov 12 at 15:12
I think you probably want
y + str(x)
but otherwise, good use of itertools– Woody1193
Nov 12 at 15:12
3
3
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
Is there some people given downvote for all answer here ? u'd better leave a reason
– W-B
Nov 12 at 15:12
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
@Woody1193 thank you updated
– W-B
Nov 12 at 15:13
add a comment |
Possible duplicate of stackoverflow.com/questions/38400096/…, stackoverflow.com/questions/5947137/…, stackoverflow.com/questions/44667519/…
– Andras Deak
Nov 12 at 15:12