How do I convert string characters into a list? [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a list with the characters of a string?
Example:
'abc'
becomes
['a', 'b', 'c']
Is it a combination of split and slicing?

>>> x = 'abc'
>>> list(x)
['a', 'b', 'c']
Not sure what you are trying to do, but you can access individual characters from a string itself:
>>> x = 'abc'
>>> x[1]
'b'

If you need to iterate over the string you do not even need to convert it to a list:
>>> n = 'abc'
>>> for i in n:
... print i
...
a
b
c
or
>>> n[1]
'b'

yourstring = 'abc'
[char for char in yourstring]

Related

Replace Single Quotes and Comma in Nested List [duplicate]

This question already has answers here:
Join list of strings with a comma
(2 answers)
Closed 3 years ago.
Have a list of list that looks like this:
mylist = [['A'],['A', 'B'], ['A', 'B', 'C']]
Need to remove and replace all ', ' instances with a comma only (no spaces). Output should look like this:
mynewlist = [['A'],['A,B'], ['A,B,C']]
Tried the following:
mynewlist = [[x.replace("', ''",",") for x in i] for i in mylist]
This works on other characters within the nested lists (e.g. replacing 'A' with 'D', but does not function for the purpose described above (something to do with with the commas not being literal strings?).
Try this :
mynewlist = [[','.join(k)] for k in mylist]
OUTPUT :
[['A'], ['A,B'], ['A,B,C']]

Replace multiple symbols using replace()

How can I replace multiple symbols with the method replace()? Is it possible to do that with just one replace()? Or is there any better ways?
The symbols can look like this for example -,+,/,',.,&.
You can use re.sub and put the characters in a character class:
import re
re.sub('[-+/\'.&]', replace_with, input)
You may do it using str.join with generator expression (without importing any library) as:
>>> symbols = '/-+*'
>>> replacewith = '.'
>>> my_text = '3 / 2 - 4 + 6 * 9' # input string
# replace char in string if symbol v
>>> ''.join(replacewith if c in symbols else c for c in my_text)
'3 . 2 . 4 . 6 . 9' # Output string with symbols replaced
# '7' -> 'A', '8' -> 'B'
print('asdf7gh8jk'.replace('7', 'A').replace('8', 'B'))
You can only do one symbol replace, what you could do is create the old strings and new strings list and loop them:
string = 'abc'
old = ['a', 'b', 'c']
new = ['A', 'B', 'C']
for o, n in zip(old, new):
string = string.replace(o, n)
print string
>>> 'ABC'

Not treating integers as strings in a list [duplicate]

This question already has answers here:
How to convert strings numbers to integers in a list?
(4 answers)
Closed 6 years ago.
Given this list:
>>> a = "123DJY65TY"
>>> list(a)
['1','2','3','D','J','Y','6','5','T','Y']
How can I produce a list where integers are not treated as strings? Like this:
[1,2,3,'D','J','Y',6,5,'T','Y']
You can use list comprehension and str.isdigit to convert each character that is a digit:
>>> a = "123DJY65TY"
>>> [int(x) if x.isdigit() else x for x in a]
[1, 2, 3, 'D', 'J', 'Y', 6, 5, 'T', 'Y']
You can convert all strings in a list containing only digits this way, using map() and str.isdigit():
a = map(lambda char: int(char) if char.isdigit() else char, list(a))
For example:
In [3]: a = map(lambda char: int(char) if char.isdigit() else char, list(a))
In [4]: a
Out[4]: [1, 2, 3, 'D', 'J', 'Y', 6, 5, 'T', 'Y']
#niemmi's solution using list comprehensions is probably a better approach given that we start with a string, not a list.

Python issue with finding given string in list of letters

I am fairly new to python and I am trying to figure out how to find if the elements of a list equal a given string?
lists=["a","b",'c']
str1='abc'
I know it is probably easy, but I am having a hard time without using string methods.
Thanks,
DD
>>> l = ['a', 'b', 'c']
>>> l == list('abc')
True
But, if the order of items in the list can be arbitrary, you can use sets:
>>> l = ['c', 'b', 'a']
>>> set(l) == set('abc')
True
or:
>>> l = ['c', 'b', 'a']
>>> s = set(l)
>>> all(c in s for c in 'abc')
True
>>> lists=["a","b",'c']
>>> str1='abc'
>>> ''.join(lists) == str1
True
you can use .join to create a string from your list:
list = ['a', 'b', 'c']
strToComapre = ''.join(list1)
Now you can check if strToComapre is "in" the original str:
if strToCompare in originalStr:
print "yes!"
If you want a pure compare use:
if strToCompare == originalStr:
print "yes! it's pure!"
There are lots of options in python i'll add some other useful posts:
Compare string with all values in array
http://www.decalage.info/en/python/print_list

How can I turn a string into a list in Python? [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 6 years ago.
How can I turn a string (like 'hello') into a list (like [h,e,l,l,o])?
The list() function [docs] will convert a string into a list of single-character strings.
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Even without converting them to lists, strings already behave like lists in several ways. For example, you can access individual characters (as single-character strings) using brackets:
>>> s = "hello"
>>> s[1]
'e'
>>> s[4]
'o'
You can also loop over the characters in the string as you can loop over the elements of a list:
>>> for c in 'hello':
... print c + c,
...
hh ee ll ll oo

Categories

Resources