Python 将字符串(不带任何分隔符)转换为列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15526883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string (without any separator) to list
提问by nv39
I have a phone number(string), e.g. "+123-456-7890", that I want to turn into a list that looks like: [+, 1, 2, 3, -, ...., 0].
我有一个电话号码(字符串),例如“+123-456-7890”,我想将其转换为如下所示的列表:[+, 1, 2, 3, -, ...., 0]。
Why? So I can go iterate through the list and remove all the symbols, so I'm left with a list of only digits, which I can then convert back to a string.
为什么?所以我可以遍历列表并删除所有符号,所以我只剩下一个数字列表,然后我可以将其转换回字符串。
What's the best way to solve this problem? None of the solutions I've come across are applicable, because I don't have any special characters in-between the digits (so I can't split the string there.)
解决这个问题的最佳方法是什么?我遇到的所有解决方案都不适用,因为我在数字之间没有任何特殊字符(所以我不能在那里拆分字符串。)
Any ideas? I really appreciate it!
有任何想法吗?对此,我真的非常感激!
Edit - this is what I've tried:
编辑 - 这是我尝试过的:
x = row.translate(None, string.digits)
list = x.split()
Also:
还:
filter(lambda x: x isdigit())
采纳答案by mgilson
You mean that you want something like:
你的意思是你想要这样的东西:
''.join(n for n in phone_str if n.isdigit())
This uses the fact that strings are iterable. They yield 1 character at a time when you iterate over them.
这使用了字符串可迭代的事实。当您迭代它们时,它们一次产生 1 个字符。
Regarding your efforts,
关于你的努力,
This one actually removesall of the digits from the string leaving you with only non-digits.
这实际上从字符串中删除了所有数字,只留下非数字。
x = row.translate(None, string.digits)
This one splits the string on runs of whitespace, not after each character:
这个在空白运行时拆分字符串,而不是在每个字符之后:
list = x.split()
回答by poitroae
Make a list(your_string).
制作一个列表(your_string)。
>>> s = "mep"
>>> list(s)
['m', 'e', 'p']
回答by uptownnickbrown
A python string isa list of characters. You can iterate over it right now!
python 字符串是一个字符列表。你现在可以迭代它!
justdigits = ""
for char in string:
if char.isdigit():
justdigits += str(char)
回答by tur1ng
You can use the re module:
您可以使用 re 模块:
import re
re.sub(r'\D', '', '+123-456-7890')
This will replace all non-digits with ''.
这将用''替换所有非数字。
回答by Kabie
''.join(filter(str.isdigit, "+123-456-7890"))
回答by Justin Ethier
Instead of converting to a list, you could just iterate over the first string and create a second string by adding each of the digit characters you find to that new string.
无需转换为列表,您只需遍历第一个字符串并通过将您找到的每个数字字符添加到该新字符串来创建第二个字符串。
回答by Rob?
You canuse str.translate, you just have to give it the right arguments:
你可以使用str.translate,你只需要给它正确的参数:
>>> dels=''.join(chr(x) for x in range(256) if not chr(x).isdigit())
>>> '+1-617-555-1212'.translate(None, dels)
'16175551212'
N.b.: This won't work with unicode strings in Python2, or at all in Python3. For those environments, you can create a custom class to pass to unicode.translate:
注意:这不适用于 Python2 中的 unicode 字符串,或者根本不适用于 Python3。对于这些环境,您可以创建一个自定义类以传递给unicode.translate:
>>> class C:
... def __getitem__(self, i):
... if unichr(i).isdigit():
... return i
...
>>> u'+1-617.555/1212'.translate(C())
u'16175551212'
This works with non-ASCII digits, too:
这也适用于非 ASCII 数字:
>>> print u'+\u00b9-\uff1617.555/1212'.translate(C()).encode('utf-8')
16175551212
回答by dmg
I know this question has been answered, but just to point out what timeithas to say about the solutions efficiency. Using these parameters:
我知道这个问题已经得到解答,但只是想指出timeit关于解决方案效率的看法。使用这些参数:
size = 30
s = [str(random.randint(0, 9)) for i in range(size)] + (size/3) * ['-']
random.shuffle(s)
s = ''.join(['+'] + s)
timec = 1000
That is the "phone number" has 30 digits, 1 plus sing and 10 '-'. I've tested these approaches:
那就是“电话号码”有30个数字,1个加sing和10个'-'。我已经测试了这些方法:
def justdigits(s):
justdigitsres = ""
for char in s:
if char.isdigit():
justdigitsres += str(char)
return justdigitsres
re_compiled = re.compile(r'\D')
print('Filter: %ss' % timeit.Timer(lambda : ''.join(filter(str.isdigit, s))).timeit(timec))
print('GE: %ss' % timeit.Timer(lambda : ''.join(n for n in s if n.isdigit())).timeit(timec))
print('LC: %ss' % timeit.Timer(lambda : ''.join([n for n in s if n.isdigit()])).timeit(timec))
print('For loop: %ss' % timeit.Timer(lambda : justdigits(s)).timeit(timec))
print('RE: %ss' % timeit.Timer(lambda : re.sub(r'\D', '', s)).timeit(timec))
print('REC: %ss' % timeit.Timer(lambda : re_compiled.sub('', s)).timeit(timec))
print('Translate: %ss' % timeit.Timer(lambda : s.translate(None, '+-')).timeit(timec))
And came out with these results:
并得出了这些结果:
Filter: 0.0145790576935s
GE: 0.0185861587524s
LC: 0.0151798725128s
For loop: 0.0242128372192s
RE: 0.0120108127594s
REC: 0.00868797302246s
Translate: 0.00118899345398s
Apparently GEs and LCs are still slower than a regex or a compiled regex. And apparently my CPython 2.6.6 didn't optimize the string addition that much. translateappears to be the fastest (which is expected as the problem is stated as "ignore these two symbols", rather than "get these numbers" and I believe is quite low-level).
显然,GE 和 LC 仍然比正则表达式或编译的正则表达式慢。显然我的 CPython 2.6.6 并没有对字符串添加进行太多优化。translate似乎是最快的(这是预期的,因为问题被描述为“忽略这两个符号”,而不是“获取这些数字”,我认为这是相当低级的)。
And for size = 100:
而对于size = 100:
Filter: 0.0357120037079s
GE: 0.0465779304504s
LC: 0.0428011417389s
For loop: 0.0733139514923s
RE: 0.0213229656219s
REC: 0.0103371143341s
Translate: 0.000978946685791s
And for size = 1000:
而对于size = 1000:
Filter: 0.212141036987s
GE: 0.198996067047s
LC: 0.196880102158s
For loop: 0.365696907043s
RE: 0.0880808830261s
REC: 0.086804151535s
Translate: 0.00587010383606s
回答by Ajith
Did you try list(x)??
你试过 list(x) 吗??
y = '+123-456-7890'
c =list(y)
c
['+', '1', '2', '3', '-', '4', '5', '6', '-', '7', '8', '9', '0']
['+', '1', '2', '3', '-', '4', '5', '6', '-', '7', '8', '9', ' 0']

