Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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转换为c#_C#_Python_Helper - Fatal编程技术网

将python转换为c#

将python转换为c#,c#,python,helper,C#,Python,Helper,Python代码: arr = list(input().split(' ')) print("no" if len([x for x in arr if arr.count(x) > 1]) else "yes") 我做了一个C#但它一直循环着是和否 String word = Console.ReadLine(); int count; word = word.ToLower(); String[] words = word.Sp

Python代码:

    arr = list(input().split(' '))

    print("no" if len([x for x in arr if arr.count(x) > 1]) else "yes")
我做了一个C#但它一直循环着是和否

    String word = Console.ReadLine();
    int count;

    word = word.ToLower();

    String[] words = word.Split(' ');

    for(int i = 0; i < words.Length; i++)
    {
        count = 1;
        for(int j = i+1; j < words.Length; j++)
        {
            if(count > 1 && words[i] != "")
            {
                Console.WriteLine("no");
            }else
            {
                Console.WriteLine("yes");
            }
        }
    }
String word=Console.ReadLine();
整数计数;
word=word.ToLower();
String[]words=word.Split(“”);
for(int i=0;i1&&words[i]!=“”)
{
控制台。写入线(“否”);
}否则
{
控制台。WriteLine(“是”);
}
}
}

如果您想检查重复项,您可以尝试一个简单的Linq(如果所有字符串都是不同的,则我们将
设置为“是”
设置为“否”
):

无Linq解决方案:

String word = Console.ReadLine();

bool hasDuplicates = false;

HashSet<string> unique = new HashSet<string>();

foreach(string w in word.Split(' '))
  if (!unique.Add(w)) {
    hasDuplicates = true;

    break;
  }

Console.WriteLine(!hasDuplicates ? "yes" : "no");
String word=Console.ReadLine();
bool hasDuplicates=false;
HashSet unique=新HashSet();
foreach(word.Split(“”)中的字符串w)
如果(!unique.Add(w)){
hasDuplicates=true;
打破
}
Console.WriteLine(!hasDuplicates?“是”:“否”);

这可能不是解决问题的最佳方法,但这是最接近当前Python代码的方法

String[] words = word.Split(' ');

Console.WriteLine(words.Any(w => words.Count(w2 => w == w2) > 1) ? "no" : "yes");
或者我猜这将是一个更准确的翻译

Console.WriteLine(words.Count(w => words.Count(w2 => w == w2) > 1) > 0 ? "no" : "yes");

这是正确的代码

    String word = Console.ReadLine();     
    String[] words = word.ToLower().Split(' ');
    var result = true;
    for(int i = 0; i < words.Length; i++)
    {
        if(words.Count(x=> x == words[i]) > 1)
            result = false; 
    }
    Console.WriteLine(result?"yes":"no");
String word=Console.ReadLine();
String[]words=word.ToLower().Split(“”);
var结果=真;
for(int i=0;ix==words[i])>1)
结果=假;
}
Console.WriteLine(结果?“是”:“否”);

请问原始问题是什么?此代码的作用是什么?搜索副本
word.ToLower().Split(“”).GroupBy(x=>x).Any(g=>g.Count()>1)
如果有任何单词出现多次,将返回true。它通过将单词分组,然后计算每组的项目来实现这一点。具体目标是什么?我们可以看到Python代码确实只打印一次,而C#代码打印多次,因为它是在一个循环中。如果没有单词重复,则输出为“是”,如果一个或多个单词重复,则输出为“否”。它正在搜索插入字符串中的重复单词@DmitryBychenko@SomeStudentPython也循环了两次。一次是因为
for
,一次是因为
arr.count(x)
for N^2复杂性不,python只打印了一次。如果
juharr我知道,你可以提前退出
for
循环,但我想尽我所能,因为OP显然是个新手。一个好的解决方案是一个基于散列集的人张贴在这里
    String word = Console.ReadLine();     
    String[] words = word.ToLower().Split(' ');
    var result = true;
    for(int i = 0; i < words.Length; i++)
    {
        if(words.Count(x=> x == words[i]) > 1)
            result = false; 
    }
    Console.WriteLine(result?"yes":"no");