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
  List Comprehension - Creating a list of the length of an item help paul41 0 49 3 hours ago
Last Post: paul41
  removing items from a list or group within a for loop. allusernametaken 9 245 Nov-13-2019, 01:58 AM
Last Post: allusernametaken
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 147 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 128 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Creating a list from a comprehension using a list rm197 6 144 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 320 Sep-28-2019, 04:58 AM
Last Post: perfringo
  convert list of dict to dict of array of list with pop up kk230689 1 139 Sep-13-2019, 07:29 AM
Last Post: perfringo
  can itertools compact a list removing all of some value? Skaperen 6 338 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  calculation with list in list gianniskampanakis 13 622 Aug-09-2019, 12:01 PM
Last Post: gianniskampanakis
  Changing elements of a list to match another list rockproper 2 207 Jul-11-2019, 02:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)