Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定Python中每行是否有重复_Python_Sequence - Fatal编程技术网

确定Python中每行是否有重复

确定Python中每行是否有重复,python,sequence,Python,Sequence,我对Python还很陌生 我试图确定某个项目是否在列中重复 如果我有: x = [a, b, c, d, d, d, e, f, f] 我想得到: rep = [no, no, no, no, yes, yes, no, no, yes] 我可以使用for循环来执行此操作吗?或者应用一个函数? 任何指导都将不胜感激。从可复制的对象开始,例如 >>> x = ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f'] 你可以使用和做 如果你想

我对Python还很陌生

我试图确定某个项目是否在列中重复

如果我有:

x = [a, b, c, d, d, d, e, f, f]
我想得到:

rep = [no, no, no, no, yes, yes, no, no, yes]
我可以使用
for
循环来执行此操作吗?或者应用一个函数?
任何指导都将不胜感激。

从可复制的对象开始,例如

>>> x = ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f']
你可以使用和做

如果你想把布尔式变成是/否,那就这么做吧

>>> ['yes' if x[:i+1].count(el)>1 else 'no' for i,el in enumerate(x)]
['no', 'no', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']

从一个可复制的对象开始,比如

>>> x = ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f']
你可以使用和做

如果你想把布尔式变成是/否,那就这么做吧

>>> ['yes' if x[:i+1].count(el)>1 else 'no' for i,el in enumerate(x)]
['no', 'no', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']

请问这个家庭作业问题的网址是什么

#! /usr/bin/env python3


def y_or_n(bool):
    return 'yes' if bool else 'no'


def rep(xs):
    seen = set()
    ret = []
    for x in xs:
        ret.append(y_or_n(x in seen))
        seen.add(x)
    return ret

if __name__ == '__main__':
    print(rep('a b c d d d e f f'.split()))

请问这个家庭作业问题的网址是什么

#! /usr/bin/env python3


def y_or_n(bool):
    return 'yes' if bool else 'no'


def rep(xs):
    seen = set()
    ret = []
    for x in xs:
        ret.append(y_or_n(x in seen))
        seen.add(x)
    return ret

if __name__ == '__main__':
    print(rep('a b c d d d e f f'.split()))

当然,您所要做的就是迭代列表中的连续项对,并检查每对中的两个项是否相等。您可以使用一个名为
pairwise()
的便捷函数来完成此操作,它的实现在中给出,或者您可以直接从中使用它。您可以这样使用它:

for item1, item2 in pairwise(rep):
    # choose yes or no
实际上,我建议把它放在一个列表理解中,这样你就可以从一开始就把结果建立在一个列表中

[ (choose yes or no) for item1, item2 in pairwise(rep)]

然后,您必须在前面附加一个
'no'
,因为第一个元素之前没有任何元素可以相等。

当然,您所要做的就是迭代列表中的连续项目对,并检查每对中的两个项目是否相等。您可以使用一个名为
pairwise()
的便捷函数来完成此操作,它的实现在中给出,或者您可以直接从中使用它。您可以这样使用它:

for item1, item2 in pairwise(rep):
    # choose yes or no
实际上,我建议把它放在一个列表理解中,这样你就可以从一开始就把结果建立在一个列表中

[ (choose yes or no) for item1, item2 in pairwise(rep)]

然后,您必须在前面粘贴一个额外的
“no”
,因为第一个元素之前没有任何元素可以相等。

使用集合跟踪您看到的内容,并根据元素是否在集合中附加条件:

x = ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f']
is_dupes = []
seen = set()

for e in x:
    if e in seen:
        is_dupes.append('yes')
    else:
        is_dupes.append('no')
        seen.add(e)

is_dupes
# ['no', 'no', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']

使用集合跟踪所看到的内容,并根据元素是否在集合中附加条件:

x = ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'f']
is_dupes = []
seen = set()

for e in x:
    if e in seen:
        is_dupes.append('yes')
    else:
        is_dupes.append('no')
        seen.add(e)

is_dupes
# ['no', 'no', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']

这不是家庭作业,我正在处理一个需要大量操作的数据集。这不是家庭作业,我正在处理一个需要大量操作的数据集。我犯了一个错误。这里是真正的错误:
KeyError:'Level bor必须与name(None)相同
bor
是序列中的项目之一。我正在做:'datahitword['misread']=[datahitword['clean_-word'][:I+1]。枚举(datahitword['clean_-word'])]中I的计数(el)>1`@DanielVargas。怎么用?哪个错误?我已经用Python2.7和3.6测试了我的示例。但我还是想探索你的,拥有多种策略总是好的。我试过:
dup=[]dup=[datahitword['clean_-word'][:I+1].count(el)>1代表I,el在enumerate(datahitword['clean_-word'])]
,结果是:
keyrerror:'Level bor必须和name(None)
@DanielVargas我很抱歉,考虑到你的问题,我回答了你的问题,对吗?出于社区利益考虑,将您的问题作为新问题发布可能是一个好主意。如果你遇到这个问题,很可能其他人也会遇到。我犯了一个错误。这里是真正的错误:
KeyError:'Level bor必须与name(None)相同
bor
是序列中的项目之一。我正在做:'datahitword['misread']=[datahitword['clean_-word'][:I+1]。枚举(datahitword['clean_-word'])]中I的计数(el)>1`@DanielVargas。怎么用?哪个错误?我已经用Python2.7和3.6测试了我的示例。但我还是想探索你的,拥有多种策略总是好的。我试过:
dup=[]dup=[datahitword['clean_-word'][:I+1].count(el)>1代表I,el在enumerate(datahitword['clean_-word'])]
,结果是:
keyrerror:'Level bor必须和name(None)
@DanielVargas我很抱歉,考虑到你的问题,我回答了你的问题,对吗?出于社区利益考虑,将您的问题作为新问题发布可能是一个好主意。如果你遇到这个问题,很可能其他人也会遇到。