Home > Mobile >  TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list

Time:05-31

how to get list that looks like likes to likes_clean

likes = ['82.3k', '20k', '18.2k', '17.4k', '14.2k', '13.8k', '13k', '11.4k', '9.2k', 
 '8.7k', '8k', '7.1k', '6.8k', '5.7k', '5.2k', '4.9k', '4.8k', '4.6k', '4.6k', '4.6k', 
 '4.4k', '4.4k', '4.2k', '4k', '4k', '3.6k', '3.6k', '3.3k', '3.2k', '3k]

likes_clean = [82300,20000,18200,17400,14200,13800,13000,11400,9200,8700,8000,7100,6800,5700,5200,4900,4800,4600,4600,4600,4400,
 4400,4200,4000,4000,3600,3600,3300,3200,3000]

#This is what i tried

def likes_count(lks):
    lks = likes
    if lks[-1] == 'k':
      return int(float(lks[:-1]) * 1000)  
    return int(lks)
 
# i'm getting error when i try to call the function
likes_count(likes)

I'm getting TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

CodePudding user response:

The error message is clear. The int() parameter isn't valid. Use a list comprehension to process each individual entry, and the values are floats not integers:

# added some non-k values for testing
likes = ['1','2','3','82.3k', '20k', '18.2k', '17.4k', '14.2k', '13.8k', '13k', '11.4k', '9.2k', 
 '8.7k', '8k', '7.1k', '6.8k', '5.7k', '5.2k', '4.9k', '4.8k', '4.6k', '4.6k', '4.6k', 
 '4.4k', '4.4k', '4.2k', '4k', '4k', '3.6k', '3.6k', '3.3k', '3.2k', '3k']

likes_clean = [1,2,3,82300,20000,18200,17400,14200,13800,13000,11400,9200,8700,8000,7100,6800,5700,5200,4900,4800,4600,4600,4600,4400,
 4400,4200,4000,4000,3600,3600,3300,3200,3000]

# items with k have possible floating point to be converted,
# but without k expected to be whole integers.
likes_processed = [int(float(x[:-1])*1000) if x.endswith('k') else int(x)
                   for x in likes]

print(likes_processed)
print(likes_processed == likes_clean)

Output:

[1, 2, 3, 82300, 20000, 18200, 17400, 14200, 13800, 13000, 11400, 9200, 8700, 8000, 7100, 6800, 5700, 5200, 4900, 4800, 4600, 4600, 4600, 4400, 4400, 4200, 4000, 4000, 3600, 3600, 3300, 3200, 3000]
True

CodePudding user response:

How about using a list comprehension where you cast each like into a float and only cast into an int at the end after you have multiplied by a thousand:

likes = [
    '82.3k', '20k', '18.2k', '17.4k', '14.2k', '13.8k', '13k', '11.4k', '9.2k',
    '8.7k', '8k', '7.1k', '6.8k', '5.7k', '5.2k', '4.9k', '4.8k', '4.6k',
    '4.6k', '4.6k', '4.4k', '4.4k', '4.2k', '4k', '4k', '3.6k', '3.6k', '3.3k',
    '3.2k', '3k'
]
expected_likes_clean = [
    82300, 20000, 18200, 17400, 14200, 13800, 13000, 11400, 9200, 8700, 8000,
    7100, 6800, 5700, 5200, 4900, 4800, 4600, 4600, 4600, 4400, 4400, 4200,
    4000, 4000, 3600, 3600, 3300, 3200, 3000
]
likes_clean = [int(float(like[:-1]) * 1000) for like in likes]
print(f'{likes_clean = }')
print(f'{expected_likes_clean == likes_clean = }')

Output:

likes_clean = [82300, 20000, 18200, 17400, 14200, 13800, 13000, 11400, 9200, 8700, 8000, 7100, 6800, 5700, 5200, 4900, 4800, 4600, 4600, 4600, 4400, 4400, 4200, 4000, 4000, 3600, 3600, 3300, 3200, 3000]
expected_likes_clean == likes_clean = True

CodePudding user response:

This is because int() is expecting only a string or a bytes-like object or a real number.

But you have put a list into the int(). It will not convert that list into a integer list as it converts strings to int.

You problem can be solved as below.

likes = ['82.3k', '20k', '18.2k', '17.4k', '14.2k', '13.8k', '13k', '11.4k', '9.2k', 
 '8.7k', '8k', '7.1k', '6.8k', '5.7k', '5.2k', '4.9k', '4.8k', '4.6k', '4.6k', '4.6k', 
 '4.4k', '4.4k', '4.2k', '4k', '4k', '3.6k', '3.6k', '3.3k', '3.2k', '3k']

likes_clean = [82300,20000,18200,17400,14200,13800,13000,11400,9200,8700,8000,7100,6800,5700,5200,4900,4800,4600,4600,4600,4400,
 4400,4200,4000,4000,3600,3600,3300,3200,3000]


def likes_count(lks):
    output=[]
    for like in lks:
        if(like[-1]=="k"):
            output.append(int(float(like[0:-1])*1000))
    return output
 

print(likes_count(likes))

OUTPUT

[82300, 20000, 18200, 17400, 14200, 13800, 13000, 11400, 9200, 8700, 8000, 7100, 6800, 5700, 5200, 4900, 4800, 4600, 4600, 4600, 4400, 4400, 4200, 4000, 4000, 3600, 3600, 3300, 3200, 3000]

There is one another problem that I have noticed.

You are assigning likes to lks in your likes_count funtion which is no use because likes list is the list you are passing as an argument when calling the method.

It means in the function it automatically gets the likes list and assign to lks variable. But again you assigning the same likes list to lks.

  • Related