是否有内置功能可以从Python中的列表中删除重复项,同时保留顺序?我知道我可以使用一个集来删除重复项,但这会破坏原始顺序.我也知道我可以像这样滚动自己:
def uniq(input):
output = []
for x in input:
if x not in output:
output.append(x)
return output
Run Code Online (Sandbox Code Playgroud)
但是如果可能的话,我想利用内置或更多的Pythonic习语.
我想从以下列表中获取唯一值:
['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
Run Code Online (Sandbox Code Playgroud)
我需要的输出是:
['nowplaying', 'PBS', 'job', 'debate', 'thenandnow']
Run Code Online (Sandbox Code Playgroud)
此代码有效:
output = []
for x in trends:
if x not in output:
output.append(x)
print(output)
Run Code Online (Sandbox Code Playgroud)
我应该使用更好的解决方案吗?
如何检查数组中的任何字符串是否存在于另一个字符串中?
喜欢:
a = ['a', 'b', 'c']
str = "a123"
if a in str:
print "some of the strings found in str"
else:
print "no strings found in str"
Run Code Online (Sandbox Code Playgroud)
该代码不起作用,只是为了展示我想要实现的目标.
假设我有两个列表,l1并且l2.我想执行l1 - l2,返回所有l1不在的元素l2.
我可以想到一个简单的循环方法来做到这一点,但这将是非常低效的.什么是pythonic和有效的方法呢?
举个例子,如果我有l1 = [1,2,6,8] and l2 = [2,3,5,8],l1 - l2应该回来[1,6]
假设我有一个词典列表:
[
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
Run Code Online (Sandbox Code Playgroud)
我需要获取一个唯一的字典列表(删除重复的字典):
[
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
]
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我在Python中实现这一目标的最有效方法吗?
我有一个Python列表:
k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
Run Code Online (Sandbox Code Playgroud)
我想从中删除重复的元素.如果它是一个正常的列表而不是我可以使用的列表set.但不幸的是,该列表不可清除,也无法制作一组列表.只有元组.所以我可以将所有列表转换为元组,然后使用set并返回列表.但这并不快.
如何以最有效的方式完成?
上面列出的结果应该是:
k = [[5, 6, 2], [1, 2], [3], [4]]
Run Code Online (Sandbox Code Playgroud)
我不关心保留秩序.
注意:这个问题很相似,但不是我需要的.搜索了SO但没有找到确切的重复.
标杆:
import itertools, time
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print '[%s]' % self.name,
print 'Elapsed: %s' % (time.time() - self.tstart)
k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [5, 2], [6], …Run Code Online (Sandbox Code Playgroud) 所以我试图让这个程序要求用户输入并将值存储在数组/列表中.
然后,当输入空行时,它将告诉用户这些值中有多少是唯一的.
我建立这是出于现实生活的原因而不是问题集.
enter: happy
enter: rofl
enter: happy
enter: mpg8
enter: Cpp
enter: Cpp
enter:
There are 4 unique words!
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
# ask for input
ipta = raw_input("Word: ")
# create list
uniquewords = []
counter = 0
uniquewords.append(ipta)
a = 0 # loop thingy
# while loop to ask for input and append in list
while ipta:
ipta = raw_input("Word: ")
new_words.append(input1)
counter = counter + 1
for p in uniquewords:
Run Code Online (Sandbox Code Playgroud)
..那就是我到目前为止所做的一切.
我不确定如何计算列表中唯一的单词数?
如果有人可以发布解决方案,以便我可以从中学习,或者至少告诉我它会如何变得更好,谢谢!
说我在这里有这个列表:
list = [a, b, c, d, e, f, g]
Run Code Online (Sandbox Code Playgroud)
我如何删除说索引2, 3, 4,5同时?
pop不接受多个值.我怎么做呢?
给定一个字符串列表,我想按字母顺序对其进行排序并删除重复项.我知道我可以这样做:
from sets import Set
[...]
myHash = Set(myList)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何按字母顺序从散列中检索列表成员.
我没有和哈希结婚,所以任何方法都可以实现.此外,性能不是问题,因此我更倾向于使用代码清楚地表达一个快速但更不透明的解决方案.