简体   繁体   English

查找 python 中的所有反向对

[英]Find all reverse pairs in python

Define the function rev_pair(slist), whose argument is a list and all elements of the list are strings.定义function rev_pair(slist),其参数是一个列表,列表的所有元素都是字符串。 This function returns another list, and all elements of the returned list are a tuple of two strings from slist, and inverted from each other.这个 function 返回另一个列表,返回列表的所有元素都是来自 slist 的两个字符串的元组,并且相互反转。 The string that appears first in slist is on the left side of this tuple, and the one that appears later is on the right side.在 slist 中首先出现的字符串在该元组的左侧,后面出现的字符串在右侧。 The tuple of strings that appear first in slist on the left appears first in the list of returned values, and the same strings appear only once in the returned values.左边 slist 中最先出现的字符串元组在返回值列表中最先出现,相同的字符串在返回值中只出现一次。

All characters in the string are either case alphabet or 0 to 9.字符串中的所有字符都是大小写字母或 0 到 9。

[example] [例子]

input : ['abc', 'ded', 'cba', 'ffa']输入:['abc','ded','cba','ffa']

output : [('abc', 'cba'), ('ded', 'ded')] output :[('abc','cba'),('ded','ded')]

here is my code but still Wrong Answer这是我的代码,但仍然是错误的答案

def rev_pair(slist):
    def isrev(s1,s2):
        if s2[::-1] == s1:
            return True
        else:
            return False
    res = []
    for i in range(len(slist)):
        for u in range(i, len(slist)):
            if isrev(slist[i], slist[u]):
                if not (slist[i], slist[u]) in res:
                    res.append((slist[i], slist[u]))
                    break
    return res
slist = []
exec('slist=' + input())
print(rev_pair(slist))

Here's a short version of what you are trying to achieve:这是您要实现的目标的简短版本:

def revevers_list(l):
    new_l = []
    for item in l:
        if len(item) == len(set(item)):
            new_l.append((item, item[::-1]))
    return new_l

slist = input().split(",")
print(revevers_list(slist))

Test result:测试结果:

>> python app.py
abc, def, ddt
[('abc', 'cba'), (' def', 'fed ')]

try something like this尝试这样的事情

def rev_pair(input_list):
    """
    Function that checks coincidence between input list elements and reversed string of element in same list.
    """
    output = []
    for string in input_list:
        reversed_string = string[::-1]
        for string_ in INPUT:
            if reversed_string == string_:
                output.extend([(string, reversed_string)])
    return output

out = rev_pair(input_list = ['abc', 'ded', 'cba', 'ffa'])
out

results in结果是

[('abc', 'cba'), ('ded', 'ded'), ('cba', 'abc')]

I think this version is a bit more pythonic:我认为这个版本有点pythonic:

def find_reversed_pairs(data):
    values_count = dict()

    for value in data:
        reversed_value = value[::-1]
        values_count[value] = values_count.get(value, 0) + 1
        if reversed_value in values_count:
            values_count[reversed_value] = values_count.get(reversed_value, 0) + 1

    return [(val, val[::-1]) for val, count in values_count.items() if count >= 2]

Here's a sample output:这是一个示例 output:

['abc', 'def', 'cba', 'ffa', 'fed'] => [('abc', 'cba'), ('def', 'fed')]
['abc', 'ded', 'cba', 'ffa'] => [('abc', 'cba'), ('ded', 'ded')]

The problems all come from the requirement for uniqueness:问题都来自对唯一性的要求:

Define the function rev_pair(slist), whose argument is a list and all elements of the list are strings.定义function rev_pair(slist),其参数是一个列表,列表的所有元素都是字符串。 This function returns another list, and all elements of the returned list are a tuple of two strings from slist, and inverted from each other.这个 function 返回另一个列表,返回列表的所有元素都是来自 slist 的两个字符串的元组,并且相互反转。 The string that appears first in slist is on the left side of this tuple, and the one that appears later is on the right side.在 slist 中首先出现的字符串在该元组的左侧,后面出现的字符串在右侧。 The tuple of strings that appear first in slist on the left appears first in the list of returned values, and the same strings appear only once in the returned values.左边 slist 中最先出现的字符串元组在返回值列表中最先出现,相同的字符串在返回值中只出现一次。

In my code, if the input is ['abc','cba','cba','abc'] , the output would be [('abc','cba'), ('cba', abc')] which is wrong according to the requirement.在我的代码中,如果输入是['abc','cba','cba','abc'] ,则 output 将是[('abc','cba'), ('cba', abc')]根据要求这是错误的。 Then simply remove the second tuple can solve this problem.然后简单地去掉第二个元组就可以解决这个问题。

Here is the new code.(Not efficient still)这是新代码。(仍然没有效率)

def rev_pair(slist):
    def isrev(s1,s2):
        if s2[::-1] == s1:
            return True
        else:
            return False
    res = []
    for i in range(len(slist)):
        for u in range(i, len(slist)):
            if isrev(slist[i], slist[u]):
                if (slist[i], slist[u]) in res or (slist[u], slist[i]) in res:  # new modification
                    continue
                else:
                    res.append((slist[i], slist[u]))
                    break
    return res


slist = []
exec('slist=' + input())
print(rev_pair(slist))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM