找到你要的答案

Q:delete item in a tuple in python

Q:在Python中的元组删除项目

I have a file in this format

[('misure', 'di', 'protezione'), ('libertà', 'di', 'espressione', 'di', 'popolo')]

What I want is to eliminate from the tuple the preposition (di), and return the result in the same format. So I created this function to do this

lista = myfilelist
prep = prepositionfile
li = ast.literal_eval(lista)

 for i in li:
    word = str(i)
    ll = word.split("', '")
    for w in ll:
        lll= w.strip("('')")
        if lll in prep:
            i = word.replace(lll, "")
            i.strip('')

     print (nonlem(li))

but it return the tuple in this format

[('misure', '', 'protezione'), ('libertà', '', 'espressione', '', 'popolo')]

The output needed is

[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')] ### without ''

我有这个格式的文件

[('misure', 'di', 'protezione'), ('libertà', 'di', 'espressione', 'di', 'popolo')]

我想要的是从元组中删除介词(DI),并返回结果相同的格式。所以我创建这个函数来做这个

lista = myfilelist
prep = prepositionfile
li = ast.literal_eval(lista)

 for i in li:
    word = str(i)
    ll = word.split("', '")
    for w in ll:
        lll= w.strip("('')")
        if lll in prep:
            i = word.replace(lll, "")
            i.strip('')

     print (nonlem(li))

但它返回这个格式的元组

[('misure', '', 'protezione'), ('libertà', '', 'espressione', '', 'popolo')]

所需的输出是

[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')] ### without ''
answer1: 回答1:

You can use ast.literal_eval to turn the string into a list of tuples

>>> from ast import literal_eval
>>> l = literal_eval("[('misure', 'di', 'protezione'), ('libertà', 'di', 'espressione')]")

Then a list comprehension to make new tuples out of the first and last element

>>> [(i[0], i[-1]) for i in l]
[('misure', 'protezione'), ('libertà', 'espressione')]

Then str to create a string from the list of tuples

>>> str([(i[0], i[-1]) for i in l])
"[('misure', 'protezione'), ('libertà', 'espressione')]"

Edit
If you want to remove all instances of the string 'di' it is the same idea, you can use a list comprehension

>>> [tuple(i for i in j if i != 'di') for j in l]
[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')]

Edit 2
Even more generally, if you have a set of prepositions you want to exclude

>>> prepositions = {'di', 'a', 'al'}
>>> [tuple(i for i in j if i not in prepositions) for j in l]
[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')]

你可以使用ast.literal_eval把字符串到一个列表中的元组

>>> from ast import literal_eval
>>> l = literal_eval("[('misure', 'di', 'protezione'), ('libertà', 'di', 'espressione')]")

然后一个列表从第一个和最后一个元素进行新的元组

>>> [(i[0], i[-1]) for i in l]
[('misure', 'protezione'), ('libertà', 'espressione')]

然后,STR创建元组列表字符串

>>> str([(i[0], i[-1]) for i in l])
"[('misure', 'protezione'), ('libertà', 'espressione')]"

Edit
If you want to remove all instances of the string 'di' it is the same idea, you can use a list comprehension

>>> [tuple(i for i in j if i != 'di') for j in l]
[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')]

Edit 2
Even more generally, if you have a set of prepositions you want to exclude

>>> prepositions = {'di', 'a', 'al'}
>>> [tuple(i for i in j if i not in prepositions) for j in l]
[('misure', 'protezione'), ('libertà', 'espressione', 'popolo')]
answer2: 回答2:

Tuple are immutable so you cannot change them once created. You can create new ones. Try this:

[tuple(j for j in i if 'di' not in j) for i in a]

Here is the working demo.

元组是不可变的,所以你不能改变它们一旦创建。您可以创建新的。试试这个:

[tuple(j for j in i if 'di' not in j) for i in a]

这里是工作演示。

answer3: 回答3:
[tuple(j for j in i if 'di' != j) for i in li]

I modified sam2090 code, since the string 'di' can be part of a word like 'dim'

[tuple(j for j in i if 'di' != j) for i in li]

我修改sam2090代码,因为字符串“地”可以是一个词“暗淡”的一部分

python  list  python-3.x  tuples