How can you take a list and get rid of the quotes when it prints out?
if you have this:
>>list = ["123", "456", "789"]
>>print list
['123','456','789']
but want it to print out like this:
[123,456,789]
or just
123,456,789
I tried .split or .replace but it says list has no attribute split or replace
5 65932 bvdet 2,851
Expert Mod 2GB
Here's some options: - >>> lst = ["123", "456", "789"]
-
>>> print ','.join(lst)
-
123,456,789
-
>>> print "[%s]" % (','.join(lst))
-
[123,456,789]
-
>>> print [int(i) for i in lst]
-
[123, 456, 789]
-
>>>
Those work great, thanks. One more question...what if the list contains something other than integers as well but I just need the integers without quotes? Example:
lst = ["hat", "3", "5", "8"]
["hat", 3, 5, 8]
I tried the last suggestion you posted with [int ( i ) for i in lst] but it gives an error message.
I'm sure bvdet can give you something better, but -
lst = ["hat", "3", "5", "8"]
-
lst_4_print = []
-
for i in lst:
-
try:
-
lst_4_print.append(int(i))
-
except ValueError:
-
lst_4_print.append(i)
-
print lst_4_print
-
bvdet 2,851
Expert Mod 2GB
I'm sure bvdet can give you something better, but -
lst = ["hat", "3", "5", "8"]
-
lst_4_print = []
-
for i in lst:
-
try:
-
lst_4_print.append(int(i))
-
except ValueError:
-
lst_4_print.append(i)
-
print lst_4_print
-
This is not necessarily better, just slightly different: - >>> lst = ["hat", "3", "5", "8"]
-
>>> lst_4_print = []
-
>>> for i in lst:
-
... if i.isdigit():
-
... lst_4_print.append(int(i))
-
... else:
-
... lst_4_print.append(i)
-
...
-
>>> lst_4_print
-
['hat', 3, 5, 8]
-
>>>
Thank you both for your suggestions!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: deko |
last post by:
I'm trying to log error messages and sometimes (no telling when or where)
the message contains a string with double quotes. Is there a way get the
query to insert the string with the double...
|
by: RyanTaylor |
last post by:
I have a final coming up later this week in my beginning Java class and my
prof has decided to give us possible Javascript code we may have to write.
Problem is, we didn't really cover JS and what...
|
by: Miguel E. |
last post by:
Hi,
I've been (self) studying Python for the past two months and I have had
no background in OOP whatsoever.
I was able to write an interactive program that randomly selects an item
from a...
|
by: nuffnough |
last post by:
This is python 2.4.3 on WinXP under PythonWin.
I have a config file with many blank lines and many other lines that I
don't need.
read the file in, splitlines to make a list, then run a loop...
|
by: bruce |
last post by:
hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii...
|
by: Eric_Dexter |
last post by:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename + "orc"
for line in alllines:
if not...
|
by: NeoPa |
last post by:
Background
Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
|
by: Michael Yanowitz |
last post by:
Hello:
If I have a long string (such as a Python file).
I search for a sub-string in that string and find it.
Is there a way to determine if that found sub-string is
inside single-quotes or...
|
by: m6s |
last post by:
Hello to all,
I am having trouble removing an item from list, which the item is a list by itself.
<code>
for self.line in self.filtered:
if self.appid:
if self.line <> self.appid:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |