0

I want to convert the strings in sublists into integer. Here is the code,

Example Input:

totalclient=[['5', '110'], ['9', '500'], ['20', '400']]
totalhouse=[['10', '100'], ['2', '200'], ['30', '300']]

This example list has only 4 sublists, But I want to process over 700 sublists for two lists and every sublist will have more than 300 strings!

Code:

totalclient = [list(map(int, sublist)) for sublist in totalclient]
totalhouse = [list(map(int, sublist)) for sublist in totalhouse]

Output

[[5, 110], [9, 500], [20, 400]]
[[10, 100], [2, 200], [30, 300]]

Above code works for me! But for loop takes too much time to convert it is there any other way to convert it into integer without using for loop?

I tried to convert sublist while inputting sublists like belowcode but it also takes same time!

for i in range(0,n[0]):
    client=[int(x) for x in input().split()]
    totalclient.append(client)
AzizStark
  • 1,390
  • 1
  • 17
  • 27
  • You have a use a *for* somewhere, even if you're not typing `for ...` explicitly. Except of course, you want to manually remove the quotes yourself, then no need for *for*. – Moses Koledoye Oct 17 '17 at 13:17
  • What you are saying please say clearly – AzizStark Oct 17 '17 at 13:18
  • The above commenter is right, but it might be faster to do it in place? Loop over it and explicitly change each one rather than creating a new list. It's still a for loop, but you avoid needing to create extra space in memory. – James Oct 17 '17 at 13:19
  • You guys saying to put like this right? for i in range(0,n[0]): client=[int(x) for x in input().split()] totalclient.append(client) – AzizStark Oct 17 '17 at 13:21
  • @AzizStark Your code is fine. Some tasks simply take some time. And how much time are we talking about here? It does not feel like this should be more than a few seconds if even.. – Ma0 Oct 17 '17 at 13:23
  • Don't you have to iterate through your sublist object before applying it to `map` to convert it into integer? I believe you have not posted the complete code? – Prasad Oct 17 '17 at 13:25
  • I edited the question see it – AzizStark Oct 17 '17 at 13:25
  • Yes, i didnot posted the complete code because it is a part of my Bipartite matching Script! – AzizStark Oct 17 '17 at 13:28
  • 2
    In the end, you still have to convert all the strings to integers. It does not matter how you loop over the collection, there are no shortcuts, unfortunately. – Kendas Oct 17 '17 at 13:28
  • 1
    possible duplicate: https://stackoverflow.com/q/46341628/7692463 – scharette Oct 17 '17 at 13:29
  • This code converts the list elements into strings without for loop! n = list(map(int, n)) – AzizStark Oct 17 '17 at 13:29
  • but for only lists without sublists – AzizStark Oct 17 '17 at 13:30
  • 2
    list() is a for loop. It loops over the stuff in the brackets. – Sekuraz Oct 17 '17 at 13:32
  • 2
    `n = list(map(int, n))` is essentially identical to `n = [int(item) for item in n]`. The fact that you didn't write `for`, does not mean that a for loop doesn't exist. – Kendas Oct 17 '17 at 13:32
  • Try collecting your input as integers to begin with, if you really are that concerned about it – OneCricketeer Oct 17 '17 at 13:34
  • @cricket_007 As i said that also makes use of for loop and takes same time. – AzizStark Oct 17 '17 at 13:35
  • Can anyone optimize my entire code if I send you? – AzizStark Oct 17 '17 at 13:37
  • For loop where? No loops are needed for `totalclient=[[5, 110]]`... You're focusing too much on the implementation than benchmarking where the actual bottleneck of the code is – OneCricketeer Oct 17 '17 at 13:38
  • If you'd like someone to review your working code, please see https://codereview.stackexchange.com/tour – OneCricketeer Oct 17 '17 at 13:40
  • Here is my entire code : http://textuploader.com/d4uzo Could any one help me? – AzizStark Oct 17 '17 at 13:41
  • I used pypy3 instead of python it worked 3 times faster than python3!! Python 3 needs to be improved! – AzizStark Oct 17 '17 at 14:29
  • @scharette thanks for the help! that post helped me a lot! – AzizStark Oct 17 '17 at 14:30
  • No problem ! Glad it helped – scharette Oct 17 '17 at 14:32
  • Sad that people that people don't upvote the comment though because there will be no accepted/useful answer to the question. – scharette Oct 17 '17 at 14:34

2 Answers2

1

This was faster on my machine:

tc = np.array(list(repeat(['1000000000000000','200000000000'], 700)))
tl = np.array(list(repeat(['2000000000000000','300000000000'], 700)))

def s1(tc,tl):
    totalc = [list(map(int, sublist)) for sublist in tc]
    totalh = [list(map(int, sublist)) for sublist in tl]

def s2(tc,tl):
    ntc = list(map(lambda w: [map(int, w)], chain(tc)))
    ntc = list(map(lambda w: [map(int, w)], chain(tl)))

def wrapper(func, *args, **kwargs):
    def wrapped():
        return func(*args, **kwargs)
    return wrapped

wrp1 = wrapper(s1, tc, tl)
wrp2 = wrapper(s2, tc, tl)
print(timeit.timeit(wrp1, number=1000))
print(timeit.timeit(wrp2, number=1000))

>>> wrp1 = 5.529523203999929
>>> wrp2 = 0.8688932149999573
00sdf0
  • 148
  • 1
  • 8
-1

try like this,

from functools import *
totalclient=[['5', '110'], ['9', '500'], ['20', '400']]
totalhouse=[['10', '100'], ['2', '200'], ['30', '300']]
a1 = []
a2 = []
for i in totalclient:
    aa = map((lambda x:int(x)),i)
    a1.append(list(aa))
totalclient = a1
for i in totalhouse:
    aa = map((lambda x:int(x)),i)
    a2.append(list(aa))
totalhouse = a2

print(totalclient)
print(totalhouse)