只有当它以特定单词开头时才打印下一行(print next line only if it begins with a specific word)
我对python有点新意,我想知道是否有人可以提供帮助。 基本上我正在读取文件的内容,当我找到单词“prb”时,我想使用next()函数检查下一行,如果它以单词“rt”开头,我想打印两行。 到目前为止,我写了这段代码:
with open('/home/user/Desktop/3rdstep.txt', 'r') as f: f.readline() for line in f: if "prb" in line: try: myword = next(f) if "rt" in myword: print(line.strip()) print(myword) except: print("pass")
这很好但唯一的问题是它会随机跳过“rt”字,原因我不知道。 任何人都可以帮忙或让别人做类似的事吗?
谢谢
I am a bit new to python and I was wondering if anyone can help. Basically I am reading contents of a file and when I find the word "prb" I want to check the next line using the next() function and if it starts with the word "rt", i want to print both lines. So far I wrote this piece of code:
with open('/home/user/Desktop/3rdstep.txt', 'r') as f: f.readline() for line in f: if "prb" in line: try: myword = next(f) if "rt" in myword: print(line.strip()) print(myword) except: print("pass")
This works fine but the only problem is that it skips randomly "rt" words for a reason I don't know. Can anyone help please or have someone done something similar?
Thanks
原文:https://stackoverflow.com/questions/37032554
最满意答案
std::cout << std::to_string(2) << std::endl; for (unsigned int i = 3; i<240; i += 2) { unsigned int j = 3; int sq = sqrt(i); for (; j <= sq; j += 2) if (!(i%j)) break; if (j>sq) std::cout << std::to_string(i) << std::endl; }
首先,主要定义:素数(或素数)是大于1的自然数,除了1和自身之外没有正除数。
所以你可以跳过所有的偶数(因此... i + = 2)。 此外,没有必要尝试划分大于sqrt(i)的数字,因为它将具有小于sqrt(i)的除数,并且代码找到并移动到下一个数字。 只考虑奇数,意味着我们可以跳过偶数作为除数(因此... j + = 2)。
在你的代码中,显然有初学者错误,比如(x = 0)而不是x == 0。 而且逻辑也不能说服。 我同意@NathanOliver,你需要学习使用调试器来查找所有错误。 其余的,与研究祝你好运。
std::cout << std::to_string(2) << std::endl; for (unsigned int i = 3; i<240; i += 2) { unsigned int j = 3; int sq = sqrt(i); for (; j <= sq; j += 2) if (!(i%j)) break; if (j>sq) std::cout << std::to_string(i) << std::endl; }
first of all, the prime definition: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
so you can skip all the even numbers (and hence ... i+=2). Moreover no point to try to divide for a number greater than sqrt(i), because then it will have a divisor less than sqrt(i) and the code finds that and move to the next number. Considering only odd numbers, means that we can skip even numbers as divisors (hence ... j+=2).
In your code there are clearly beginner errors, like (x = 0) instead of x==0. but also the logic doesn't convince. I agree with @NathanOliver, you need to learn to use a debugger to find all the errors. For the rest, good luck with the studies.
相关问答
更多-
Prime数字的Java程序(Java Program for Prime numbers)[2022-08-15]
您的示例解决方案根本不符合问题的规范。 您应该首先关注编写static boolean isPrime(int m, int[] P)方法。 所有这些方法需要做的是: 迭代P的内容 如果元素均匀地除以m ,则m为复合 - 返回false 如果元素的平方大于m ,则m为素数 - 返回true 。 这听起来像问题描述这种情况永远不会发生, P只会在穿越sqrt(m)边界之前只有从2到1的素数 如果已经测试了P所有元素,则m是素数 - 返回true 之后你可以编写main来制作primes数组并使用描述的循环构建 ... -
没有确切的细节就不可能知道,但两种最可能的可能性是: 在使用之前, sum未初始化为0 您遇到溢出 ,因为数字的总和太大而不适合它。 这显然取决于sum的类型和范围。 编辑: 编辑后的代码适用于我,对于小范围(请注意,对于更大的范围,还应考虑问题#2)。 你可能会误读结果,尝试添加endl到cout << sum ; It is impossible to know without the exact details but two most likely possibilities are: sum wa ...
-
双素数对的公式(Formula for twin prime pairs)[2022-11-04]
当然,第一个错误是你的make_pair_table函数中你没有调用get_pairs而是调用get_primes ,但似乎你已经修复了它。 另一个错误是,只要遇到素数对,您只需将对中的第一个数字添加到对列表中。 例如,对于素数3,5,7,11你比较3和5,然后加3,然后你比较5和7并加5,但是你比较7和11并且不加7,即使它是在一对素数中。 解决此问题的一种方法是为每对添加两个素数,但是您必须检查第一个数字是否已经是最后一对的一部分。 for i in range(len(prime_list) - 1) ... -
计算一百万个素数(to calculate one million prime numbers)[2022-10-19]
一个简单的Eratosthenes筛子就像拍板一样运行。 这在我的盒子里计算了不到一秒的第1,000,000个素数: class PrimeSieve { public ListPrimes; private BitArray Sieve; public PrimeSieve(int max) { Primes = new List { 2, 3 }; // Must include at least 2, 3. Si ... -
素数python嵌套循环(Prime number python nested loops)[2023-11-25]
首先,我想发布代码的正确语法。 i = 2 while(i < 50): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j): print(i, " is prime") i = i + 1 通过编写这样的代码,我们可以做几件事。 我们能做的第一件事是获得一个很好 ... -
我建议使用像Eclipse这样的IDE,它会显示是否有任何死代码。 在你的情况下,内循环的i ++是死代码,因为你在做i = n + 1。 第一次迭代后,我总是大于n。 使用以下代码作为参考 public static void main(String[] args) { HashSet
A = new HashSet<>(); A.add(2); boolean addNumber; for (int n = 3; n < 100; n++) { ... -
基本的嵌套循环计算1 - 239之间的素数(basic nestled loop calculate prime numbers between 1 - 239, inclusive)[2024-03-19]
std::cout << std::to_string(2) << std::endl; for (unsigned int i = 3; i<240; i += 2) { unsigned int j = 3; int sq = sqrt(i); for (; j <= sq; j += 2) if (!(i%j)) break; if (j>sq) std::cout << std::to_string(i) << std::endl; } 首先,主要定义:素数(或素数 ... -
Java非素数(Java non-prime numbers)[2023-01-07]
你可以使用ArrayList而不是像这样的int数组: import java.util.ArrayList; public static void main (String[] args) { int x=10;//end of the interval int y=2;//first of the interval ArrayListnonPrime = new ArrayList (); for(int i=y+1;i< ... -
素数嵌套循环逻辑(Prime number nested loop Logic)[2023-09-15]
不需要遍历内部循环的整个范围,对于内部循环,可能的值从2开始到<=那个数字/ 2 。 就像你想要检查99是否为素数一样,你需要将内循环从2设置为49(99/2是该数字的最大可能因子),所以不要遍历其余的全部。 因此,如果你将内部循环从2迭代到98,那么它意味着在49之后迭代这个循环,考虑一下。 #include#include using namespace std; int main() { //vector n; // will store ... -
素数没有循环没有递归(Prime numbers no loop no recursive)[2021-12-02]
没有办法在没有任何循环的情况下对无限范围的数字进行素性测试。 这个问题的递归解决方案也非常有限,因为它们不可扩展。 No there is no way to perform primality test on an unlimited range of numbers without using any loop. Recursive solutions to this problem are also very limited in that they are not scalable.