标签: key-value

将 Objective-C NSNumber 对象分配给 int 变量的说明?

谁能解释为什么这在 Objective-C 中有效?我希望它给出一个错误,因为一个对象被分配给一个 int 变量。我知道它确实有效,这很棒,但我想知道为什么允许这样做?

int i = [NSNumber numberWithInt:123];
Run Code Online (Sandbox Code Playgroud)

此外,这似乎存储了错误的值(即,在我的系统上,当我使用 NSLog 打印出“i”的值时,我得到的是“252711”而不是 123);但是,只有在我的主代码中声明和分配变量时。如果我将其声明为对象的实例变量,然后使用键值编码为其赋值,则它可以正常工作:

对象实例变量...

@interface myObject : NSObject
{
    int anInt;
}
Run Code Online (Sandbox Code Playgroud)

主要代码...

[myObject setValue:[NSNumber numberWithInt:123] forKey:@"anInt"];
NSLog(@"%@", [myObject valueForKey:@"anInt"]);
Run Code Online (Sandbox Code Playgroud)

这段代码按预期打印出“123”,但我不确定为什么在使用上面看似相似的方法时它无法正常工作。

objective-c instance-variables key-value key-value-coding

0
推荐指数
1
解决办法
600
查看次数

如何在Python中将2个字典变成1个?

我有两本词典:

fruit1 = {'apple': 3, 'banana': 1, 'cherry': 1}
fruit2 = {'apple': 42, 'peach': 1}
Run Code Online (Sandbox Code Playgroud)

我想要的最终结果是:

inv3 = {'apple': 45, 'banana': 1, 'cherry': 1, 'peach': 1}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试过这个示例代码,因为这个输出看起来几乎与我想要的类似,只是它没有按照我想要的方式打印,而是接近:

d1 = {'apple': 3, 'orange': 1,} 
d2 = {'apple': 42, 'orange': 1}

ds = [d1, d2]
d = {}

for k in d1.keys():
    d[k] = tuple(d[k] for d in ds)
print(ds)
Run Code Online (Sandbox Code Playgroud)

输出将是这样的:

[{'apple': 3, 'orange': 1}, {'apple': 42, 'orange': 1}]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用示例代码输入我的两个词典时:

