Python Forum
Partial convertion string to int in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial convertion string to int in lists
#1
Hi everyone!

I'm new in Python and I think it's amazing. I'm doing an automation project to convert a string in values in table.
Now, I'd like to insert in my work a string that works in my list. In particular I need that all items (string type) inside the list are converted in int to recognize it in forward operations.
List is similar to this list=['A', 'B', '3', 'C', '4']
I want that numbers are not strings, but ints.

Thanks a lot and Let's Python!! :D :D
Reply
#2
What have you tried?
Reply
#3
One way to describe it in spoken language: "for every member of list try to convert into int, except in case of value error when leave as it is"
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Here, for loops are incredibly useful. i also used range to int only numbers -
aList = ['A', '4', 'G', '1', '3']
x = 0
for i in aList:
    if i in range(-9999, 9999): #IDK if there is a better way to identify a number
        aList[x] = int(i)
        x += 1
Untested btw
Reply
#5
(Apr-22-2019, 07:49 PM)SheeppOSU Wrote: Untested btw
Do test,i think there are some problems,and there are better ways Wink

Do try something next time @satellite89,if it dos not work doesn't matter at all it's the effort that counts.
To make some code of @perfringo explanation.
def convert(value):
    try:
        return int(value)
    except ValueError:
        return value
Then can use this function in a couple of ways.
>>> lst = ['A', 'B', '3', 'C', '4']
>>> list(map(convert, lst))
['A', 'B', 3, 'C', 4]
>>> 
>>> [convert(i) for i in lst]
['A', 'B', 3, 'C', 4]
Reply
#6
I solve this problem, but now I want to create sub lists on the main one.
mylist=['a','b','/',3,'/','c']
number 3 is an int, and I want to make a sub list inside mylist with this items: '/',3,'/'.
How could I do?
Reply
#7
If this is solved mark it as solved and start a new question and please explain it better than you have Confused
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 536 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 744 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,199 Mar-28-2023, 07:24 AM
Last Post: buran
  remove partial duplicates from csv ledgreve 0 746 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 953 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Optimal way to search partial correspondence in a large dict genny92c 0 975 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,338 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Partial key lookup in dictionary GaryNR 1 3,384 Jul-16-2020, 06:55 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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