equivalent of a python dict in R

前端 未结 3 660
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 00:46

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] =          


        
3条回答
  •  生来不讨喜
    2021-01-31 01:49

    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"
    

    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.

提交回复
热议问题