-1
\$\begingroup\$

What is the most pythonic way to transform string into list, remove item and return to string?

I tried:

qo = ['10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,']
mylist = []

# Transform string to list
for i in qo:
    if i[-1] == ',':
        mylist.append([int(x) for x in i[:-1].split(',')])
    else:
        mylist.append([int(x) for x in i.split(',')])

print(mylist)

# Remove item
for i in mylist:
    i.remove(28)

print(mylist)

# Transform in string again
for i in mylist:
    ''.join(str(i[1:-1]).split())

print(mylist)
\$\endgroup\$
  • \$\begingroup\$ Please clarify what this code should accomplish by stating what the intended output should be at each stage. \$\endgroup\$ – 200_success Jul 28 '17 at 3:36
  • \$\begingroup\$ Your third for loop is useless. You should remove it. \$\endgroup\$ – Eric Citaire Jul 28 '17 at 13:43
2
\$\begingroup\$

Quickest? Look at @Ruan's answer. Most Pythonic? Probably something like this:

def remove_int(haystack, needle, delimiter):
    items = haystack.split(delimiter)
    if items[-1] == "": items.pop(-1)  # Remove blank last element, like in your code
    numbers = list(map(int, items))  # Or [int(x) for x in items]
    numbers.remove(needle)
    return ','.join(map(str, numbers))  # Or ','.join(str(x) for x in items). Note the lack of square brackets.

... But why are you storing data like this, anyway? What's stopping you from storing it as a plaint integer list? Since it seems like that's how you're dealing with it conceptually, the data type should reflect this.

\$\endgroup\$
  • \$\begingroup\$ your function remove all commas in the end. \$\endgroup\$ – Regis da Silva Jul 28 '17 at 15:09
  • \$\begingroup\$ @RegisdaSilva You're right; fixed. \$\endgroup\$ – Quelklef Jul 28 '17 at 15:10
1
\$\begingroup\$

Your code makes 24 functions calls and it takes 0.037619808997988 seconds to run it 3500 times.

However, the code below makes 16 function calls and it takes 0.013617821998195723 seconds to run it 3500 times.

So the code below is about two and a half times faster than your code.

qo = ['10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,']

for i,j in enumerate(qo):

    #Transform string to list
    *mylist, = qo[i]

    for k,l in enumerate(mylist):
        try:
            if mylist[k] == '2' and mylist[k+1] == '8' and mylist[k+2] == ',':

                #Remove item
                mylist.pop(k+1)
                mylist.pop(k)
                mylist.pop(k-1)

        except: pass

        #Transform in string again
    qo[i] = ''.join(mylist)

print(qo)

I achieved this by:

  • Using less temporary variables.
  • Not converting the strings into integers. If I'm not going to deal with them as numbers and do arithmetics over them, this is not necessary.
  • Using .pop() instead of .remove(). I am removing items by their index. It may be tricky to understand what is happening here at a first sight but it gets easier if you remember that the item at the k-numbered index changes after each .pop() call. After all three calls, we sucessfully remove ['2','8',','] from our list.
\$\endgroup\$
  • \$\begingroup\$ Great. What if I want to remove any other number, example 2, 10, 20 or 100? Also, I thought it would be possible, or easy, to remove using only strings, with regex maybe. \$\endgroup\$ – Regis da Silva Jul 28 '17 at 10:56
  • \$\begingroup\$ Just replace if mylist[k] == '2' and mylist[k+1] == '8' and mylist[k+2] == ',': with the number followed by a comma and adjust .pop() to remove a suitable amount of items, or turn this into a function that accepts a variable number of arguments. I'm not very knowledgeable in regex so I can't inform you about that route. \$\endgroup\$ – Ruan Jul 28 '17 at 11:03
  • \$\begingroup\$ I would argue not converting them to numbers is a bad idea. Unless you generalized more, I think it'd be better to do so, since that's what we're dealing with conceptually. I definitely do not like the line if mylist[k] == '2' and mylist[k+1] == '8' and mylist[k+2] == ','. \$\endgroup\$ – Quelklef Jul 28 '17 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.