Friday 15 February 2013

Python - remove elements (foreign characters) from list -



Python - remove elements (foreign characters) from list -

i have python list foreign characters denoted unicode values:

python_list = ['to', 'shrink', u'\u7e2e\u3080', u'\u3061\u3062\u3080', 'chijimu', 'tizimu', 'tidimu', 'to', 'continue', u'\u7d9a\u304f', u'\u3064\u3065\u304f', 'tsuzuku', 'tuzuku', 'tuduku', u'\u30ed\u30fc\u30de\u5b57\uff08\u30ed\u30fc\u30de\u3058\uff09\u3068\u306f\u3001\u4eee\u540d\u6587\u5b57\u3092\u30e9\u30c6\u30f3\u6587\u5b57\u306b\u8ee2\u5199\u3059\u308b\u969b\u306e\u898f\u5247\u5168\u822c\uff08\u30ed\u30fc\u30de\u5b57\u8868\u8a18\u6cd5\uff09\u3001\u307e\u305f\u306f\u30e9\u30c6\u30f3\u6587\u5b57\u3067\u8868\u8a18\u3055\u308c\u305f\u65e5\u672c\u8a9e\uff08\u30ed\u30fc\u30de\u5b57\u3064\u3065\u308a\u306e\u65e5\u672c\u8a9e\uff09\u3092\u8868\u3059\u3002']

i need remove items '\u7e2e ' or other similar types . if item in list contains 1 ascii letter or word , shouldn't excluded. eg: 'china\u3062' should included. referred question , realized there's related values greater 128. tried different approaches one:

new_list = [item item in python_list if ord(item) < 128]

but returns error:

typeerror: ord() expected character, string of length 2 found

expected output:

new_list = ['to', 'shrink','chijimu', 'tizimu', 'tidimu', 'to', 'continue','tsuzuku', 'tuzuku', 'tuduku']

how should go one??

if wish maintain words have @ to the lowest degree 1 ascii letter in them code below this

from string import ascii_letters, punctuation python_list = ['to', 'shrink', u'\u7e2e\u3080', u'\u3061\u3062\u3080', 'chijimu','china,', 'tizimu', 'tidimu', 'to', 'continue', u'\u7d9a\u304f', u'\u3064\u3065\u304f', 'tsuzuku', 'tuzuku', 'tuduku', u'china\u3061'] allowed = set(ascii_letters) output = [word word in python_list if any(letter in allowed letter in word)] print(output) # ['to', # 'shrink', # 'chijimu', # 'china,', # 'tizimu', # 'tidimu', # 'to', # 'continue' # 'tsuzuku', # 'tuzuku', # 'tuduku', # 'china?']

this iterate through each letter of each word , if single letter contained in allowed add together word output list.

python list python-2.7 unicode ord

No comments:

Post a Comment