标签: nested

Python:打印嵌套类

打印嵌套类的内容的最佳方法是什么?例如:

class Library():
    def __init__(self):
        self.shelves = []
        self.shelf = self.Shelf()

    class Shelf():
        def __init__(self):
            self.books = []


x = Library()
Run Code Online (Sandbox Code Playgroud)

我知道如果该类未嵌套,则vars(x)可以工作。但是,如果是这样,我会得到类似以下内容的信息:

{'shelf': <__main__.Shelf instance at 0x7f4bae723560>, 'shelves': []}
Run Code Online (Sandbox Code Playgroud)

作为输出。如何获取python将其打印为:

{'shelf': {'books': []}, 'shelves': []}
Run Code Online (Sandbox Code Playgroud)

python printing nested class

1
推荐指数
1
解决办法
1751
查看次数

使用strncpy(),其中destination包含源

首先,如果有人回答,我道歉.我做了搜索stackoverflow,但找不到类似的主题.(也许我的搜索功能很弱)

我编写了一个函数来修剪C中字符串中的空格字符.我关心的是trim函数中的最后一行,其中源包含在目标中.测试用例都很好,还有其他一些测试.可以复制源和目标位于同一内存中的全部或部分字符串会导致奇怪的问题吗?

谢谢,

#include <stdio.h>
#include <string.h>

void trim(char *line)
  {
  int  i, len = strlen(line);
  char *ptr, whitespace[] = " \t\n";

  // scan for first char which does not match a char in whitespace string
  for (i=0; i<len; i++)
    if (strchr(whitespace, line[i]) == NULL)
      break;
  ptr = line + i;

  // scan for last char which does not match a char in whitespace string
  for (i=len; i>0; i--)
    if (strchr(whitespace, line[i]) == NULL)
      break;
  line[i] + 1) = …
Run Code Online (Sandbox Code Playgroud)

c string nested trim strncpy

1
推荐指数
1
解决办法
436
查看次数

如何使用JSON.NET创建具有嵌套数组值的JSON String?

感谢@ don-jayamanne和@dbc提到我的JSON需要格式良好

这是我的改述问题:

我们的工作应用程序是使用JSON.NET来创建JSON字符串.

这是我正在尝试创建的JSON字符串:

