Python 列表追加2个反向元组
我有一张这样的清单Python 列表追加2个反向元组,python,list,tuples,Python,List,Tuples,我有一张这样的清单 Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)] >>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)] >>> [tuple(reversed(item)) for item in test] [(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)] >>> from collections import OrderedDi
Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> [tuple(reversed(item)) for item in test]
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(tuple(reversed(item)) for item in test).keys())
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
我想在每个元组中附加反向元素(我可能使用了错误的语言)
这是一个例子
我想附加这个
(5.0, 3.0), (7.0, 1.0), (4.0, 1.0)
如果可能,我不想在列表中添加重复项
我试过这个
Test.append(Test[i][1]),(Test[i][0]) # (where i = 0 to 1)
但是失败了要反转列表中的元素,只需使用函数,然后重新创建列表,如下所示
Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> [tuple(reversed(item)) for item in test]
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(tuple(reversed(item)) for item in test).keys())
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
如果可能,我不想在列表中添加重复项 由于您也想删除重复项,最好的选择是这样使用
Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> [tuple(reversed(item)) for item in test]
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(tuple(reversed(item)) for item in test).keys())
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
要反转列表中的元素,只需使用函数并重新创建列表,如下所示
Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> [tuple(reversed(item)) for item in test]
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(tuple(reversed(item)) for item in test).keys())
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
如果可能,我不想在列表中添加重复项 由于您也想删除重复项,最好的选择是这样使用
Test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> test = [(3.0, 5.0), (1.0, 7.0), (3.0, 4.0)]
>>> [tuple(reversed(item)) for item in test]
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(tuple(reversed(item)) for item in test).keys())
[(5.0, 3.0), (7.0, 1.0), (4.0, 3.0)]
但是我没有完全理解你对
i
的意思。但是一个简单的列表理解就可以了
myList = [(5.0, 3.0), (7.0, 1.0), (4.0, 3.0), (3.0, 5.0)]
myList.extend([(y, x) for x, y in myList if (y, x) not in myList])
或者只使用普通for循环。您可以附加到同一列表,也可以将项目添加到新列表,然后进行扩展。我个人更喜欢新列表,然后扩展,否则您将迭代新添加的项(除了效率之外,这没有什么区别)
但是我没有完全理解你对
i
的意思。但是一个简单的列表理解就可以了
myList = [(5.0, 3.0), (7.0, 1.0), (4.0, 3.0), (3.0, 5.0)]
myList.extend([(y, x) for x, y in myList if (y, x) not in myList])
或者只使用普通for循环。您可以附加到同一列表,也可以将项目添加到新列表,然后进行扩展。我个人更喜欢新列表,然后扩展,否则您将迭代新添加的项(除了效率之外,这没有什么区别)
注意:
Test.append(Test[i][1]),(Test[i][0])#(其中i=0到1)
失败,因为append()
只接受一个参数。(括号顶部不匹配)Test.append((Test[i][1],Test[i][0])#(其中i=0到1)
可以工作(通过整个元组)。注意:Test.append(Test[i][1]),(Test[i][0])#(其中i=0到1)
失败,因为append()
只接受一个参数。(括号顶部不匹配)Test.append((Test[i][1],Test[i][0])#(其中i=0到1)
将起作用(通过整个元组)。适用于测试中的所有项,但我希望逐项获取列表,以便我可以将其用于loop@AamirKhan您可以将结果分配给一个变量,并可以迭代该变量(测试[1])@AamirKhan如果不需要维护项目的顺序,您可以使用set
。适用于测试中的所有项目,但我希望逐项获取列表,以便我可以将其用于loop@AamirKhan您可以将结果分配给一个变量,并可以迭代该变量。Works Set.append(tuple(reversed(test[1]))@AamirKhan如果不需要维护项目顺序,可以使用set
。