How to Correctly Use Lists

How to correctly use lists?

Just to address the last part of your question, since that really points out the difference between a list and vector in R:

Why do these two expressions not return the same result?

x = list(1, 2, 3, 4); x2 = list(1:4)

A list can contain any other class as each element. So you can have a list where the first element is a character vector, the second is a data frame, etc. In this case, you have created two different lists. x has four vectors, each of length 1. x2 has 1 vector of length 4:

> length(x[[1]])
[1] 1
> length(x2[[1]])
[1] 4

So these are completely different lists.

R lists are very much like a hash map data structure in that each index value can be associated with any object. Here's a simple example of a list that contains 3 different classes (including a function):

> complicated.list <- list("a"=1:4, "b"=1:3, "c"=matrix(1:4, nrow=2), "d"=search)
> lapply(complicated.list, class)
$a
[1] "integer"
$b
[1] "integer"
$c
[1] "matrix"
$d
[1] "function"

Given that the last element is the search function, I can call it like so:

> complicated.list[["d"]]()
[1] ".GlobalEnv" ...

As a final comment on this: it should be noted that a data.frame is really a list (from the data.frame documentation):

A data frame is a list of variables of the same number of rows with unique row names, given class ‘"data.frame"’

That's why columns in a data.frame can have different data types, while columns in a matrix cannot. As an example, here I try to create a matrix with numbers and characters:

> a <- 1:4
> class(a)
[1] "integer"
> b <- c("a","b","c","d")
> d <- cbind(a, b)
> d
a b
[1,] "1" "a"
[2,] "2" "b"
[3,] "3" "c"
[4,] "4" "d"
> class(d[,1])
[1] "character"

Note how I cannot change the data type in the first column to numeric because the second column has characters:

> d[,1] <- as.numeric(d[,1])
> class(d[,1])
[1] "character"

List vs tuple, when to use each?

There's a strong culture of tuples being for heterogeneous collections, similar to what you'd use structs for in C, and lists being for homogeneous collections, similar to what you'd use arrays for. But I've never quite squared this with the mutability issue mentioned in the other answers. Mutability has teeth to it (you actually can't change a tuple), while homogeneity is not enforced, and so seems to be a much less interesting distinction.

How to correctly use in for string list in KQL

The blank line is considered separator between queries, unless you select the whole code for execution.

See screenshots below.

Select the whole code for execution.

=> Valid query

select the code for execution

Put the cursor on the query for execution.

There is no blank line after the let statement.

=> Valid query

put the cursor on the query for execution - without blank line

Put the cursor on the query for execution.

There is a blank line after the let statement.

=> Invalid query

Please not how the query is marked by a pale blue color, but not the let statement

put the cursor on the query for execution - with blank line

How to correctly work with mailing lists?

Fixing your problem in three easy steps:

  • Set up the mail-filtering capabilities of your mail client to store mail from each list to a separate folder. You could also use something like procmail on Unix-like systems.

  • Use the threading feature of your mail client so that you can follow discussions.

  • Set up your mailing list subscriptions, so that you receive individual messages, rather than digests. Digests are quite harder for you (and your mail client) to handle. They also frustrate us when you reply to the digest, rather than the individual message, thus breaking the threading in our mail clients.

How to properly print a list?

In Python 2:

mylist = ['x', 3, 'b']
print '[%s]' % ', '.join(map(str, mylist))

In Python 3 (where print is a builtin function and not a syntax feature anymore):

mylist = ['x', 3, 'b']
print('[%s]' % ', '.join(map(str, mylist)))

Both return:

[x, 3, b]

This is using the map() function to call str for each element of mylist, creating a new list of strings that is then joined into one string with str.join(). Then, the % string formatting operator substitutes the string in instead of %s in "[%s]".

Creating lists and sub items in R markdown not working any longer?

For everyone that has problems showing simple list, markdown need an Empty line before a list.


This will not work
* Item 1
* Item 2
* Item 3

The output will look something like this.

This will not work
* Item 1
* Item 2
* Item 3

To fix this;

INSERT EMPTY LINE HERE, BEFORE LIST.

* Item 1

* Item 2

* Item 3

should output like this

  • Item 1
  • Item 2
  • Item 3

Correctly marking up multiple nested lists within the same list item

You can use fake bullet lists to achieve this. You can build them up using non-breaking spaces and a ⦁ (Z NOTATION SPOT) character.

 - This is a list item
- This is also a list item  
   ⦁  ...containing a sublist  
   ⦁  with two items.

and then...

   ⦁  another sublist  
   ⦁  which also has two items
- This is a third list item

Result:

  • This is a list item

  • This is also a list item

    ⦁ ...containing a sublist

    ⦁ with two items.

    and then...

    ⦁ another sublist

    ⦁ which also has two items

  • This is a third list item

Proper way to make HTML nested list?

Option 2 is correct.

The nested list should be inside a <li> element of the list in which it is nested.

Link to the W3C Wiki on Lists (taken from comment below): HTML Lists Wiki.

Link to the HTML5 W3C ul spec: HTML5 ul. Note that a ul element may contain exactly zero or more li elements. The same applies to HTML5 ol.
The description list (HTML5 dl) is similar,
but allows both dt and dd elements.

More Notes:

  • dl = definition list.
  • ol = ordered list (numbers).
  • ul = unordered list (bullets).

Official W3C link (updated).

Suspceted error in declaration of list of lists, not sure how to correct

All patterns except the first one match with a singleton list: a list with one element. In that list you match again with a singleton list (so a list with one element that is a list with one element), and a list with two elements).

But you thus do not match with a list with an arbitrary number of sublists, nor with a list that has as first list a list with zero or more than two elements.

You however do not need to pattern match on the list, you can work with:

nested_max :: [[Int]] -> Int
nested_max = helper . concat
where helper [] = minBound
helper xs = maximum xs

We thus first concatenate the list of lists into a single list, then we check if that list is empty, in which case we return minBound, or we return maximum xs which will return the maximum of the elements.

How to markdown nested list items in Bitbucket?

Use 4 spaces.

# Unordered list

* Item 1
* Item 2
* Item 3
* Item 3a
* Item 3b
* Item 3c

# Ordered list

1. Step 1
2. Step 2
3. Step 3
1. Step 3.1
2. Step 3.2
3. Step 3.3

# List in list

1. Step 1
2. Step 2
3. Step 3
* Item 3a
* Item 3b
* Item 3c

Here's a screenshot from that updated repo:

screenshot

Thanks @Waylan, your comment was exactly right.



Related Topics



Leave a reply



Submit