相当于R中的python dict [英] equivalent of a python dict in R

查看:104
本文介绍了相当于R中的python dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使R中的python字典等效。基本上,在python中,我有:

I want to make the equivalent of a python dict in R. Basically, in python I have:

visited = {}

if atom_count not in visited:
  Do stuff
  visited[atom_count] = 1

这个想法是,如果我看到特定的atom_count,则我访问过[atom_count] =1。因此,如果再次看到那个atom_count,我就不会做东西。 Atom_Count是一个整数。

The idea is, if I saw that specific, atom_count, I have visited[atom_count] = 1. Thus, if I see that atom_count again, i don't "Do Stuff". Atom_Count is an integer.

谢谢!

推荐答案

与python字典最接近的东西R只是一个列表。与大多数R数据类型一样,列表可以具有一个名称属性,该属性可以使列表像一组名称/值对一样起作用:

The closest thing to a python dict in R is simply a list. Like most R data types, lists can have a names attribute that can allow lists to act like a set of name-value pairs:

> l <- list(a = 1,b = "foo",c = 1:5)
> l
$a
[1] 1

$b
[1] "foo"

$c
[1] 1 2 3 4 5

> l[['c']]
[1] 1 2 3 4 5
> l[['b']]
[1] "foo"

现在通常的免责声明:它们不是完全相同。会有差异。因此,您将很失望,尝试使用与在python中使用字典完全相同的方式来实际使用列表。

Now for the usual disclaimer: they are not exactly the same; there will be differences. So you will be inviting disappointment to try to literally use lists exactly the way you might use a dict in python.

这篇关于相当于R中的python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