How can I remove an element from a list?

If you don’t want to modify the list in-place (e.g. for passing the list with an element removed to a function), you can use indexing: negative indices mean “don’t include this element”. x <- list(“a”, “b”, “c”, “d”, “e”); # example list x[-2]; # without 2nd element x[-c(2, 3)]; # without 2nd and 3rd Also, … Read more

Get column index from column name in python pandas

Sure, you can use .get_loc(): In [45]: df = DataFrame({“pear”: [1,2,3], “apple”: [2,3,4], “orange”: [3,4,5]}) In [46]: df.columns Out[46]: Index([apple, orange, pear], dtype=object) In [47]: df.columns.get_loc(“pear”) Out[47]: 2 although to be honest I don’t often need this myself. Usually access by name does what I want it to (df[“pear”], df[[“apple”, “orange”]], or maybe df.columns.isin([“orange”, “pear”])), … Read more

How can I index a MATLAB array returned by a function without first assigning it to a local variable?

It actually is possible to do what you want, but you have to use the functional form of the indexing operator. When you perform an indexing operation using (), you are actually making a call to the subsref function. So, even though you can’t do this: value = magic(5)(3, 3); You can do this: value … Read more

List of all index & index columns in SQL Server DB

There are two “sys” catalog views you can consult: sys.indexes and sys.index_columns. Those will give you just about any info you could possibly want about indices and their columns. EDIT: This query’s getting pretty close to what you’re looking for: SELECT TableName = t.name, IndexName = ind.name, IndexId = ind.index_id, ColumnId = ic.index_column_id, ColumnName = … Read more

SQL join: selecting the last records in a one-to-many relationship

This is an example of the greatest-n-per-group problem that has appeared regularly on StackOverflow. Here’s how I usually recommend solving it: SELECT c.*, p1.* FROM customer c JOIN purchase p1 ON (c.id = p1.customer_id) LEFT OUTER JOIN purchase p2 ON (c.id = p2.customer_id AND (p1.date < p2.date OR (p1.date = p2.date AND p1.id < p2.id))) … Read more

How do MySQL indexes work?

Basically an index on a table works like an index in a book (that’s where the name came from): Let’s say you have a book about databases and you want to find some information about, say, storage. Without an index (assuming no other aid, such as a table of contents) you’d have to go through … Read more

Replace a character at a specific index in a string?

String are immutable in Java. You can’t change them. You need to create a new string with the character replaced. String myName = “domanokz”; String newName = myName.substring(0,4)+’x’+myName.substring(5); Or you can use a StringBuilder: StringBuilder myName = new StringBuilder(“domanokz”); myName.setCharAt(4, ‘x’); System.out.println(myName);

How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

Provide the :name option to add_index, e.g.: add_index :studies, [“user_id”, “university_id”, “subject_name_id”, “subject_type_id”], unique: true, name: ‘my_index’ If using the :index option on references in a create_table block, it takes the same options hash as add_index as its value: t.references :long_name, index: { name: :my_index }

Get first row value of a given column

To select the ith row, use iloc: In [31]: df_test.iloc[0] Out[31]: ATime 1.2 X 2.0 Y 15.0 Z 2.0 Btime 1.2 C 12.0 D 25.0 E 12.0 Name: 0, dtype: float64 To select the ith value in the Btime column you could use: In [30]: df_test[‘Btime’].iloc[0] Out[30]: 1.2 There is a difference between df_test[‘Btime’].iloc[0] (recommended) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)