我有一个moy代码中的元组:
('H', 'NNP')
这是代码:
# -*- coding: utf-8 -*-
from nltk.corpus import wordnet as wn
from nltk import pos_tag
import nltk
syno =[]
sentence = '''His father suggested he study to become a parson instead, but Darwin was far more inclined to study natural history.DarwinDar·win (där'wĭn),Charles Robert.1809-1882.British naturalist who revolutionized the study of biology with his theory ofevolutionbased on natural selection
Like several scientists before him, Darwin believed all the life on earth evolved (developed gradually) over millions of years from a few common ancestors.'''
sent = pos_tag(sentence)
alpha = [s for s in sent if s[1] == 'NNP']
for i in range(0,len(alpha)-1):
print alpha[i] #return the tuple
我想从中删除H.我怎么能这样做?
答案 0 :(得分:1)
元组是不可变的,所以你必须创建一个新元素:
>>> t = ('H', 'NNP')
>>> tuple(x for x in t if x != 'H')
('NNP',)
>>> z = tuple(x for x in t if x == 'H')
>>> z
('H',)
>>> z[0]
'H'
>>>
答案 1 :(得分:0)
你不能改变元组,它们是“不可变的”
如果您改为使用类似列表的可变数据结构,则可以更改它们
>>>a = ['H', 'NNP']
>>>a[0] = 'J' # this changes the 'H' to a 'J'
>>>print a
['J', 'NNP']
如果您因某种原因需要将其中一个转换为另一个,则可以执行myTuple = tuple(myList)
或myList = list(myTuple)
答案 2 :(得分:0)
>>> x = ('H', 'NNP')
>>> x = tuple(list(x)[1:])
>>> x
('NNP',)