如何从R环境中的ls()获取所有对象的dim()[重复](How to get the dim() of all of the objects obtaining from ls() in R environment [duplicate])
这个问题在这里已经有了答案:
- 在R会话中管理可用内存的技巧 25个答案
我想知道如何通过
R
ls()
来获取有关所有objects
信息。 举例来说,我想通过ls()
函数知道所有列出对象的dim()
。我做了以下,但它是不正确的!
lapply(ls(),dim)
This question already has an answer here:
I would like to know how I can get some information about all of the
objects
can be find throughls()
inR
. As example let's say I'd like to know thedim()
of all of the listed objects throughls()
function.I did the following but it is not correct!
lapply(ls(),dim)
原文:https://stackoverflow.com/questions/43148871
最满意答案
Python支持一个内置的机制来处理列表,你可以很容易地做这样的事情:
list = [] list.append(10) list.insert(0, 20) list.append(30) list.insert(1, 50)
有关更多示例,请参阅官方文档https://docs.python.org/3/tutorial/datastructures.html 。
无论如何,如果你想通过你的自我编程一个双链表,我已经对你的代码做了一些修改:
class createnode: def __init__(self, data): self.data = data self.next = None self.prev = None class Unordered_list: def __init__(self): self.head = None def buildlst(self, data): node = createnode(data) if self.head is None: self.head = node else: node.next = self.head self.head.prev = node # <--- # node.prev = None <---- this is not necessary self.head = node def buildlstend(self, data): node = createnode(data) ptr = self.head while ptr.next: ptr = ptr.next ptr.next = node node.prev = ptr # <--- def insertbeforenode(self, data, srch_data): if srch_data == self.head.data: # <-- if you try to insert before the head self.buildlst(data) else: node = createnode(data) ptr = self.head while ptr: if ptr.data == srch_data: node.prev = ptr.prev node.next = ptr ptr.prev = node node.prev.next = node # <-- break # <--- when you find your node you have to quit from the loop else: ptr = ptr.next def printlist(self): temp = self.head while (temp): print(temp.data) temp = temp.next
我已经把箭头(#< - )放在了我进行更改的地方。 在处理指针和其他错误时存在一些错误。 请试试这个,让我知道!
Python supports a built-in mechanism to work with lists, you could easily do something like this:
list = [] list.append(10) list.insert(0, 20) list.append(30) list.insert(1, 50)
See the official documentation for more examples https://docs.python.org/3/tutorial/datastructures.html.
Anyway if you want to program a double linked list by your self I have made some changes to your code:
class createnode: def __init__(self, data): self.data = data self.next = None self.prev = None class Unordered_list: def __init__(self): self.head = None def buildlst(self, data): node = createnode(data) if self.head is None: self.head = node else: node.next = self.head self.head.prev = node # <--- # node.prev = None <---- this is not necessary self.head = node def buildlstend(self, data): node = createnode(data) ptr = self.head while ptr.next: ptr = ptr.next ptr.next = node node.prev = ptr # <--- def insertbeforenode(self, data, srch_data): if srch_data == self.head.data: # <-- if you try to insert before the head self.buildlst(data) else: node = createnode(data) ptr = self.head while ptr: if ptr.data == srch_data: node.prev = ptr.prev node.next = ptr ptr.prev = node node.prev.next = node # <-- break # <--- when you find your node you have to quit from the loop else: ptr = ptr.next def printlist(self): temp = self.head while (temp): print(temp.data) temp = temp.next
I have put an arrow (# <--) where I made the changes. There were a couples of mistakes on working with pointers and others inaccuracies. Please try this and let me know!
相关问答
更多-
是双重链接列表或BST(Is Doubly link list OR BST)[2023-07-17]
我建议你可以用这段代码检查它是否是一个双链表: node = givenNode; if(givenNode->P1 == null || givenNode->P2 == null) // It can not be double link list (circular) else if(givenNode->p1->p2 == givenNode || givenNode->p2->p1 == givenNode) { //It is a double linked list } else { It ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
每当数据首先插入列表时, Current将为null。 当您查找插入数据的位置时,检查Current != null是循环的结束。 检查一个空引用,它将告诉您将数据放在第一个项目中: if (Current == null) { _Head.Next.Data = Hold.Data; } else { Current.Next.Data = Hold.Data; } 您还可以让Current指向数据应该结束的节点而不是之前的节点: for (Current = FirstUnsorted; Cu ...
-
当您调用insert_before ,如果给定节点是头部,则新节点将是新头部。 所以你需要传递head的地址才能修改它。 你现在拥有的是这样的: head | v ------ ------ ------ - 30 - ---> - 20 - ---> - 10 - ------ <--- ------ <--- ------ ^ ------ | - 70 - --- ...
-
从您在评论中输出的输出看起来,在第二级看起来没有添加任何内容。 如果您希望在第二级添加节点,则会将它们添加到第一级。 我认为问题出在add子函数的底部: Student* StudentList::addChild(Student* node1, int num) { // rest of function. node1 is never modified return node1; } 您始终将节点输入返回到该函数。 我想在你的下面的代码中 newNode = sl->addNode( ...
-
Python支持一个内置的机制来处理列表,你可以很容易地做这样的事情: list = [] list.append(10) list.insert(0, 20) list.append(30) list.insert(1, 50) 有关更多示例,请参阅官方文档https://docs.python.org/3/tutorial/datastructures.html 。 无论如何,如果你想通过你的自我编程一个双链表,我已经对你的代码做了一些修改: class createnode: def __i ...
-
带字符串的双链表(Doubly Linked List with strings)[2023-07-01]
你的函数insert_beginning有一个问题: var=(struct node *)malloc(sizeof(struct node)); var->data[100]=words[100] 通过这两行你想要复制words数组并将它放在列表的节点上:)但你实现是假的,你只需复制第100个char (这是数组的界限,因为第一个char开始于0,最后一个是99索引)从words数组到节点data 100th char。 您必须复制所有words内容,因此您可以使用strncpy函数来执行此操 ... -
双链表最后删除(Doubly Linked List delete last)[2023-10-10]
如果有任何方法可以访问新删除的lastNode对象,则只需要将引用更新为null。 如果无法通过遍历列表来访问此节点,那么您不必担心它。 虽然,为了以防万一,进行这种清理是一种很好的做法。 You would only need to update the reference to null if there was any way to access the newly deleted lastNode object. If there is no way to reach this node by tr ... -
双链表类(Doubly linked list class)[2022-09-28]
绘图总是有助于这些数据结构。 你目前有什么: 你正确设置t的next : t->set_next(n); 。 缺少的是它下面的箭头,它是: n->set_prev(t) 。 通常,在处理双向链表时,对set_next的调用应该总是(大部分时间)伴随着对set_next参数的set_next的调用。 这是因为双链表的属性: x->next == y 表示 y->prev == x , y->prev == x 表示 x->next == y 。 Drawing always helps with such d ... -
用于在双向链表中插入的更好的方法结构(Java)(Better method structure for insertion in doubly linked list (Java))[2022-04-08]
public void insert(Link newNode) { if(head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } 顺便说一句,重命名你的类因为LinkedList与java.uti ...