目前我正在查看这样的列表列表: [[x,y,z],[x1,y1,z1],[x2,y2,z2] ......]
我想循环并将x1与x2和y1与y2进行比较,如果两者相等,我想将z2的值添加到z1并将它们变成新的列表。
library(ggplot2)
Final.Buscount.Alias <- data.frame(Values=rep(c("A","B", "C", "D", "E"), times=5))
Final.Buscount.Alias$Values = factor(Final.Buscount.Alias$Values,
levels=LETTERS[c(3,4,2,1,5)])
Final.Buscount.Alias$Probabilities <- c(5/9,4/9,0,0,0, 0, 6/9, 0, 3/9,0, 0,0,9/9,0,0, 0,0,2/9,7/9,0, 0,0,4/9,0,5/9)
Final.Buscount.Alias$Columns <- rep(c("a-2","a-1", "a", "a+1", "a+2"), each=5)
Final.Buscount.Alias$Columns = factor(Final.Buscount.Alias$Columns,
levels=unique(Final.Buscount.Alias$Columns))
ggplot(Final.Buscount.Alias, aes(x=Columns, y=Probabilities, fill=Values)) +
geom_col(width=1) +
scale_fill_manual(values=c(A="cyan",B="magenta2",C="gold",D="gray",E="darkolivegreen3", "black"), breaks=LETTERS[1:5]) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(breaks=seq(0, 15/9, by=3/9),
labels=c("0", paste0(seq(3,15,3),"/9")),
expand=c(0,0)) +
geom_vline(xintercept=c(1.5, 2.5, 3.5, 4.5), color="gray30") + # Darkened this to make it obvious where the lines are. Remove this line of code if you want the colors to abut each other.
labs(x="Real Count", y="Probabilities including alias") +
theme(panel.border=element_rect(size=2, fill=NA))
但是我需要它是连续的,所以如果x3和y3也等于x2和y2那么它们将像以前一样加在一起,并且只有当x和y的下一个值为no时才会添加到新列表中比现在的更长。
有效的,如果一个链
if x1 == x2 and y1 == y2:
list.append(x1, y1, z1+z2)
评估为true然后新列表应该只包含[x,y,z1 + z2 + z3 + z4]的一个条目
到目前为止,我已经尝试了for循环中的递归函数(以及列表推导)但是我遇到了各种各样的问题而且我不知道人们通常会如何做这类事情。
期望的输出:
x1 == x2 == x3 == x4 and y1 == y2 == y3 == y4
到目前为止我的代码(它有点乱。我在中途感到沮丧,并尝试更改了很多。):
[[x1, y1, z1+z2+z3+z4], [x5, y5, z5], [x6, y6, z6+z7]...]
答案 0 :(得分:1)
这要求您检查列表y中每个x的每个值,这将在计算上变得昂贵。但它确实有效。
x = [[ 0, 1, 2],
[ 1, 1, 6],
[ 2, 2, 10],
[ 2, 2, 25],
[ 3, 2, 14]]
y = [x[0]]
for val in x[1:]:
for ix, i in enumerate(y):
if val[0] == i[0] and val[1] == i[1]:
i[2] += val[2]
break
else: y.append(val)
print(y)
答案 1 :(得分:1)
UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayPause)];
tapGestureRec.allowedPressTypes = @[@(UIPressTypePlayPause)];
[self.playerViewController.view addGestureRecognizer:tapGestureRec];
没有列表理解:
-(void)PlayPause{
[self.playerViewController.player play];
NSLog(@"Do Anything or Nothing");}
<强>输出强>:
[player addObserver:self forKeyPath:@"rate" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
//When rate is 0.0, it means the player has stopped
if (![self.player rate]){
if(self.isLiveStream){
[self.playerViewController.player play]; // Disable pause in Live stream
}
}
<强>输出强>:
self.playerViewController.requiresLinearPlayback = true;
在这个单行的最里面,你会看到import itertools
l = [[1, 2, 3], [1, 2, 4], [1, 3, 5], [1, 3, 6], [1, 3, 7]]
print ([list(key) + [sum(group[-1] for group in groups)] for key, groups in itertools.groupby(l, lambda x: (x[0], x[1]))])
。这样做的目的是将它找到的连续值分组到关键函数(result = []
for key, groups in itertools.groupby(l, lambda x: (x[0], x[1])):
group_res = list(key)
group_res.append(sum(group[-1] for group in groups))
result.append(group_res)
)。在这种情况下,我们按每个列表中的前两个元素对值进行分组。
此函数返回一个元组,该元组由[[1, 2, 7], [1, 3, 18]]
和为该键连续匹配的值组成。这允许我们执行l = [[0, 1, 2], [1, 1, 6], [2, 2, 10], [2, 2, 25], [3, 2, 14]]
[list(key) + [sum(group[-1] for group in groups)] for key, groups in itertools.groupby(l, lambda x: (x[0], x[1]))]
获取密钥并将其转换为列表,然后将其最后元素的总和附加到列表中。