Преобразуйте значения в кортежах из юникода в строку, когда кортежи находятся в списке списка в Python 2.7. - PullRequest
0 голосов
/ 08 ноября 2019

Как я могу преобразовать значения в кортежах из юникода в строку, когда кортежи находятся в списке списка в Python 2.7

Вот пример для того же -

Из

 unicode_list=[[(u'Mr.', u'UNK'), (u'Vinken', u'UNK'), (u'is', u'UNK'), (u'chairman', u'UNK'), (u'of', u'UNK'), (u'Elsevier', u'UNK'), (u'N.V.', u'UNK'), (u',', u'UNK'), (u'the', u'UNK'), (u'Dutch', u'UNK'), (u'publishing', u'UNK'), (u'group', u'UNK'), (u'.', u'UNK')], [(u'Rudolph', u'UNK'), (u'Agnew', u'UNK'), (u',', u'UNK'), (u'55', u'UNK'), (u'years', u'UNK'), (u'old', u'UNK'), (u'and', u'UNK'), (u'former', u'UNK'), (u'chairman', u'UNK'), (u'of', u'UNK'), (u'Consolidated', u'UNK'), (u'Gold', u'UNK'), (u'Fields', u'UNK'), (u'PLC', u'UNK'), (u',', u'UNK'), (u'was', u'UNK'), (u'named', u'UNK'), (u'*-1', u'UNK'), (u'a', u'UNK'), (u'nonexecutive', u'UNK'), (u'director', u'UNK'), (u'of', u'UNK'), (u'this', u'UNK'), (u'British', u'UNK'), (u'industrial', u'UNK'), (u'conglomerate', u'UNK'), (u'.', u'UNK')], [(u'A', u'UNK'), (u'form', u'UNK'), (u'of', u'UNK'), (u'asbestos', u'UNK'), (u'once', u'UNK'), (u'used', u'UNK'), (u'*', u'UNK'), (u'*', u'UNK'), (u'to', u'UNK'), (u'make', u'UNK'), (u'Kent', u'UNK'), (u'cigarette', u'UNK'), (u'filters', u'UNK'), (u'has', u'UNK'), (u'caused', u'UNK'), (u'a', u'UNK'), (u'high', u'UNK'), (u'percentage', u'UNK'), (u'of', u'UNK'), (u'cancer', u'UNK'), (u'deaths', u'UNK'), (u'among', u'UNK'), (u'a', u'UNK'), (u'group', u'UNK'), (u'of', u'UNK'), (u'workers', u'UNK'), (u'exposed', u'UNK'), (u'*', u'UNK'), (u'to', u'UNK'), (u'it', u'UNK'), (u'more', u'UNK'), (u'than', u'UNK'), (u'30', u'UNK'), (u'years', u'UNK'), (u'ago', u'UNK'), (u',', u'UNK'), (u'researchers', u'UNK'), (u'reported', u'UNK'), (u'0', u'UNK'), (u'*T*-1', u'UNK'), (u'.', u'UNK')], [(u'The', u'UNK'), (u'asbestos', u'UNK'), (u'fiber', u'UNK'), (u',', u'UNK'), (u'crocidolite', u'UNK'), (u',', u'UNK'), (u'is', u'UNK'), (u'unusually', u'UNK'), (u'resilient', u'UNK'), (u'once', u'UNK'), (u'it', u'UNK'), (u'enters', u'UNK'), (u'the', u'UNK'), (u'lungs', u'UNK'), (u',', u'UNK'), (u'with', u'UNK'), (u'even', u'UNK'), (u'brief', u'UNK'), (u'exposures', u'UNK'), (u'to', u'UNK'), (u'it', u'UNK'), (u'causing', u'UNK'), (u'symptoms', u'UNK'), (u'that', u'UNK'), (u'*T*-1', u'UNK'), (u'show', u'UNK'), (u'up', u'UNK'), (u'decades', u'UNK'), (u'later', u'UNK'), (u',', u'UNK'), (u'researchers', u'UNK'), (u'said', u'UNK'), (u'0', u'UNK'), (u'*T*-2', u'UNK'), (u'.', u'UNK')]]

