我正在尝试在 Tensorflow 中执行以下操作 -
import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
i = tf.Variable(0)
sol = tf.Variable(0)
def cond(i, sol):
return tf.less(i, 2)
def body(i, sol):
i = tf.add(i, 1)
sol = tf.add(sol, 1)
tf.while_loop(cond, body, [i, sol])
with tf.Session(graph=graph) as session:
tf.global_variables_initializer().run()
result = session.run(sol, feed_dict={})
print result
Run Code Online (Sandbox Code Playgroud)
我无法理解错误消息中的两个“结构”是什么。我想最终根据 tf.Placeholder (上面代码中的“i”)的值制作一个带有“条件”的“tf.while_loop”。
我正在使用 Wordpress 的高级自定义字段并尝试在组内循环转发器。我得到的只是“注意:数组到字符串的转换......”
出了什么问题,我该如何解决?
<?php if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); ?>
<?php $horlur = get_sub_field('horlur'); ?>
<?php if( have_rows( $horlur['arsmodeller_lankar']) ): while ( have_rows($horlur['arsmodeller_lankar']) ) : the_row(); ?>
<?php echo get_sub_field('lank'); ?>
<?php endwhile; endif; ?>
<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud) php wordpress while-loop nested-loops advanced-custom-fields
Scala Tour Of Scala 文档以 whileLoop 函数为例解释了按名称传递的参数。
def whileLoop(condition: => Boolean)(body: => Unit): Unit =
if (condition) {
body
whileLoop(condition)(body)
}
var i = 2
whileLoop (i > 0) {
println(i)
i -= 1
} // prints 2 1
Run Code Online (Sandbox Code Playgroud)
该部分解释了如果不满足条件,则不会评估主体,从而通过不评估未使用的代码主体来提高性能。
Scala 的实现是否while已经使用了按名称传递的参数?
如果有原因或特定情况导致实现无法使用按名称传递参数,请向我解释,到目前为止我还没有找到任何相关信息。
编辑:根据 Valy Dia(/sf/users/407844461/)的回答,我想添加另一个问题......
如果在某些情况下可能根本不评估主体,while 语句的方法实现是否会比语句本身性能更好?如果是这样,为什么要使用 while 语句呢?
我将尝试对此进行测试,但我是 Scala 的新手,因此可能需要一些时间。如果有人愿意解释,那就太好了。
干杯!
我试图将值 0 存储在关联数组中,以便以后将其用作另一个数组的键。
但是,我无法使用下面的代码检索 0。
发生的情况是,在 while 循环中,当内容为 0 时,它们的“主题”键被完全忽略。
任何其他值都可以。
如何遍历关联数组并获得 0 值?
这种行为的原因是什么?
$requestdata = [];
$requestdata['company'] = 'some company';
$requestdata['country'] = 'some country';
$requestdata['message'] = 'some message';
$requestdata['link'] = 'www.somelink.com';
$requestdata['containers'] = 'some container';
$requestdata['products'] = 'some product';
$requestdata['subject'] = 0;
while (current($requestdata)):
$key = key($requestdata);
if($key != "subject"):
$ncf_values = [];
$ncf_values["fieldValue"] = [];
$ncf_values["fieldValue"]["contact"] = 120;
$ncf_values["fieldValue"]["field"] = 12;
$ncf_values["fieldValue"]["value"] = $requestdata[$key];
elseif($key == "subject"):
$subjects_array = ["Selling","Buying", "Services", "Other"];
$ncf_values = [];
$ncf_values["fieldValue"] = …Run Code Online (Sandbox Code Playgroud) 我在完成这项任务时遇到了麻烦。我不能使用高级方法。我的教授说这只能使用循环来完成。没有函数,没有数学课,没有列表,什么都没有。
用户输入一个大于或等于 1 的数字 (n)。然后,代码要求用户输入与 n-1 一样多的数字,然后代码应该打印出用户没有输入的缺失数字'进入。所以:
示例运行:
请输入 n:5
请输入数字:4
请输入数字:5
请输入一个数字:1
请输入一个数字:2
缺少的数字是:3
-------------- 这是我到目前为止所拥有的,但它不起作用。它仅在按顺序输入数字时才有效。
n = int(input("Please enter n: "))
counter = 1
holder = -1
while (counter < n):
x = int(input("Please enter a number: "))
if x != counter:
holder = counter
counter += 1
print("This missing number is " + str(holder))
Run Code Online (Sandbox Code Playgroud) 我正在将许多每日 Apache 日志文件读入 R。这些文件都被命名为“logfile_”加上它们的日期,例如logfile_2019-12-30. 这是我读取文件的方式:
path <- "/path/to/logs/"
beginning <- as.Date("2019-12-01", format="%Y-%m-%d")
ending <- as.Date("2019-12-31", format="%Y-%m-%d")
d <- beginning
dat <- data.frame()
while (d < ending)
{
dat <- rbind(dat, read.table(paste0(path, "logfile_", d), stringsAsFactors = FALSE))
d <- d + 1
}
Run Code Online (Sandbox Code Playgroud)
一个月(大约有 100 万行日志条目)while-loop 执行大约需要四分钟。我想阅读和处理几年的文件,但不想等待几个小时。
如何更有效、更快地读取文件?
如果用户输入 like 输入输入句子和句子中单词的索引,您将如何使用fororwhile循环来查找句子中的单词。
sentence = input('Please enter a sentence: ')
word_number = input ('Please enter word number request: ')
Run Code Online (Sandbox Code Playgroud) 我有一个用例,我需要重复一些代码块:
我想知道,是否有一些 Python 内置方法可以:
while循环的方式重复for循环的方式重复注意:我知道我可以使用if语句(取决于是否存在上限),并在 afor或whileloop之间切换。我试图不让代码块重复两次,或者它分解为另一个函数。
当前实施
我使用 Python 3.8.2编写了以下有缺陷的(见下文)for_while_hybrid.py。
from typing import Iterator
def iter_for_while(num_yield: int = None) -> Iterator[int]:
"""Generate an incrementing counter.
This enables one to make a for loop or a while loop, depending on args.
Args:
num_yield: Number of times to yield.
If left as None, the generator makes a while loop
If …Run Code Online (Sandbox Code Playgroud) 作为 js 的新手,当我遇到以下代码的一些特殊行为时,我正在玩循环:
var i = 5;
while(i!=0){
console.log(i);
i--;
}Run Code Online (Sandbox Code Playgroud)
OUTPUT: 543211
Run Code Online (Sandbox Code Playgroud)
我在 c++ 中复制了相同的代码:
OUTPUT: 543211
Run Code Online (Sandbox Code Playgroud)
OUTPUT: 54321
Run Code Online (Sandbox Code Playgroud)
知道我是否遗漏了两种语言之间的一些重要差异会有所帮助。
为什么我可以这样做:
if (int result=getValue(); result > 100) {
}
Run Code Online (Sandbox Code Playgroud)
但不能这样做:
while (int result=getValue(); result > 100) {
}
Run Code Online (Sandbox Code Playgroud)
为什么要歧视while?条件就是条件。为什么while不能像ifcan那样评价它?
为了使用 实现所需的行为while,我必须以这种方式实现它:
int result = getValue();
while (result > 100) {
//do something
result = getValue();
}
Run Code Online (Sandbox Code Playgroud) while-loop ×10
python ×4
for-loop ×2
loops ×2
php ×2
arrays ×1
c++ ×1
c++17 ×1
c++20 ×1
if-statement ×1
input ×1
iterator ×1
javascript ×1
nested-loops ×1
numbers ×1
pass-by-name ×1
r ×1
rbind ×1
read.table ×1
scala ×1
tensorflow ×1
wordpress ×1