unique

PHP unique array by value?

你离开我真会死。 提交于 2019-12-28 04:14:05
问题 I have an array in PHP that looks like this: [0]=> array(2) { ["name"]=> string(9) "My_item" ["url"]=> string(24) "http://www.my-url.com/" } [1]=> array(2) { ["name"]=> string(9) "My_item" ["url"]=> string(24) "http://www.my-url2.com/" } The two values in "name" are the same in this two items. I want to sort out duplicates like this. How do I create an unique array by checking the "name" value? 回答1: basically foreach($your_array as $element) { $hash = $element[field-that-should-be-unique];

Rails: how can I get unique values from column

99封情书 提交于 2019-12-28 03:29:06
问题 How can I get unique values from column in the table? For example, I have this Products table: ID NAME CATEGORY 1 name1 1st_cat 2 name2 2nd_cat 3 name3 1st_cat Here I want to get only 2 values - 1st_cat and 2nd_cat: <%Products.each do |p|%> <%=p.category%> <%end%> 回答1: Two more ways: Product.select(:category).map(&:category).uniq Product.uniq.pluck(:category) For Rails >= 5.1 use: Product.distinct.pluck(:category) 回答2: I think you can do this: <% Products.select("DISTINCT(CATEGORY)").each do

Getting unique items from a list [duplicate]

纵然是瞬间 提交于 2019-12-27 17:10:57
问题 This question already has answers here : Remove duplicates from a List<T> in C# (26 answers) Closed 5 years ago . What is the fastest / most efficient way of getting all the distinct items from a list? I have a List<string> that possibly has multiple repeating items in it and only want the unique values within the list. 回答1: Use a HashSet<T>. For example: var items = "A B A D A C".Split(' '); var unique_items = new HashSet<string>(items); foreach (string s in unique_items) Console.WriteLine(s

How can I get unique values from an array in Bash?

夙愿已清 提交于 2019-12-27 16:58:11
问题 I've got almost the same question as here. I have an array which contains aa ab aa ac aa ad , etc. Now I want to select all unique elements from this array. Thought, this would be simple with sort | uniq or with sort -u as they mentioned in that other question, but nothing changed in the array... The code is: echo `echo "${ids[@]}" | sort | uniq` What am I doing wrong? 回答1: A bit hacky, but this should do it: echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ' To save the sorted unique

Plot multiple lines (data series) each with unique color in R

坚强是说给别人听的谎言 提交于 2019-12-27 11:38:45
问题 I am fairly new to R and I have the following queries : I am trying to generate a plot in R which has multiple lines (data series). Each of these lines is a category and I want it to have a unique color. Currently my code is setup in this way : First, I am creating an empty plot : plot(1,type='n',xlim=c(1,10),ylim=c(0,max_y),xlab='ID', ylab='Frequency') Then for each of my category, I am plotting lines in this empty plot using a "for" loop like so : for (category in categories){ lines(data

Plot multiple lines (data series) each with unique color in R

十年热恋 提交于 2019-12-27 11:38:07
问题 I am fairly new to R and I have the following queries : I am trying to generate a plot in R which has multiple lines (data series). Each of these lines is a category and I want it to have a unique color. Currently my code is setup in this way : First, I am creating an empty plot : plot(1,type='n',xlim=c(1,10),ylim=c(0,max_y),xlab='ID', ylab='Frequency') Then for each of my category, I am plotting lines in this empty plot using a "for" loop like so : for (category in categories){ lines(data

How to put text from file into array

雨燕双飞 提交于 2019-12-25 19:04:45
问题 I need to read text from a file (text of few sentences) and then write down all unique characters. To do that I need to use an array. I wrote this code but it gives me nothing. #include <stdio.h> int main(void) { int i; FILE *in = fopen("test.txt", "r"); if (in) { char mas[50]; size_t n = 0; int ch; while ((ch = getc(in)) != EOF) { mas[n++] = (char)ch; } fclose(in); } for (i = 0; i < 50; i++) { printf("%c", mas[i]); printf("\n"); } return 0; } 回答1: //low level input output commands method

Unique value of object to use in section

二次信任 提交于 2019-12-25 17:06:23
问题 I have a Class and I need to get genre object, put in an array, get unique value (because I will have many) and use the array to populate section of a tableView. I have the following code: class Movie { var name: String! var plot: String! var imageUrl: String! var genre: String! init(name: String, plot: String, img: String, genre: String) { self.name = name self.plot = plot self.imageUrl = img self.genre = genre } } } import UIKit class ViewController: UIViewController, UITableViewDataSource,

Identify unique levels of categorical variable

坚强是说给别人听的谎言 提交于 2019-12-25 16:49:09
问题 I have a list of person ids, and the types of medicines they got on specific dates. I would like to create a variable count whereby I can give the indicator 1 to the first drug that occurs, 2 to the second unique drug and 3 to the third unique drug. When the first drug occurs after the second and third, I want it to still have the indicator 1. Likewise for unique drug 2, it should maintain the value 2 throughout the person's whole medication history, and the same for drug 3. +----------------

Unique the columns and get the frequencies in linux

萝らか妹 提交于 2019-12-25 12:44:05
问题 I have a data.txt with a matrix structure (4 X 9): 101000110 000000010 001010010 100101101 I want to count the frequencies of unique columns, the expected result is: 1001 2 0000 1 1010 1 0001 3 0010 1 1110 1 I only find "unique lines according to specific columns" using awk on the Internet, do I need to first transpose my data to solve this problem. I wonder whether there is a more direct way to figure it out? Thank you. 回答1: This awk will help: awk '{for (i=1;i<=NF;i++){ a[i]=a[i]""$i } }