Posts: 22
Threads: 13
Joined: Dec 2016
Here is my input
I need the output like this:
Output: [1,5,2,6,3,7,4,8]
How to do this without build in function
Posts: 591
Threads: 26
Joined: Sep 2016
Please show us an attempt so we can further assist you.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Dec-21-2016, 08:47 AM
(This post was last modified: Dec-21-2016, 08:50 AM by wavic.)
Hello! Open a Python interpreter, input a=[1,2,3,4] then input dir(a) and see the output. It'll print all the 'a' methods.
Posts: 22
Threads: 13
Joined: Dec 2016
Dec-21-2016, 09:01 AM
(This post was last modified: Dec-21-2016, 09:22 AM by MeeranRizvi.)
Hello Guys!
i just did the following code works as expected.
1 2 3 4 5 6 7 |
a = [ 1 , 2 , 3 , 4 ]
b = [ 5 , 6 , 7 , 8 ]
c = []
for i in range ( len (a)):
c.append(a[i])
c.append(b[i])
print c
|
Output: [1, 5, 2, 6, 3, 7, 4, 8]
Thanks for the info #Wavic
how to make this output to look like this:
Output: [1, 5, 2, 6, 3, 7, 4, 8]
Output: [(1, 5), (2, 6),(3, 7),(4, 8)]
Posts: 2,953
Threads: 48
Joined: Sep 2016
There is a better way to do it. Without a loop
Posts: 591
Threads: 26
Joined: Sep 2016
(Dec-21-2016, 09:01 AM)MeeranRizvi Wrote:
1 |
[( 1 , 5 ), ( 2 , 6 ),( 3 , 7 ),( 4 , 8 )]
|
If this is what you want, look into the zip function.
Posts: 22
Threads: 13
Joined: Dec 2016
Yeah i knew it,
BUt how to do without zip?
Posts: 3,458
Threads: 101
Joined: Sep 2016
Why would you want to do it without zip?
1 2 3 4 5 6 |
>>> a = [ 1 , 2 , 3 , 4 ]
>>> b = [ 5 , 6 , 7 , 8 ]
>>> list ( zip (a, b))
[( 1 , 5 ), ( 2 , 6 ), ( 3 , 7 ), ( 4 , 8 )]
>>> [(a[i], b[i]) for i in range ( min ( len (a), len (b)))]
[( 1 , 5 ), ( 2 , 6 ), ( 3 , 7 ), ( 4 , 8 )]
|
Posts: 7,324
Threads: 123
Joined: Sep 2016
Jan-03-2017, 06:12 PM
(This post was last modified: Jan-03-2017, 06:12 PM by snippsat.)
Quote:Why would you want to do it without zip?
Because this is a standard way many do education,do task without using stuff(forbid to use) that's build into python.
I do not like this way at all
We even see student that have to use regex with html,because they can not a use parser.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-03-2017, 06:28 PM
(This post was last modified: Jan-03-2017, 06:43 PM by wavic.)
Can you use enumerate()
Because of I still celebrate here is another way
1 2 3 4 5 6 7 8 9 10 11 12 |
In [ 1 ]: a = [ 1 , 2 , 3 , 4 ]
In [ 2 ]: b = [ 5 , 6 , 7 , 8 ]
In [ 3 ]: c = []
In [ 4 ]: for index, var in enumerate (a):
...: c.append((var, b[index]))
...:
In [ 5 ]: c
Out[ 5 ]: [( 1 , 5 ), ( 2 , 6 ), ( 3 , 7 ), ( 4 , 8 )]
|
|