{
    "RouteID": "123321213312",
    "DriverName": "JohnDoe",
    "Shift": "Night",
    "ItineraryCoordinates": [
        [
            9393,
            4443
        ],
        [
            8832,
            3322
        ],
        [
            223,
            3432
        ],
        [
            223,
            3432
        ]
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是我编写的用于创建上述JSON字符串的错误代码:

writer.WriteStartObject();
writer.WritePropertyName("RouteID");
serializer.Serialize(writer, routeID);

writer.WritePropertyName("DriverName");
serializer.Serialize(writer, driverName);

writer.WritePropertyName("Shift");
serializer.Serialize(writer, shift);

writer.WritePropertyName("ItineraryCoordinates");

ItineraryCoordinatesCollectionFactory tpCollFac = new ItineraryCoordinatesCollectionFactory();
ItineraryCoordinates anItineraryCoordinates;

StringBuilder coordSB = new StringBuilder();

IList<TimePeriod> ItineraryCoordinatesCollection = tpCollFac.createItineraryCoordinatesCollection();
for (int j = 0; j < ItineraryCoordinatesCollection.Count(); j++)
{
    anItineraryCoordinates = ItineraryCoordinatesCollection[j];

    writer.WriteStartObject();
    writer.WritePropertyName("nested");
    coordSB.Append(anItineraryCoordinates.StartTimePeriodCoordinate.X.ToString());
    coordSB.Append(" , ");
    coordSB.Append(anItineraryCoordinates.StartTimePeriodCoordinate.Y.ToString()); …
Run Code Online (Sandbox Code Playgroud)

c# json nested json.net converters

1
推荐指数
1
解决办法
4362
查看次数

使用类似数据解析.NET中的嵌套JSON

我有一个我正在调用的API返回一组JSON.这个特别是给我一套项目.这些项目可以有多个子项目.所有子项目的结构都是相同的,因为它是主要项目.我似乎找不到任何能告诉我如何解析JSON的内容,以便它在.NET中为我提供某种可用的类,以便我可以在我的网页上显示它.

这是结构(缩小,因为原始是巨大的,并重命名项目和数字,以使其更容易阅读):

{
"data": [
    {
        "id": 70000,
        "name": "Project 70000",
        "children": [ 
            {
                "id": 71000,
                "name": "Project 71000",
                "children": [
                    {
                        "id": 71100,
                        "name": "Project 71100",
                        "children": [
                            {
                                "id": 71110,
                                "name": "Project 71110",
                                "children": [
                                    {
                                        "id": 71111,
                                        "name": "Project 71111"
                                    },
                                    {
                                        "id": 71112,
                                        "name": "Project 71112"
                                    }
                                 ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 80000,
        "name": "Project 80000",
        "children": [
           {
               "id": 81000,
               "name": "Project 81000"
           }
        ]
    }
]
}
Run Code Online (Sandbox Code Playgroud)

我认为我希望我的班级看起来像(我可能在这里错了)是这样的:

public …
Run Code Online (Sandbox Code Playgroud)

.net c# json nested json.net

1
推荐指数
1
解决办法
135
查看次数

Spark获取嵌套json的列名

我试图通过DataFrames从嵌套的JSON中获取列名.架构如下:

root
 |-- body: struct (nullable = true)
 |    |-- Sw1: string (nullable = true)
 |    |-- Sw2: string (nullable = true)
 |    |-- Sw3: string (nullable = true)
 |    |-- Sw420: string (nullable = true)
 |-- headers: struct (nullable = true)
 |    |-- endDate: string (nullable = true)
 |    |-- file: string (nullable = true)
 |    |-- startDate: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)

我可以使用df.columns()获取列名"body"和"header"但是当我尝试使用df.select("body")从正文中获取列名(例如:Sw1,Sw2,...) ).columns它总是给我身体专栏.

有什么建议吗?:)

java json nested apache-spark-sql spark-dataframe

1
推荐指数
1
解决办法
4897
查看次数

如何访问嵌套类

如果我有以下几点的话

namespace mynamespace
{
  class A
  {
    public:
    class B{};
    class C{};
  };
int foo(B bObject, C cObject); //error
}
Run Code Online (Sandbox Code Playgroud)

编译时,B和C不命名类型.有没有办法让我可以在命名空间中定义使用A类中定义的公共嵌套类(B和C)的函数?

c++ nested namespaces class

1
推荐指数
1
解决办法
36
查看次数

在Python中嵌套

我有一个列表,我想迭代它,然后迭代嵌入for在第一个我读过的下一个位置的同一个列表中的第一个.

在像Java这样的语言中:

int[10] array;
for (int i=0; i < array.length(); i++)
    for (int j=i+1; j < array.length(); j ++)
        //do something with the array comparing a[i] and a[j]
Run Code Online (Sandbox Code Playgroud)

我怎么能在Python上做到这一点?我试试这个:

for a in array:
     del array[0]
     for a2 in array:
         //do something with the array comparing a and a2
Run Code Online (Sandbox Code Playgroud)

但只适用于第一次迭代..任何帮助?

python nested python-2.7

1
推荐指数
1
解决办法
92
查看次数

聚合物嵌套dom-repeat模板将第二个模板复用

我按照Polymer官方的嵌套模板示例,重复第二个模板.

我的数组数据类似于:

[
  {
    "title": "My title book",
    "author": "The author",
    "votes": [
      { "bad": 0 },
      { "regular": 2 },
      { "good": 201 },
      { "excellent": 458 } 
    ]
  },
  {
    "title": "My title book",
    "author":"The author",
    "votes": [
      { "bad": 0 },
      { "regular": 2 },
      { "good":201 },
      { "excellent": 458 }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

这是我的聚合物元素代码:

<template is="dom-repeat" items="{{books}}" as="book">
      <div><b>Title: </b><span>{{book.title}}</span></div>
      <div><b>Author: </b><span>{{book.author}}</span></div>
      <div>
        <p>Votes:</p>
        <template is="dom-repeat" items="{{book.votes}}" as="vote">
          <b>Bad: </b><span>{{vote.bad}}</span>
          <b>Regular: </b><span>{{vote.regular}}</span>
          <b>Good: </b><span>{{vote.good}}</span>
          <b>Excellent: …
Run Code Online (Sandbox Code Playgroud)

javascript templates nested repeat polymer

1
推荐指数
1
解决办法
141
查看次数

遍历嵌套列表,单个为

我在列表中有一堆列表.嵌套深度在运行时确定,我只想访问它们到特定的(运行时决定的)深度,以任意方式操纵该级别的内容.

理想情况下,我希望能够像下面这样做:

for x in access_list(nested_list, d)
    # do stuff at nesting-depth d
Run Code Online (Sandbox Code Playgroud)

该怎么access_list做:

>>> mylist = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
>>> for d in range(4):
...     for l in access_list(mylist, d):
...         print((d, l))
(0, [[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
(1, [[[0, 1], [2, 3]])
(1, [[4, 5], [6, 7]]])
(2, [0, 1])
(2, [2, 3])
(2, [4, 5])
(2, [6, 7])
(3, 0)
(3, 1)
(3, 2)
(3, 3) …
Run Code Online (Sandbox Code Playgroud)

python for-loop nested list python-3.x

1
推荐指数
1
解决办法
141
查看次数

根据嵌套for循环中的条件创建新列

我正在尝试在我的数据集中创建一个新列,告诉我产品的所有3个月的收入是0,所有3个月都是0,或者3个月都没有0.

我提供NewColumn了我想要的结果.

data$ZEROES <- 0
data$ZEROES2 <- 0
for (i in unique(data$product_id)){
    for (j in unique(data$Revenue)){
        n[j] <-ifelse(all(data$Value == 0)," ALL 0", 
        ifelse(any(data$Value == 0),"Some 0", 
        ifelse(all(data$Value != 0), "None 0", "Blank")))
    data$ZEROES[j] <-n[j]
    data$ZEROES2[i] <-long$ZEROES[j]
    }
} 

product_ id Date         Revenue           Value    NewColumn
1           January       in               0           Some 0   
1           February      in               1           Some 0 
1           March         in               0           Some 0 
1           January       out              0           All 0 
1           February      out              0           All 0 
1           March         out              0 …
Run Code Online (Sandbox Code Playgroud)

for-loop nested r subset lapply

1
推荐指数
1
解决办法
75
查看次数