I have a df listing a number of areas (df$area) and the areas which these shares a border with (df$next_area).
Starting from it i want to get a similar df but with the neighbour of its neighbour.
I wrote the following, which works, but appear extremely convoluted.
Was there a better solution?
library(dplyr)
library(tidyr)
df <- data.frame(area=c("A","A","B","B","C","C","C","D"),next_area=c("B","C","A" ,"C","A","B","D","C") )
df <- df %>% group_by(area) %>%
summarize(next_area = list(sort(unique(as.character(next_area)))))
df$next_area_exploded <- df$next_area
for(i in 1:nrow(df)){
for(j in …Run Code Online (Sandbox Code Playgroud) 目前,我正在研究一个问题。给我一个列表,该列表的元素可以包含其他列表,列表列表或整数。例如,我可能会收到:
[[[[], 1, []], 2, [[], 3, []]], 4, [[[], 5, []], 6, [[], 7, [[], 9, []]]]]
Run Code Online (Sandbox Code Playgroud)
我的目标是解析数组,并且仅将整数附加到新列表中。到目前为止,这是我所做的:
def fun(a):
if a == []:
return None
elif type(a) == int:
print("Found a digit: ", a)
return a
for i in a:
fun(i)
Run Code Online (Sandbox Code Playgroud)
当前,此函数以递归方式遍历列表并成功找到每个整数。现在,我遇到了将这些整数附加到新列表,然后在最后返回该列表的问题。输出应如下所示:
[1,2,3,4,5,6,7,9]
Run Code Online (Sandbox Code Playgroud)
有指针吗?
我正在尝试转换以下字符串:
"CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain"
转换成这种形式(字符串数组列表):
[
{"CN" , "Test"},
{"OU" , "ABC"},
{"OU" , "Company"},
{"DC" , "CFLA"},
{"DC" , "domain"},
]
Run Code Online (Sandbox Code Playgroud)
我已经尝试过将字符串转换成字典列表。我尝试将字符串拆分两次。首先,基于=。
以下是我使用的代码:
var result = str.Split(',')
.Select(line => line.Split('='))
.ToDictionary(b=> b[0], b=> b[1])
.ToList();
Run Code Online (Sandbox Code Playgroud)
但这给了我以下异常:'System.ArgumentException:'已经添加了具有相同键的项',正确的做法是将“ OU”和“ DC”作为键重复。
我现在想要的是将该字符串拆分/转换/处理为字符串数组列表(如上所示)。可能吗?
或以其他解决方案指导我。提前致谢。
PS,我有数百个这样的字符串,我只想要每个字符串中第一个“ OU”的值。
我正在尝试对二维列表进行排序,同时优先考虑对其中一个维度进行排序。
例:
twod_list = [[116.2,103.4],[124.9,103.4],[129.5,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[125.9,103.4],[146.3,109.3],[135.8,103.4]]
Run Code Online (Sandbox Code Playgroud)
我试图按以下顺序对此进行排序,首先沿第二维排序,然后沿第一维排序,同时保持第二维的顺序。所以我的预期排序列表如下所示
sorted_twod_list = [[116.2,103.4],[124.9,103.4],[125.9,103.4],[129.5,103.4],[135.8,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[146.3,109.3]]
Run Code Online (Sandbox Code Playgroud)
我尝试先沿第二维排序,然后再按第一维排序,但这样做会改变顺序。
twod_list = [[116.2,103.4],[124.9,103.4],[129.5,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[124.9,103.4],[146.3,109.3],[135.8,103.4]]
twod_sorted_on_y = sorted(twod_list,key=lambda l:l[1])
twod_sorted = sorted(twod_sorted_on_y,key=lambda l:l[0])
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
twod_sorted = [[116.2, 103.4],[119.1, 109.3],[124.9, 103.4],
[124.9, 103.4],[129.5, 103.4],
[135.8, 103.4],[144.6, 103.4],[146.3, 109.3],[148.9,103.4]]
Run Code Online (Sandbox Code Playgroud)
我们也可以使用if条件来完成此操作,但是我想知道是否使用numpy或pandas存在捷径?
我想将数据框转换为列表。请参见表1中的输入。请参见表2中的输出。从环境中打开R中的列表时。名称-以下名称clus1,clus2 ...类型-应包含列V1中的值值-长度为3的列表
Table 1
V1 V2 V3
clus1 10 a d
clus2 20 b e
clus3 5 c f
Run Code Online (Sandbox Code Playgroud)
Table 2
$`clus1`
[1] "a" "d"
$`clus2`
[2] "b" "e"
$`clus3`
[2] "c" "f"
Run Code Online (Sandbox Code Playgroud) 例如,给定
def expensive_call(x):
print(x)
if x == "d":
return x
def expensive_call_2(x, y):
print(x)
print(y)
return x + y
a = [expensive_call("a"), expensive_call_2("b", "c"), expensive_call("d")]
next((e for e in a if e is not None), 'All are Nones')
Run Code Online (Sandbox Code Playgroud)
输出是
a
b
c
d
Out[22]: 'bc'
Run Code Online (Sandbox Code Playgroud)
由于expensive_call("d")急切地进行了评估,因此请注意,即使next在第二次呼叫出现呼叫短路且输出为“ bc”的情况下,也会打印“ d ”。
我正在对列表中的调用进行硬编码a,而a不必是列表数据结构。
一种可能的解决方案如下:
a = ['expensive_call("a")', 'expensive_call_2("b", "c")', 'expensive_call("d")']
def generator():
for e in a:
r = eval(e)
if r is not None:
yield …Run Code Online (Sandbox Code Playgroud) 简短而甜美的问题是,我正在解析一个数字列表([Integer]),并且我想将该列表转换为嵌套元组,因此例如,列表[1,2,3,4]为(1 ,(2,(3,4)))在我看来这可以通过折叠操作来实现。我认为我遇到的问题是类型不是事先确定的,从而使元组嵌套可能无限。
根据评论进行编辑
感谢您的答复,根本的问题是分配作业,所以这就是为什么较大的问题的细节很少的原因,但是为了使您不致怀疑我可以扩大一点,尽管我不是在寻找答案。扩张。我的问题可以归结为上下文无关语法的正确递归和正确关联部分的问题,其中
A-> n A
所以我可以得到一个像'nnnn A'这样的表达式,它可以解析为'n(n(n(n A)))'(A确实有其他终端)。我可以将其解析为'[n,n,n,n] A',这就是为什么我想要转换的原因(我知道有更好的方法,我只是在努力寻找它)。
我试图遍历列表的递归并检查是否所有值都等于0,即时消息错误是:
* Couldn't match expected type `[Integer]' with actual type `Bool'
* In the second argument of `(:)', namely `allZero s'
In the expression: 0 : allZero s
In an equation for `allZero': allZero (0 : s) = 0 : allZero s
Run Code Online (Sandbox Code Playgroud)
我的代码是:
allZero :: [Int] -> Bool
allZero (0:_) = True
allZero (0:s) = 0 : allZero s
allZero (_:s) = False;
allZero _ = False
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到此错误,因为allZero (0:s) = 0 : allZero s我给了它正确的参数,即列表“ s”
我有下面的代码。我需要根据键值替换地图的值。
定义类具有以下变量:
private Map<String, String> rows;
private String tableName;
Run Code Online (Sandbox Code Playgroud)
我试过下面的代码。可以用其他更简单的方法来实现吗?
/*Definitions*/
Definition definitionObj = new Definition();
definitionObj.setTableName("TABLE1");
Map map = new HashMap<String,String>();
map.put("1","one");
map.put("2","two");
map.put("3","threee");
definitionObj.setRows(map);
Definition definitionObj1 = new Definition();
definitionObj1.setTableName("TABLE2");
Map map1 = new HashMap<String,String>();
map1.put("4","one");
map1.put("5","two");
map1.put("6","threee");
definitionObj1.setRows(map1);
String key1 = "2";
String value1 = "modifiedvalue"; /*the value that i need to set*/
List<Definition> myList = Arrays.asList(definitionObj,definitionObj1);
Definition result1 = myList.stream()
.filter(x -> "TABLE1".equals(x.getTableName()))
.findAny()
.orElse(null);
result1.getRows().replace(key1,value1);
System.out.println(result1);
Run Code Online (Sandbox Code Playgroud) 遍历列表并删除每个项目时,为什么其他所有项目都被跳过而不被删除?(我意识到这样做可能是不好的做法,我只是想了解发生了什么)
lst=[0,1,2,3,4,5,6,7,8,9]
for item in lst:
lst.remove(item)
print(lst)
Run Code Online (Sandbox Code Playgroud)
预期输出:[]
实际输出:[1, 3, 5, 7, 9]