简体   繁体   English

如何在python中使引号成为字符串

[英]how to make quotes a string in python

how to process quotes issue in this question? 如何处理此问题中的报价问题?

class Solution:
    # @param strs: A list of strings
    # @return: A list of strings
    def anagrams(self, strs):
        # write your code here
        dic = {}
        result = []

        for s in strs:
            if s == '""':
                key ='""'
            else:
                key = ''.join(sorted(s))
            dic[key].add(s)
        for key, value in dic.iteritems():
            if len(value) > 1:
                result.append(value)
        return result

Input 输入

["",""]

Expected 预期

["",""]

Error Message 错误信息

Traceback (most recent call last): File "Main.py", line 7, in ans = Solution().anagrams(strs) File "Solution.py", line 14, in anagrams dic[key].add(s) KeyError: '' EXITCODE=1

When you write ["",""] , the elements of the input array are empty strings -- the "" are not part of the string contents. 当您编写["",""] ,输入数组的元素为空字符串- ""不属于字符串内容。 So s = '""' will not match them. 因此s = '""'将不匹配它们。 You need to write: 您需要写:

if s == '':

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

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