Python Forum
Delete strings from a list to create a new only number list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete strings from a list to create a new only number list
#1
Hello,

I am doing a Python training course and am stuck at this question:

Got a list:
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]

Want to have this result:
Num_List = [10, 20, 30, 40]

Used this code:
Num_List = []
for entry in range(len(Char_List)):
    if type(entry) != str:
        Num_List.append(entry)
print(Num_List)
But I get [0, 1, 2, 3, 4, 5], the indexes of the complete Char_List is shown instead of the numeric content. Thanks.
Reply
#2
maybe using isinstance like:

Num_List = []
for Num in Char_List: if not isinstance(Num, str): Num_List.append(Num)
print(Num_List)
Reply
#3
use for entry in Char_List

Char_List = ['Bla', 'Blu', 10, 20, 30, 40]

Num_List = []
for entry in Char_List:
    if type(entry) != str:
        Num_List.append(entry)
print(Num_List)
or for using len

Char_List = ['Bla', 'Blu', 10, 20, 30, 40]

Num_List = []
for x in range(len(Char_List)):
    if type(Char_List[x]) != str:
        Num_List.append(Char_List[x])
print(Num_List)
Reply
#4
Same as a list-comprehension:
mixed_types = ['Bla', 'Blu', 10, 20, 30, 40]

numbers = [element for element in mixed_types if isinstance(element, int)]
Or with filter:
def isint(x):
    return isinstance(x, int)


mixed_types = ['Bla', 'Blu', 10, 20, 30, 40]
numbers = list(filter(isint, mixed_types))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Quote:But I get [0, 1, 2, 3, 4, 5], the indexes of the complete Char_List is shown instead of the numeric content. Thanks.
The problem is you never referenced the items in the list. This gives you sequence of numbers whose only relation to the list is the count is the same.
for entry in range(len(Char_List)):  # entry is not an item from  the list.  It is a number 0 to 5
Your code would work fine if you checked the actual list values.
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]
Num_List = []
for entry in Char_List:  #<- Loop through the list values
    if type(entry) != str:
        Num_List.append(entry)
print(Num_List)
Reply
#6
use below code:

import numpy as np
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]
Num_List = np.arange(10,len(Char_List)*10,10)
print(Num_List)
Reply
#7
(Apr-27-2023, 11:32 PM)learningPython Wrote: use below code:

import numpy as np
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]
Num_List = np.arange(10,len(Char_List)*10,10)
print(Num_List)




I misunderstood your question earlier. Here is the right code:
If you want to use numpy library:

import numpy as np
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]
arr = np.array(Char_List)    #### converting into numpy array
Num_List = np.array(arr[np.char.isdigit(arr)],dtype = 'int')  ## np.char.isdigit(arr) gives boolean output True if array valye is digit else False. Then creating array out of it with datatype "int"
print(Num_List)
Ouput:
[10 20 30 40]
Reply
#8
If this is a training course question, maybe you should just do what the question actually asks?

Enumerate through your list.
Check if current iteration is a string and if so, delete it.
Return (or print) the list that's now devoid of strings.

This should be simple and only require basic Python. You don't need type-checking tools, list comprehensions or Numpy(!? Doh ?!)

Once you can do that, preferably on you own, take a look at some of the other answers posted for alternative methods.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#9
Quote:If this is a training course question, maybe you should just do what the question actually asks?
You are assuming the instructions say delete the string values from the list. That is in the thread topic, but the OP says this:
Got a list:
Char_List = ['Bla', 'Blu', 10, 20, 30, 40]

Want to have this result:
Num_List = [10, 20, 30, 40]
One implies changing the list object, the other implies making a new list (otherwise why the new variable name?). Changing the existing list is a more interesting challenge.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 364 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Create X Number of Variables and Assign Data RockBlok 8 973 Nov-14-2023, 08:46 AM
Last Post: perfringo
  How to read module/class from list of strings? popular_dog 1 483 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,183 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 775 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 714 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 924 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Help with Logical error processing List of strings dmc8300 3 1,093 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,855 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020