I have a list like this:
[ '0D',
'0A,0C',
'0C,0A',
'0C,0E,0D,0F',
'0C,0D,0E,0F',
'0B,0G',
'0B,0F'
]
In this list '0A,0C' and '0C,0A'.Also '0C,0E,0D,0F' &
'0C,0D,0E,0F' are similar. How to get the unique items from a list like this. I tried set but I guess the functionality of set is a bit different.
´set´ is good, if you use ´split´ first:
l = ['0D', '0A,0C', '0C,0A', '0C,0E,0D,0F', '0C,0D,0E,0F', '0B,0G', '0B,0F']
for i in range(len(l)):
l[i] = ','.join(sorted(l[i].split(',')))
l = set(l)
# {'0A,0C', '0B,0F', '0B,0G', '0C,0D,0E,0F', '0D'}
Related
I'm trying to remove url prefix from urls Array in logstash using ruby:
The url looks like this: urlArray = ['https://www.google.com','https://www.bcc.com']
I tried to use other array and do something like:
urlArray.each{ |url| newUrlArray.push(url.gsub("(https?://)?(www\.)?","")) }
I also tried:
newUrlArray = urlArray.map{ |url| url.gsub("(https?://)?(www\.)?","") }
I think I miss here something with the gsub.
Thanks
I suggest to use slice and then capture the group you're interested in.
urlArray = [
"https://www.google.com",
"https://www.bcc.com",
"http://hello.com",
"www.world.com"
]
newUrlArray = []
pattern = /(https?:\/\/)?(www.)?(.*)/
urlArray.each{ |url| newUrlArray.push(url.slice(pattern, 3)) }
puts newUrlArray
# google.com
# bcc.com
# hello.com
# world.com
Below is my code to find distinct site and distinct names from a list of tuples:
from collections import defaultdict,OrderedDict
weblogs = [
('Nanda', 'google.com'), ('Maha', 'google.com'),
('Fei', 'python.org'), ('Maha', 'google.com'),
('Fei', 'python.org'), ('Nanda', 'python.org'),
('Fei', 'dzone.com'), ('Nanda', 'google.com'),
('Maha', 'google.com'), ]
web = [t for t in (set(tuple(i) for i in weblogs))]
res = defaultdict(list)
for i, j in web:
res[j].append(i)
a = OrderedDict()
a = dict(res)
for i,j in sorted(a.items()):
print(i,j)
The output of this keeps changing between the below two outputs:
dzone.com ['Fei']
google.com ['Maha', 'Nanda']
python.org ['Nanda', 'Fei']
dzone.com ['Fei']
google.com ['Nanda', 'Maha']
python.org ['Fei', 'Nanda']
Is there a way to maintain a constant order of values?
The problem comes from the order of the values in the set. A simple solution would be to change that line:
web = [t for t in (set(tuple(i) for i in weblogs))]
like this:
web = [t for t in (sorted(set(tuple(i) for i in weblogs)))]
Could you help me wiht my issue ? Let's say that I have few list with ID's their members, like below:
team_A = [1,2,3,4,5]
team_B = [6,7,8,9,10]
team_C = [11,12,13,14,15]
and now I have a dictionary with their values:
dictionary = {5:23, 10:68, 15:68, 4:1, 9:37, 14:21, 3:987, 8:3, 13:14, 2:98, 7:74, 12:47, 1:37, 6:82, 11:99}
I would like to take correct elements from dictionary and create new dictionary for team A, B and C, like below:
team_A_values = {5:23, 4:1, 3:987, 2:98, 1:37}
Could you give advice how to do that ? Thanks for your help
You can do something like below by just Iterating through the lists
team_A = [1,2,3,4,5]
team_B = [6,7,8,9,10]
team_C = [11,12,13,14,15]
dictionary = {5:23, 10:68, 15:68, 4:1, 9:37, 14:21, 3:987, 8:3, 13:14, 2:98, 7:74, 12:47, 1:37, 6:82, 11:99}
team_A_values = {}
for i in team_A:
team_A_values[i] = dictionary[i]
print(team_A_values )
can repeat this to team B and team C
in that case you can do like this
team_values = [{i: dictionary[i] for i in team_A },{i: dictionary[i] for i in team_B},{i: dictionary[i] for i in team_C}]
teamA,teamB,teamC = team_values
print(team_values)
print(teamA)
print(teamB)
print(teamC)
in one line you can do like this
team_values = [{i: dictionary[i] for i in team } for team in [team_A ,team_B ,team_C]]
teamA,teamB,teamC = team_values
print(team_values)
print(teamA)
print(teamB)
print(teamC)
I am using a data frame to create a marked point process using as.ppp function. I get an error Error: is.numeric(x) is not TRUE. The data I am using is as follows:
dput(head(pointDataUTM[,1:2]))
structure(list(POINT_X = c(439845.0069, 450018.3603, 451873.2925,
446836.5498, 445040.8974, 442060.0477), POINT_Y = c(4624464.56,
4629024.646, 4624579.758, 4636291.222, 4614853.993, 4651264.579
)), .Names = c("POINT_X", "POINT_Y"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
I can see that the first two columns are numeric, so I do not know why it is a problem.
> str(pointDataUTM)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5028 obs. of 31 variables:
$ POINT_X : num 439845 450018 451873 446837 445041 ...
$ POINT_Y : num 4624465 4629025 4624580 4636291 4614854 ...
Then I also checked for NA, which shows no NA
> sum(is.na(pointDataUTM$POINT_X))
[1] 0
> sum(is.na(pointDataUTM$POINT_Y))
[1] 0
When I tried even only the first two columns of the data.frame, the error I get on using as.ppp is this:
Error: is.numeric(x) is not TRUE
5.stop(sprintf(ngettext(length(r), "%s is not TRUE", "%s are not all TRUE"), ch), call. = FALSE, domain = NA)
4.stopifnot(is.numeric(x))
3.ppp(X[, 1], X[, 2], window = win, marks = marx, check = check)
2.as.ppp.data.frame(pointDataUTM[, 1:2], W = studyWindow)
1.as.ppp(pointDataUTM[, 1:2], W = studyWindow)
Could someone tell me what is the mistake here and why I get the not numeric error?
Thank you.
The critical check is whether PointDataUTM[,1] is numeric, rather than PointDataUTM$POINT_X.
Since PointDataUTM is a tbl object, and tbl is a function from the dplyr package, what is probably happening is that the subset operator for the tbl class is returning a data frame, and not a numeric vector, when a single column is extracted. Whereas the $ operator returns a numeric vector.
I suggest you convert your data to data.frame using as.data.frame() before calling as.ppp.
In the next version of spatstat we will make our code more robust against this kind of problem.
I'm on the phone, so can't check but I think it is happens because you have a tibble and not a data.frame. Please try to convert to a data.frame using as.data.frame first.
i don't know sqlite but I have to implement a database already done. I'm programming with Corona SDK. The problem: i have a column called "answers" in this format: House,40|Bed,20|Mirror,10 ecc.
I want to split the string and remove "," "|" like this:
VARIABLE A=House
VARIABLE A1=40
VARIABLE B=Bed
VARIABLE B1=20
VARIABLE C=Mirror
VARIABLE C1=10
I'm sorry for my english. Thanks to everybody.
Try this:
If you want to simply remove the characters, then you can use the following:
Update 3 :
local myString = "House;home;flat,40|Bed;bunk,20|Mirror,10"
local myTable = {}
local tempTable = {}
local count_1 = 0
for word in string.gmatch(myString, "([^,|]+)") do
myTable[#myTable+1]=word
count_1=count_1+1
tempTable[count_1] = {} -- Multi Dimensional Array
local count_2 = 0
for word_ in string.gmatch(myTable[#myTable], "([^,|,;]+)") do
count_2=count_2+1
local str_ = word_
tempTable[count_1][count_2] = str_
--print(count_1.."|"..count_2.."|"..str_)
end
end
print("------------------------")
local myTable = {} -- Resetting my table, just for using it again :)
for i=1,count_1 do
for j=1,#tempTable[i] do
print("tempTable["..i.."]["..j.."] = "..tempTable[i][j])
if(j==1)then myTable[i] = tempTable[i][j] end
end
end
print("------------------------")
for i=1,#myTable do
print("myTable["..i.."] = "..myTable[i])
end
--[[ So now you will have a multidimensional array tempTable with
elements as:
tempTable = {{House,home,flat},
{40},
{Bed,bunk},
{20},
{Mirror},
{10}}
So you can simply take any random/desired value from each.
I am taking any of the 3 from the string "House,home,flat" and
assigning it to var1 below:
--]]
var1 = tempTable[1][math.random(3)]
print("var1 ="..var1)
-- So, as per your need, you can check var1 as:
for i=1,#tempTable[1] do -- #tempTable[1] means the count of array 'tempTable[1]'
if(var1==tempTable[1][i])then
print("Ok")
break;
end
end
----------------------------------------------------------------
-- Here you can print myTable(if needed) --
----------------------------------------------------------------
for i=1,#myTable do
print("myTable["..i.."]="..myTable[i])
end
--[[ The output is as follows:
myTable[1]=House
myTable[2]=40
myTable[3]=Bed
myTable[4]=20
myTable[5]=Mirror
myTable[6]=10
Is it is what you are looking for..?
]]--
----------------------------------------------------------------
Keep coding............. :)