Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 removing quotes from a list and keep type list
#1
hello all ..
im trying to use sys.argv take and ip like : 192.168.1.99 and convert it to [192.168.1.99] ... but it add '
import sys

start = sys.argv[1]
start = start.replace(".", ",")
start = [start]
print(start)
Output:
['85,8,8,8']
i need to remove ' from it and print it like : [85,8,8,8] but i need to keep the type (list) not str
how i can do that
Quote
#2
You still have the type list, but the value inside is string. So you want to change the value inside to multiple int numbers from my interpretation. Here's how that can be done
List = ['85, 8, 8, 8']
output = []

for number in List[0].split(','):
    while ' ' in number:
		number = number.replace(' ', '')
    output.append(int(number))
print(output)
Output:
[85, 8, 8, 8]
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#3
start will be a str, so you can do something like

use map
>>> start = '192.168.1.99'
>>> start = list(map(int, start.split('.'))) # you need to convert the map object to list
>>> start
[192, 168, 1, 99]
or use list comprehension
>>> start = '192.168.1.99'
>>> start = [int(num) for num in start.split('.')]
>>> start
[192, 168, 1, 99]
Quote
#4
When dealing with IP addresses it’s might be good idea to use built-in ipaddress module.
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Add items from one list to list of lists PUP280 5 512 May-05-2020, 03:47 PM
Last Post: PUP280
  Help removing asterisk item in a nested list. bmcguire 3 183 Apr-06-2020, 02:35 PM
Last Post: snippsat
  appending list of list glennford49 2 219 Mar-29-2020, 09:33 AM
Last Post: ibreeden
  dot product of a list by a list in a dictionary stored as a value Fraher123 1 182 Mar-25-2020, 06:08 PM
Last Post: deanhystad
  Creating new list based on exact regex match in original list interjectdirector 1 201 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  create a list of object with a list of character studenthch 0 155 Feb-12-2020, 08:43 AM
Last Post: studenthch
  List items verification for Integer type vintysaw 4 321 Jan-17-2020, 01:56 PM
Last Post: perfringo
  Type hinting - return type based on parameter micseydel 2 232 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how to compare a list to a list of lists kevthew 1 246 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 778 Dec-16-2019, 12:57 PM
Last Post: BamBi25

Forum Jump:


Users browsing this thread: 1 Guest(s)