我有这样的 json 对象
[
{"id" : 1, "parentid" : null},
{"id" : 2, "parentid" : null},
{"id" : 3, "parentid" : 2},
{"id" : 4, "parentid" : 3}
]
Run Code Online (Sandbox Code Playgroud)
我想让它像这样嵌套在javascript中
[
{"id" : 1, "parentid" : null},
{"id" : 2, "parentid" : null, "childs":
[{"id" : 3, "parentid" : 2, "childs":
[{"id": 4, "parentid" : 3}]}]
}
]
Run Code Online (Sandbox Code Playgroud)
我需要使用递归函数还是只需一个简单的循环即可完成?实现这一目标最有效的方法是什么?
我正在 Unity 环境中进行一些视觉艺术研究。我正在尝试实现与此处解释的差分线增长非常相似的东西, 但我主要担心的是,在算法中的某个地方,每个节点都应该检查每个其他节点以查看它的接近程度,并根据所有这些构建排斥力阵列附近的粒子。
这是我的代码片段:
public void Differentiate()
{
int c = nodes.Count;
Vector3[] repulsionForces = new Vector3[c];
for (int i = 0; i < c ; i++)
{
// Construct nearbies
List<DifferentialNode> nearby = new List<DifferentialNode>();
foreach(DifferentialNode n in nodes)
{
float d = Vector3.Distance(n.position, nodes[i].position);
if (d < 5)
{
nearby.Add(n);
}
}
// Get Forces
Vector3 repulsionForce = nodes[i].RepulsionForce(nearby);
// Limit Forces
repulsionForce = Vector3.ClampMagnitude(repulsionForce, maxForce);
// Apply Multipliers
repulsionForce *= Repulsion;
// Put …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码。此处引用的其他类已经过测试并且可以正常工作,但它们不应影响此处语句的功能。
import java.util.Scanner;
public class TransferTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double transferAmount;
Account acct1 = new Account(100, null);
Account acct2 = new Account(100, null);
System.out.print("Choose an option: "
+ "\n1 <Enter>: to transfer from account 1 to account 2"
+ "\n2 <Enter>: to transfer from account 2 to account 1"
+ "\n3 <Enter>: to quit");
int userOption = scan.nextInt();
scan.nextLine();
while(userOption != 3);
{
if (userOption == 1)
{
System.out.print("You chose transfer …Run Code Online (Sandbox Code Playgroud) 我有两个关于使用嵌套循环的问题。
colors = []
for color in soccer_match:
colors.append(color['colors'])
colors
Run Code Online (Sandbox Code Playgroud)
这给了我[['蓝色','白色','红色'],['绿色','金色']]。我怎样才能将两个列表合并为一个?我还没有学习列表理解或函数。
这是给定的示例。谢谢你!
soccer_match = [
{ "home_team": True,
"away_team": False,
"country": "France",
"num_passes": 484,
"passes_completed": 423,
"fouls_committed": 16,
"colors": ["blue", "white", "red"],
"players": [
{
"name": "Hugo LLORIS",
"captain": True,
"shirt_number": 1,
"position": "Goalie"
},
{
"name": "Benjamin PAVARD",
"captain": False,
"shirt_number": 2,
"position": "Defender"
},
{
"name": "Raphael VARANE",
"captain": False,
"shirt_number": 4,
"position": "Defender"
},
{
"name": …Run Code Online (Sandbox Code Playgroud) 这是一个嵌套循环,其中内部索引取决于外部索引,具有以下参数:
f = rand(1,70299)
nech=24*30*24
N=length(f);
xh=(1:nech)/24;
Run Code Online (Sandbox Code Playgroud)
在 MATLAB 中:
sf2(1:nech)=0.;
sf2vel(1:nech)=0.;
count(1:nech)=0.;
for i=1:nech
for j=1:N-i-1
sf2(i)=sf2(i)+(f(j+i)-f(j))^2;
count(i)=count(i)+1;
end
sf2(i)=sf2(i)/count(i);
end
Run Code Online (Sandbox Code Playgroud)
在Python中:
def structFunPython(f,N,nech):
sf2 = np.zeros(N)
count = np.zeros(N)
for i in range(nech):
indN = np.arange(1,N-i-1)
for j in indN:
sf2[i] += np.power((f[i+j]-f[j]),2)
count[i] += 1
sf2[i] = sf2[i]/count[i]
return sf2
Run Code Online (Sandbox Code Playgroud)
与赛通:
import cython
cimport numpy as np
import numpy as np
def structFun(np.ndarray f,N,nech):
cdef np.ndarray sf2 = np.zeros(N), count = np.zeros(N),
for i in range(nech):
indN …Run Code Online (Sandbox Code Playgroud) 我这里有一段代码,我想知道是否可以通过 Python 中的列表理解使这变得更简单......
T: str = 'XY'
combinations: list = [] # Make an empty list
for i in range(len(T)):
for j in range(len(T)): combinations.append((T[i], T[j])) # (T[i], T[j]) is act as a tuple here
Run Code Online (Sandbox Code Playgroud)
如何将其简化为更好的 for 循环,而不需要花费这么多次循环iand j?非常感谢您的帮助:)
所以我有一个阵列进入酒店信息,我需要的一件是每个酒店的位置,所以我可以将其发送到另一种方法.所以我有我的第一个foreach设置,但现在我想知道如何将位置上的所有数据收集到字符串数组中,以便我可以在读完所有酒店后发送出去.有人可以帮忙,谢谢.
// API call to get the info
ContentAPI.BasicHotelMedia[] rawData = DataHelper.NewContentAPI().GetBasicHotelMedia(ProductCode, ProductYear, SiteBrand);
//setting up the datatable
DataTable dtHotels = InitHotelTable();
//set my variables
foreach (ContentAPI.BasicHotelMedia item in rawData)
{
DataRow dr = dtHotels.NewRow();
dr["HotelCode"] = item.BasicHotelCode;
dr["HotelDescription"] = item.BasicDescription;
dr["WiFi"] = item.HasWifi;
// This is the variable that i need set in the string array so i can send into another method
dr["SellingLocation"] = item.BasicSellingLocation;
// Add other raw data
// Get other info about the hotel
GetHotelMedia(item.BasicHotelCode, …Run Code Online (Sandbox Code Playgroud) 有没有办法从ABAP中最嵌套的循环中继续最外层循环?
Java中的示例.这种语言中有一个使用标签的构造(大多数人还不知道它),它允许我从嵌套的循环中继续最外层的循环.
public class NestedLoopContinue {
public static void main(String[] args) {
label1: for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
if (i == 3) {
continue label1;
}
}
System.out.println(i + 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这输出
1
2
3
5
Run Code Online (Sandbox Code Playgroud)
现在,我怎么能以聪明的方式在ABAP中做到这一点?一种解决方案是使用TRY. ENDTRY.块,但它更像是一个黑客攻击.还有其他想法吗?
DATA: l_outer_counter TYPE i.
DO 5 TIMES.
l_outer_counter = sy-index.
TRY.
DO 2 TIMES.
IF l_outer_counter = 4.
RAISE EXCEPTION TYPE cx_abap_random.
ENDIF. …Run Code Online (Sandbox Code Playgroud) 我希望能帮助理解在R中进行某个计算所需的语法.
我有这样的数据帧:
a b c
1 1 0
2 1 1
3 1 0
4 2 0
5 2 0
6 3 1
7 3 0
8 3 0
9 4 0
Run Code Online (Sandbox Code Playgroud)
并且我想创建一个值为1的新列"d",如果(且仅当)列"c"中的任何值对于"b" 列中具有相同值的每组行等于1 . 否则(见第4,5和9行)列"d"给出0.
a b c d
1 1 0 1
2 1 1 1
3 1 0 1
4 2 0 0
5 2 0 0
6 3 1 1
7 3 0 1
8 3 0 1
9 4 0 0
Run Code Online (Sandbox Code Playgroud)
这可以用for循环完成吗?如果是这样,任何关于如何写这个的建议都将非常感激.
我已经看到了十二个要学习应用,应用,应用的站点,但是它们没有比教您如何获取行或列的总和或平均值更有效的方法。我必须使用很多for循环,通常是嵌套的。请在下面说明如何用Apply替换。
for (i in 1:NTR) {
X = as.vector(as.matrix(XTR[i,]))
YZ = FN(W, V, U, X) # return Y and Z
Y = YZ$Y
Z = YZ$Z
for (j in 1:NOUT) {
F[i+NOUT*(j-1)] = Y[j]-YTR[i,j]
} # j
} # i
Run Code Online (Sandbox Code Playgroud)
和
for (k in 1:NOUT) {
for (iwt in 1:NWT) {
m = NHID*(NNLIN+1)
if (iwt <= m) {
i = (iwt-1) %/% (NNLIN+1) + 1
j = (iwt-1) %% (NNLIN+1)
EVZ = V[k,i+1]*Z[i]*(1-Z[i])
if (j>0) EVZ = EVZ*X[j]
J[k+(n-1)*NOUT,iwt] …Run Code Online (Sandbox Code Playgroud) nested-loops ×10
python ×3
c# ×2
for-loop ×2
r ×2
abap ×1
apply ×1
cython ×1
if-statement ×1
java ×1
javascript ×1
kdtree ×1
loops ×1
matlab ×1
numpy ×1
recursion ×1
sap ×1
while-loop ×1