As.Alist.Character

as.alist.character?

I'm just copying my own dirty solution here from #142 which is pretty much equivalent to yours (and has the same issue about "infernal circles"):

x = 'label,a=1,b=asdf,c="qwer",d=FALSE,e=c(1,2,3)'
z = formals(eval(parse(text = sprintf('function(%s){}', x))))
str(z)

Let's see if there are other cleaner tricks.

Can you add characters from a string to a list?

Use extend instead of append function.

#Extend
x=input('Choose word: ').lower()
letters=[]
letters.extend(list(x))
print(letters)

# ['p', 'y', 't', 'h', 'o', 'n']

And to remove a character from a list while retaining position as blank after removing, use replace while within a list:

y=input("Choose a letter to remove: ").lower()
removed=[s.replace(y,'') for s in letters]
print(removed)

#['p', '', 't', 'h', 'o', 'n']

I hope this help, unless its different from what you want. Then let me know. Otherwise, happy coding!

Break string into list of characters in Python

Strings are iterable (just like a list).

I'm interpreting that you really want something like:

fd = open(filename,'rU')
chars = []
for line in fd:
for c in line:
chars.append(c)

or

fd = open(filename, 'rU')
chars = []
for line in fd:
chars.extend(line)

or

chars = []
with open(filename, 'rU') as fd:
map(chars.extend, fd)

chars would contain all of the characters in the file.

Store each character of a String in a list

use a list comprehension:

In [24]: s = ['ABC','DEF','GHI','JKL']

In [25]: lis=[list(x) for x in s]

In [26]: lis
Out[26]: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L']]

or use map():

In [27]: lis1=map(list,s)

In [28]: lis1
Out[28]: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L']]

Convert a list of characters into a string

Use the join method of the empty string to join all of the strings together with the empty string in between, like so:

>>> a = ['a', 'b', 'c', 'd']
>>> ''.join(a)
'abcd'


Related Topics



Leave a reply



Submit