简体   繁体   English

从 python 列表中删除 'u'

[英]Remove 'u' from a python list

I have a python list of list as follows.我有一个 python 列表列表如下。 I want to flatten it to a single list.我想将其展平为一个列表。

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

The u means a unicode string which should be perfectly fine to use. u表示一个unicode字符串,应该完全可以使用。 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 .但是,如果要转换unicodestr (这只是表示在Python 2的平纹字节)则可能encode使用作为编码这种字符它utf-8

>>> items = [u'[190215]']
>>> [item.encode('utf-8') for item in items]
['[190215]']

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']

You can convert your unicode to normal string with str :您可以使用str将您的 unicode 转换为普通字符串:

>>> list(str(l[0]))
['[', '1', '9', '0', '2', '1', '5', ']']

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

I think this issue occurred in python 2.7 but in latest python version u did 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 concatenate string 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM