从 python 列表中删除 'u'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30975911/
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
Remove 'u' from a python list
提问by station
I have a python list of list as follows. I want to flatten it to a single list.
我有一个 python 列表列表如下。我想将其展平为一个列表。
l = [u'[190215]']
I am trying.
我在尝试。
l = [item for value in l for item in value]
It turns the list to [u'[', u'1', u'9', u'0', u'2', u'1', u'5', u']']
它将列表变为 [u'[', u'1', u'9', u'0', u'2', u'1', u'5', u']']
How to remove the u
from the list.
如何u
从列表中删除。
采纳答案by u1860929
In your current code, you are iterating on a string, which represents a list, hence you get the individual characters.
在您当前的代码中,您正在迭代一个表示列表的字符串,因此您将获得单个字符。
>>> from ast import literal_eval
>>> l = [u'[190215]']
>>> l = [item for value in l for item in value]
>>> l
[u'[', u'1', u'9', u'0', u'2', u'1', u'5', u']']
Seems to me, you want to convert the inner string representation of list, to a flattened list, so here you go:
在我看来,您想将列表的内部字符串表示形式转换为扁平列表,因此您可以这样做:
>>> l = [u'[190215]']
>>> l = [item for value in l for item in literal_eval(value)]
>>> l
[190215]
The above will work only when all the inner lists are strings:
以上仅当所有内部列表都是字符串时才有效:
>>> l = [u'[190215]', u'[190216, 190217]']
>>> l = [item for value in l for item in literal_eval(value)]
>>> l
[190215, 190216, 190217]
>>> l = [u'[190215]', u'[190216, 190217]', [12, 12]]
>>> l = [item for value in l for item in literal_eval(value)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
回答by jamylak
The u
means a unicode
string which should be perfectly fine to use.
But if you want to convert unicode
to str
(which just represents plain bytes in Python 2) then you may encode
it using a character encoding such as utf-8
.
这u
意味着一个unicode
应该完全可以使用的字符串。但是如果你想转换unicode
为str
(它只代表 Python 2 中的纯字节),那么你可以encode
使用诸如utf-8
.
>>> items = [u'[190215]']
>>> [item.encode('utf-8') for item in items]
['[190215]']
回答by Kasramvd
You can convert your unicode to normal string with str
:
您可以使用以下命令将您的 unicode 转换为普通字符串str
:
>>> list(str(l[0]))
['[', '1', '9', '0', '2', '1', '5', ']']
回答by Aneesh R S
use [str(item) for item in list]
用 [str(item) for item in list]
example
例子
>>> li = [u'a', u'b', u'c', u'd']
>>> print li
[u'a', u'b', u'c', u'd']
>>> li_u_removed = [str(i) for i in li]
>>> print li_u_removed
['a', 'b', 'c', 'd']
回答by Kalana
I think this issue occurred in python 2.7
but in latest python version udid not displayed when it run
我认为这个问题发生在python 2.7
但在最新的 python 版本中你没有在运行时显示
l = [u'[190215]']
l = [item for value in l for item in value]
print(l)
output -:['[', '1', '9', '0', '2', '1', '5', ']']
输出 -:['[', '1', '9', '0', '2', '1', '5', ']']
If you want to concatenatestring items in a list into a single string, you can try this code
如果要将列表中的字符串项连接成单个字符串,可以尝试此代码
l = [u'[190215]']
l = [item for value in l for item in value]
l = ''.join(l)
print(l)
output -:[190215]
输出 -:[190215]