1

how do I convert all the numerical strings, inside a list of list that contains both alphabetical, and numerical strings, into an integer?

My Output:

[['69', ' Test', 'Results'], ['A', 'B', 'C'], ['D', '420', 'F']]

Intended Output:

[[69, ' Test', 'Results'], ['A', 'B', 'C'], ['D', 420, 'F']]

Note that my code reads a CSV file. Thanks everyone

def get_csv_as_table(a, b):
s = False
import csv

with open(a) as csv_file:
    file_reader = csv.reader(csv_file, delimiter=b)
    member = list(file_reader) 

    print(member)

print ("Enter filename: ")
a = input()
print ("Enter the delimiter: ")
b = input()
get_csv_as_table(a, b)
3
  • How about str.isdigit()? Commented Nov 14, 2020 at 8:55
  • I tried using that yes, but im having trouble operating since its a list of lists :( Commented Nov 14, 2020 at 8:56
  • 1
    [[int(s) if s.isdigit() else s for s in lst] for lst in lists]. Commented Nov 14, 2020 at 8:57

3 Answers 3

1

You can use list comprehension to achieve this. The only minor downside to this is that you will be creating a new list for this instead of modifying the existing list.

my_list = [['69', 'Test', 'Results'], ['A', 'B', 'C'], ['D', '420', 'F']]

filtered_list = [
    [int(item) if item.isdigit() else item for item in sub_list]
    for sub_list in my_list
]

If you want to edit the list in-place, you can use traditional for-loop. The following code will edit the existing list without creating a new list. This could turn out to be useful in case you have a large list.

my_list = [['69', 'Test', 'Results'], ['A', 'B', 'C'], ['D', '420', 'F']]

for i in range(len(my_list)):
    for j in range(len(my_list[i])):
        if my_list[i][j].isdigit():
            my_list[i][j] = int(my_list[i][j])

str.isdigit() checks if a given string is a number or not. An important note to keep in mind is that, it does not work for floating-point numbers, just integers. Once the condition passes, the item is converted to integer.

Sign up to request clarification or add additional context in comments.

2 Comments

Yessss, very nice. Thanks alot my dude! that helped a lot
If this answer seems helpful, you can accept and upvote it :)
1

Yoy have to combine 2 levels of list-comprehension and use str.isdigit()

values = [
    [int(val) if val.isdigit() else val for val in row]
    for row in values
]

Comments

1

Try with 2-level list comprehension and int()+.isdigit() power combo in list comprehension ;-)

l=[['69', ' Test', 'Results'], ['A', 'B', 'C'], ['D', '420', 'F']]
l=[[int(y) if y.isdigit() else y for y in x] for x in l]
print(l)

Output:

[[69, ' Test', 'Results'], ['A', 'B', 'C'], ['D', 420, 'F']]

.isdigit() only works on string representation of pure integers, In case if you have floats too then replace '.' to nothing ;-)

l=[['69', ' Test', 'Results'], ['A', 'B', 'C'], ['D', '420', 'F']]
l=[[float(y) if y.replace('.','').isdigit() else y for y in x] for x in l]
print(l)

Output:

[[69.0, ' Test', 'Results'], ['A', 'B', 'C'], ['D', 420.0, 'F']]

1 Comment

Thanks alot my dudes :) Youre a legend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.