fruit1 = {'apple': 3, 'banana': 1, 'cherry': 1}
fruit2 = {'apple': 42, …
Run Code Online (Sandbox Code Playgroud)

python dictionary list key-value

0
推荐指数
1
解决办法
109
查看次数

将句子中的键替换为其值

我有一本字典和一个清单

dict1= {'good':'bad','happy':'sad','pro':'anti'}
Run Code Online (Sandbox Code Playgroud)
list1=['she is good','this is a product','they are pro choice']
Run Code Online (Sandbox Code Playgroud)
newlist=[]
for index, data in enumerate(list1):
    for key, value in dict1.items():
        if key in data:
            list1[index]=data.replace(key, dict1[key])
            newlist.append(list1[index])
Run Code Online (Sandbox Code Playgroud)
The output I get is
['she is bad','this is a antiduct','they are anti choice']

desired output is
['she is bad','this is a product','they are anti choice']
Run Code Online (Sandbox Code Playgroud)

我该如何修复它?它也正在取代产品的专业版。我只希望它作为独立词存在时取代 pro。

python dictionary key list key-value

0
推荐指数
1
解决办法
46
查看次数

Java TreeSet<Pair<Integer, Integer>> 按 Pair 元素的值排序,如何插入与现有对具有相同值的新对?

我当前有一个包含 Pair 类型元素的 TreeSet,并且我按其值降序对它们进行排序。

以下是原始 Pair 类文档的链接:https://docs.oracle.com/javase/9​​/docs/api/javafx/util/Pair.html

这是声明:

TreeSet<Pair<Integer, Integer>> sortedSet = new TreeSet<Pair<Integer, Integer>>((a,b) -> b.getValue() - a.getValue());
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我尝试在集合中插入几对,例如: Pair(4, 51)、Pair(8, 85)、Pair(1, 16)、Pair(2,51)、Pair( 2,51) 未插入。

插入对的代码:

sortedSet.add(new Pair<Integer, Integer>(4, 51));
sortedSet.add(new Pair<Integer, Integer>(8, 85));
sortedSet.add(new Pair<Integer, Integer>(1, 16));
sortedSet.add(new Pair<Integer, Integer>(2, 51));
Run Code Online (Sandbox Code Playgroud)

我设法找出原因是因为已经存在一个具有相同值的对元素 - P(4,51),但是,它们具有不同的键,因此它们是不同的元素,并且我期望 Pair(2,51)插入到 P(4,51) 之前或之后。

我有办法解决这个问题吗?

java key-value comparator treeset

0
推荐指数
1
解决办法
649
查看次数

Python:字典中是否存在密钥(Python 3.1)

arguments=dict()
if (arg.find("--help") == 0):
  arguments["help"] = 1
if help in arguments:
  #this doesnt work

print(arguments["help"]) # This will print 1
Run Code Online (Sandbox Code Playgroud)

无法确定是否已定义某个键..has_key已在2.7中弃用,我还没有找到其他解决方案.我究竟做错了什么?

python dictionary key-value

-1
推荐指数
1
解决办法
1291
查看次数

列出<double>以获取字典查找特定项的值

我在列表中有一个包含5个不同双打的词典.我知道列表中每个项目的顺序.我正在尝试找到一行代码,我可以在给定密钥的情况下查找列表中的特定值.

所以类似于:

double myDbl = myDict["key"][value[3]];
Run Code Online (Sandbox Code Playgroud)

这样的事情是可能的,我无法在任何地方找到它.谢谢

c# dictionary list key-value

-1
推荐指数
1
解决办法
391
查看次数

按 r 中的键值求和

我有一个看起来像这样的数据集。您可以看到列 X2 具有键值。我想通过 X2 列中的键值对 X1 列中的对值求和。我一直在尝试使用 ddply 或聚合,但我遇到了问题。有没有人有一个简单的方法来做到这一点?

> X1=rnorm(30,mean=20, sd=10)
> X2=rep(1:15,2)
>df= data.frame(X1,X2)
          X1 X2
1  10.065545  1
2   6.938315  2
3   5.657782  3
4  11.371521  4
5  37.037992  5
6  13.443674  6
7   8.868314  7
8   5.944946  8
9  18.493563  9
10 16.059931 10
11 22.520693 11
12 17.172936 12
13 28.676676 13
14 27.200911 14
15 30.560696 15
16 22.435021  1
17 31.143627  2
18 19.234473  3
19 29.329251  4
20 18.420183  5
21 13.184905 …
Run Code Online (Sandbox Code Playgroud)

aggregate r key key-value plyr

-1
推荐指数
1
解决办法
1691
查看次数

将分隔键值对的数据框列拆分为新列

我有一个包含 5 列的数据框,其中 4 个是“正常”值,第 5 个是一组以分隔符分隔的键值对形式

key1 = value1 | key2 = value2 | key3 = value3
Run Code Online (Sandbox Code Playgroud)

单行中没有重复的键,但不能保证它们在每一行中的顺序相同,或者每行中键/值对的数量都相同。

将值放入 ~15 列并为其键正确命名的最简单方法是什么?

r key-value

-1
推荐指数
1
解决办法
806
查看次数

像地图但有一个价值的东西?

我使用地图来快速查找名称,这些名称可以是大约200并且总是不同的值或位置.所以现在我写这个:

map<string, string> names;
void function( string name)
{
  names.insert(pair<string,string>(name,name));
}
Run Code Online (Sandbox Code Playgroud)

所以关键和价值永远相同.使用它还是存在更好的东西是好的吗?我不想使用矢量,因为我经常找到名字,矢量总是连续这样做.

c++ key-value

-2
推荐指数
1
解决办法
42
查看次数

Ruby调用哈希值返回nil

我有一个代码

def pitch_class(note)
    note_hash = {:C=>0, :D=>2, :E=>4, :F=>5, :G=>7, :A=>9, :B=>11} 
    note_hash[:note]
end
Run Code Online (Sandbox Code Playgroud)

但每当我尝试调用其中的值时,返回nil.

pitch_class("C")
#=> nil
Run Code Online (Sandbox Code Playgroud)

如何使用键作为参数调用值?谢谢!

ruby hash key-value

-3
推荐指数
1
解决办法
620
查看次数