до

 ascii_list=[[('Mr.', 'NOUN'), ('Vinken', 'NOUN'), ('is', 'VERB'), ('chairman', 'NOUN'), ('of', 'ADP'), ('Elsevier', 'NOUN'), ('N.V.', 'NOUN'), (',', '.'), ('the', 'DET'), ('Dutch', 'NOUN'), ('publishing', 'VERB'), ('group', 'NOUN'), ('.', '.')], [('Rudolph', 'NOUN'), ('Agnew', 'NOUN'), (',', '.'), ('55', 'NUM'), ('years', 'NOUN'), ('old', 'ADJ'), ('and', 'CONJ'), ('former', 'ADJ'), ('chairman', 'NOUN'), ('of', 'ADP'), ('Consolidated', 'NOUN'), ('Gold', 'NOUN'), ('Fields', 'NOUN'), ('PLC', 'NOUN'), (',', '.'), ('was', 'VERB'), ('named', 'VERB'), ('*-1', 'X'), ('a', 'DET'), ('nonexecutive', 'ADJ'), ('director', 'NOUN'), ('of', 'ADP'), ('this', 'DET'), ('British', 'ADJ'), ('industrial', 'ADJ'), ('conglomerate', 'NOUN'), ('.', '.')], [('A', 'DET'), ('form', 'NOUN'), ('of', 'ADP'), ('asbestos', 'NOUN'), ('once', 'ADV'), ('used', 'VERB'), ('*', 'X'), ('*', 'X'), ('to', 'PRT'), ('make', 'VERB'), ('Kent', 'NOUN'), ('cigarette', 'NOUN'), ('filters', 'NOUN'), ('has', 'VERB'), ('caused', 'VERB'), ('a', 'DET'), ('high', 'ADJ'), ('percentage', 'NOUN'), ('of', 'ADP'), ('cancer', 'NOUN'), ('deaths', 'NOUN'), ('among', 'ADP'), ('a', 'DET'), ('group', 'NOUN'), ('of', 'ADP'), ('workers', 'NOUN'), ('exposed', 'VERB'), ('*', 'X'), ('to', 'PRT'), ('it', 'PRON'), ('more', 'ADV'), ('than', 'ADP'), ('30', 'NUM'), ('years', 'NOUN'), ('ago', 'ADP'), (',', '.'), ('researchers', 'NOUN'), ('reported', 'VERB'), ('0', 'X'), ('*T*-1', 'X'), ('.', '.')], [('The', 'DET'), ('asbestos', 'NOUN'), ('fiber', 'NOUN'), (',', '.'), ('crocidolite', 'NOUN'), (',', '.'), ('is', 'VERB'), ('unusually', 'ADV'), ('resilient', 'ADJ'), ('once', 'ADP'), ('it', 'PRON'), ('enters', 'VERB'), ('the', 'DET'), ('lungs', 'NOUN'), (',', '.'), ('with', 'ADP'), ('even', 'ADV'), ('brief', 'ADJ'), ('exposures', 'NOUN'), ('to', 'PRT'), ('it', 'PRON'), ('causing', 'VERB'), ('symptoms', 'NOUN'), ('that', 'DET'), ('*T*-1', 'X'), ('show', 'VERB'), ('up', 'PRT'), ('decades', 'NOUN'), ('later', 'ADJ'), (',', '.'), ('researchers', 'NOUN'), ('said', 'VERB'), ('0', 'X'), ('*T*-2', 'X'), ('.', '.')]]

1 Ответ

1 голос
/ 08 ноября 2019

Вы можете выполнить преобразование с пониманием вложенного списка:

>>> ascii_list  = [[tuple([str(x) for x in tpl]) 
...  for tpl in sublist]
...  for sublist in unicode_list]

Это создает новый список списков - исходные кортежи не могут быть преобразованы, поскольку они, по сути, неизменны.

...