if list != [‘int’,’bool’,’real’]:
时间: 2023-09-26 07:09:01 浏览: 30
这段代码是在判断列表 list 中是否包含三个元素 'int'、'bool'、'real',如果不包含则条件成立。可以使用以下代码实现:
```
if not all(x in list for x in ['int', 'bool', 'real']):
# do something
```
或者:
```
if set(['int', 'bool', 'real']).issubset(list):
# do something
else:
# do something else
```
相关问题
将如下代码转成C#语言代码 import math def relatively_prime(a,b): # a > b while b != 0: temp = b b = a%b a = temp if a==1: return True else: return False def millerRabin(num): if num%2 ==0: return False flag = True Subsquare = 0 temp = num - 1 while True: temp = temp / 2 Subsquare += 1 if temp % 2 != 0: break b=[] # 存放所求整数(num)的原根 count = 0 for i in range(2,num-1):# g^(P-1) = 1 (mod P) if relatively_prime(num,i): b.append(i) count += 1 if count == 5: # 控制检测次数 break for i in b: two = 0 while True: if (i**temp)**(2**two)%num == 1 or (i**temp)**(2**two)%num == num-1: flag = True break else: two += 1 if two == Subsquare: flag = False break if flag == False: break # 如果存在一次不满足条件,则退出循环 return flag num = input(u"请输入要进行Miller-Rabin算法检测的数:") if millerRabin(num): print u"{0}大概率是素数".format(num) else: print u"{0}是合数 ".format(num)
using System;
namespace MillerRabin
{
class Program
{
static bool RelativelyPrime(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
if (a == 1)
{
return true;
}
else
{
return false;
}
}
static bool MillerRabin(int num)
{
if (num % 2 == 0)
{
return false;
}
bool flag = true;
int subsquare = 0;
int temp = num - 1;
while (true)
{
temp = temp / 2;
subsquare += 1;
if (temp % 2 != 0)
{
break;
}
}
int[] b = new int[5]; // 存放所求整数(num)的原根
int count = 0;
for (int i = 2; i < num - 1; i++)
{
if (RelativelyPrime(num, i))
{
b[count] = i;
count += 1;
if (count == 5) // 控制检测次数
{
break;
}
}
}
for (int i = 0; i < 5; i++)
{
int two = 0;
while (true)
{
if (Math.Pow(b[i], temp * Math.Pow(2, two)) % num == 1 || Math.Pow(b[i], temp * Math.Pow(2, two)) % num == num - 1)
{
flag = true;
break;
}
else
{
two += 1;
if (two == subsquare)
{
flag = false;
break;
}
}
}
if (flag == false)
{
break; // 如果存在一次不满足条件,则退出循环
}
}
return flag;
}
static void Main(string[] args)
{
Console.WriteLine("请输入要进行Miller-Rabin算法检测的数:");
int num = Convert.ToInt32(Console.ReadLine());
if (MillerRabin(num))
{
Console.WriteLine("{0}大概率是素数", num);
}
else
{
Console.WriteLine("{0}是合数", num);
}
Console.ReadLine();
}
}
}
template<typename OutputType> inline optional<OutputType> ValueComposer<OutputType>::compose(InternalStateContainer* internalState, const std::vector<std::string>* names, ValueComposerUserFunc<OutputType> getter) { //printf("ValueComposer compose 000-----.\n"); EStorageMap states; for (auto name : *names) { eframework::TypeID type = internalState->getType(name); if (type == TypeID::get<int>() && (internalState->get<int>(name)) != nullopt) { states.set<int>(name, *internalState->get<int>(name)); //printf("ValueComposer compose set---int--.\n"); } else if (type == TypeID::get<float>() && (internalState->get<float>(name)) != nullopt) { states.set<float>(name, *internalState->get<float>(name)); //printf("ValueComposer compose set---float--.\n"); } else if (type == TypeID::get<bool>() && (internalState->get<bool>(name)) != nullopt) { states.set<bool>(name, *internalState->get<bool>(name)); //printf("ValueComposer compose set---bool--.\n"); } else if (type == TypeID::get<std::string>() && (internalState->get<std::string>(name)) != nullopt) { states.set<std::string>(name, *internalState->get<std::string>(name)); //printf("ValueComposer compose set---string--.\n"); } else { //printf("ValueComposer compose set---nullopt--.\n"); return nullopt; } } //printf("ValueComposer compose 111-----.\n"); return getter(&states); }
这是一个ValueComposer类的成员函数模板`compose`的实现。该函数接受三个参数:一个指向`InternalStateContainer`对象的指针`internalState`,一个指向字符串向量的指针`names`,以及一个`ValueComposerUserFunc<OutputType>`类型的函数指针`getter`。
函数首先创建一个名为`states`的`EStorageMap`对象,用于存储状态值。然后遍历`names`向量中的每个字符串,通过调用`internalState`对象的`getType`函数获取相应名称的类型,并根据类型将对应的值存储到`states`对象中。
如果某个名称对应的类型不匹配或者值为`nullopt`,则返回空的`optional<OutputType>`。
最后,调用传入的`getter`函数指针,将`states`对象作为参数传递,并返回其结果。
注释部分是一些调试打印语句,可以用于调试程序时打印相关信息。