连接字典值和键,以及进一步的操作 [英] Concatenating dictionary values and keys, and further operations

查看:79
本文介绍了连接字典值和键,以及进一步的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来连接字典的每两个键和

它们对应的值。

例如,如果我有tiDict1 = tiDict1 = {''a' ':[1,2],''b'':[3,4,5]}我应该得到

tiDict2 = {''ab'':[1,2] [3, 4,5]}和类似的大号没有的dicts。

功能。

现在我想检查每一对以查看它们是否已连接...元素

此对将是一个从第一个列表和第二个列表....例如

为''ab''我想检查1和3是否连接,然后是1和4,然后是1和

5,然后是2和3,然后是2和4,然后是2和5.

此连接的信息在文本文件中如下:

1,''a'',2,'b''

3,'''',5,''a''

3, ''a'',6,''a''

3,'''',7,'b''

8,''a' ',7,''b''

..

..

这意味着1(输入''a'')和2 (类型''b'')连接,3和5连接

等等。

我无法弄清楚如何做到这一点。任何指针会有所帮助

这是我到现在为止编写的代码:

展开 | 选择 | 换行 | 行号

解决方案

Girish Sahani写道:

我编写了以下代码来连接字典中的每两个键和
它们对应的值。
例如,如果我有tiDict1 = tiDict1 = {''a'':[1,2],''b'':[3,4,5]}我应该得到
tiDict2 = {''ab'':[1, 2] [3,4,5]}和类似的大号没有的dicts。
现在我想检查每一对,看看它们是否已连接......
这一对的元素将是第一个列表中的一个,第二个列表中的一个...... .eg
对于''ab''我想检查1和3是否连接,然后是1和4,然后是1和
5,然后是2和3,然后是2和4,然后是2和5.
这个连接的东西的信息在一个文本文件中如下:
1,'''',2,''b''
3,''a'' ,5,'''''
3,'''',6,'''''
3,'''',7,'b''
8,'''',7,''b''


这意味着1(类型''a'')和2(类型''b'')连接,3和5连接
等。
我无法弄清楚如何做到这一点。任何指针都会有所帮助



Girish


看来你想要

字典中的每对列表的笛卡尔积,包括列表的产品(但你是

不要说为什么;-))。


我不确定以下是你想要的,或者它是非常的

高效,但也许会让你失望。它使用了一个函数

''xcombine''取自大卫的ASPN食谱中的食谱

Klaffenbach(2004)。


(它应该提供所有可能性,然后你在文件中查看)


Gerard


-------- -------------------------------------------------- ---------------


def nkRange(n,k):

m = n - k + 1

indexer = range(0,k)

vector = range(1,k + 1)

last = range(m,n + 1) )

收益率矢量

而vector!= last:

high_value = -1

high_index = -1

for i in indexer:

val = vector [i]

if val> high_value和val< m + i:

high_value = val

high_index = i

范围内的j(k - high_index):

vector [j + high_index] = high_value + j + 1

收益率矢量


def kSubsets(alist,k):

n = len(alist)

for nkRange(n,k)中的向量:

ret = []

for i in vector:

ret.append(alist [i-1])

收益率回报


data = {''a'': [1,2],''b'':[3,4,5],''c'':[1,4,7]}


对= list( kSubsets(data.keys(),2))+ [[k,k] for k in

data.iterkeys()]

打印对
$对于s成对的b $ b:对于x在xcombine(数据[s [0]],数据[s [1]])中的


print"% S,'%s '的',%S '' %s '的' " %(t [0],s [0],t [1],s [1])

-------------------- -------------------------------------------------- ---


1,''a'',1,''c''

1,''a'',4,'' c''

1,''a'',7,''c''

2,'''',1,''c''

2,''a'',4,''''

2,'''',7,'''''

1,''a'',3,'b''

1,'''',4,''b''

1 ,''a'',5,'b''

2,'''',3,'b''

2,''a '',4,''b''

2,''a'',5,'b''

1,''c'',3 ,''b''

1,''c'',4,''b''

1,''c'',5,''b ''

4,''c'',3,'b''

4,''c'',4,''b''

4,''c'',5,''b''

7,''c'',3,''b''

7,''c'',4,''b''

7,'' c'',5,''b''

1,''a'',1,''a''

1,''a'', 2,''a''

2,'''',1,''a''

2,''a'',2,'' a''

1,'c'',1,''c''

1,''c'',4,''c''

1,''c'',7,''c''

4,''c'',1,''c''

4,''c'',4,''c''

4,''c'',7,''c''

7 ,''c'',1,''c''

7,''c'',4,''c''

7,''c '',7,''c''

3,''b'',3,'b''

3,''b'',4 ,'b''

3,''b'',5,'b''

4,''b'',3,''b ''

4,''b'',4,'b''

4,''b'',5,'b''

5,''b'',3,''b''

5,''b'',4,''b''

5,''b'',5,'b''




Gerard Flanagan写道:

Girish Sahani写道:

我编写了以下代码来连接字典中的每两个键并且
他们相应的值。
例如,如果我有tiDict1 = tiDict1 = {''a'':[1,2],''b'':[3,4,5]}我应该得到
tiDict2 = {''ab'':[1,2] [3,4,5]}和类似的大号没有的dicts。
现在我想检查每一对,看看它们是否已连接......
这一对的元素将是第一个列表中的一个,第二个列表中的一个...... .eg
对于''ab''我想检查1和3是否连接,然后是1和4,然后是1和
5,然后是2和3,然后是2和4,然后是2和5.
这个连接的东西的信息在一个文本文件中如下:
1,'''',2,''b''
3,''a'' ,5,'''''
3,'''',6,'''''
3,'''',7,'b''
8,'''',7,''b''


这意味着1(类型''a'')和2(类型''b'')连接,3和5连接
等。
我无法弄清楚如何做到这一点。任何指针都会有所帮助



Girish

看起来你想要
词典中每对列表的笛卡尔积,包括列表与自己的产品(但你不是为什么;-))。

我不确定以下是你想要的,或者它是否非常高效,但也许它会让你失望。它使用了一个函数
''xcombine''取自David
Klaffenbach(2004)的ASPN食谱中的食谱。



http://aspn.activestate.com/ASPN/Coo.../Recipe/302478


我有一个以下格式的文本文件:


1,''a '',2,''b''

3,'''',5,'c''

3,''a'',6 ,''''

3,'''',7,'b''

8,''a'',7,''b ''

..

..

..

现在我需要通过阅读文件:

1)一个字典,数字为键,字母为数值。

例如,上面会给我一个字典,如

{1:'''',2:''b'',3:''a'',5:''c'',6:''c''........}

2)包含pa的列表每行的数字。

上面的formmat会给我列表

[[1,2],[3,5],[3,6] [3,7] [8,7] ......]


我为这两个写了以下代码,但问题是

行返回一个列表,如[" 1,'a'',2,''b''"," 3,'a'',5,''c"," 3,'' '',6,''c''" .....]

现在由于"在每一行周围,它被视为一个对象

,我无法访问一行的元素。

展开 | 选择 | Wrap | 行号

I wrote the following code to concatenate every 2 keys of a dictionary and
their corresponding values.
e.g if i have tiDict1 = tiDict1 = {''a'':[1,2],''b'':[3,4,5]} i should get
tiDict2={''ab'':[1,2][3,4,5]} and similarly for dicts with larger no. of
features.
Now i want to check each pair to see if they are connected...element of
this pair will be one from the first list and one from the second....e.g
for ''ab'' i want to check if 1 and 3 are connected,then 1 and 4,then 1 and
5,then 2 and 3,then 2 and 4,then 2 and 5.
The information of this connected thing is in a text file as follows:
1,''a'',2,''b''
3,''a'',5,''a''
3,''a'',6,''a''
3,''a'',7,''b''
8,''a'',7,''b''
..
..
This means 1(type ''a'') and 2(type ''b'') are connected,3 and 5 are connected
and so on.
I am not able to figure out how to do this.Any pointers would be helpful
Here is the code i have written till now:

Expand|Select|Wrap|Line Numbers

解决方案

Girish Sahani wrote:

I wrote the following code to concatenate every 2 keys of a dictionary and
their corresponding values.
e.g if i have tiDict1 = tiDict1 = {''a'':[1,2],''b'':[3,4,5]} i should get
tiDict2={''ab'':[1,2][3,4,5]} and similarly for dicts with larger no. of
features.
Now i want to check each pair to see if they are connected...element of
this pair will be one from the first list and one from the second....e.g
for ''ab'' i want to check if 1 and 3 are connected,then 1 and 4,then 1 and
5,then 2 and 3,then 2 and 4,then 2 and 5.
The information of this connected thing is in a text file as follows:
1,''a'',2,''b''
3,''a'',5,''a''
3,''a'',6,''a''
3,''a'',7,''b''
8,''a'',7,''b''
.
.
This means 1(type ''a'') and 2(type ''b'') are connected,3 and 5 are connected
and so on.
I am not able to figure out how to do this.Any pointers would be helpful


Girish

It seems you want the Cartesian product of every pair of lists in the
dictionary, including the product of lists with themselves (but you
don''t say why ;-)).

I''m not sure the following is exactly what you want or if it is very
efficient, but maybe it will start you off. It uses a function
''xcombine'' taken from a recipe in the ASPN cookbook by David
Klaffenbach (2004).

(It should give every possibility, which you then check in your file)

Gerard

-------------------------------------------------------------------------

def nkRange(n,k):
m = n - k + 1
indexer = range(0, k)
vector = range(1, k+1)
last = range(m, n+1)
yield vector
while vector != last:
high_value = -1
high_index = -1
for i in indexer:
val = vector[i]
if val > high_value and val < m + i:
high_value = val
high_index = i
for j in range(k - high_index):
vector[j+high_index] = high_value + j + 1
yield vector

def kSubsets( alist, k ):
n = len(alist)
for vector in nkRange(n, k):
ret = []
for i in vector:
ret.append( alist[i-1] )
yield ret

data = { ''a'': [1,2], ''b'': [3,4,5], ''c'': [1,4,7] }

pairs = list( kSubsets(data.keys(),2) ) + [ [k,k] for k in
data.iterkeys() ]
print pairs
for s in pairs:
for t in xcombine( data[s[0]], data[s[1]] ):
print "%s,''%s'',%s,''%s''" % ( t[0], s[0], t[1], s[1] )
-------------------------------------------------------------------------

1,''a'',1,''c''
1,''a'',4,''c''
1,''a'',7,''c''
2,''a'',1,''c''
2,''a'',4,''c''
2,''a'',7,''c''
1,''a'',3,''b''
1,''a'',4,''b''
1,''a'',5,''b''
2,''a'',3,''b''
2,''a'',4,''b''
2,''a'',5,''b''
1,''c'',3,''b''
1,''c'',4,''b''
1,''c'',5,''b''
4,''c'',3,''b''
4,''c'',4,''b''
4,''c'',5,''b''
7,''c'',3,''b''
7,''c'',4,''b''
7,''c'',5,''b''
1,''a'',1,''a''
1,''a'',2,''a''
2,''a'',1,''a''
2,''a'',2,''a''
1,''c'',1,''c''
1,''c'',4,''c''
1,''c'',7,''c''
4,''c'',1,''c''
4,''c'',4,''c''
4,''c'',7,''c''
7,''c'',1,''c''
7,''c'',4,''c''
7,''c'',7,''c''
3,''b'',3,''b''
3,''b'',4,''b''
3,''b'',5,''b''
4,''b'',3,''b''
4,''b'',4,''b''
4,''b'',5,''b''
5,''b'',3,''b''
5,''b'',4,''b''
5,''b'',5,''b''



Gerard Flanagan wrote:

Girish Sahani wrote:

I wrote the following code to concatenate every 2 keys of a dictionary and
their corresponding values.
e.g if i have tiDict1 = tiDict1 = {''a'':[1,2],''b'':[3,4,5]} i should get
tiDict2={''ab'':[1,2][3,4,5]} and similarly for dicts with larger no. of
features.
Now i want to check each pair to see if they are connected...element of
this pair will be one from the first list and one from the second....e.g
for ''ab'' i want to check if 1 and 3 are connected,then 1 and 4,then 1 and
5,then 2 and 3,then 2 and 4,then 2 and 5.
The information of this connected thing is in a text file as follows:
1,''a'',2,''b''
3,''a'',5,''a''
3,''a'',6,''a''
3,''a'',7,''b''
8,''a'',7,''b''
.
.
This means 1(type ''a'') and 2(type ''b'') are connected,3 and 5 are connected
and so on.
I am not able to figure out how to do this.Any pointers would be helpful


Girish

It seems you want the Cartesian product of every pair of lists in the
dictionary, including the product of lists with themselves (but you
don''t say why ;-)).

I''m not sure the following is exactly what you want or if it is very
efficient, but maybe it will start you off. It uses a function
''xcombine'' taken from a recipe in the ASPN cookbook by David
Klaffenbach (2004).



http://aspn.activestate.com/ASPN/Coo.../Recipe/302478


I have a text file in the following format:

1,''a'',2,''b''
3,''a'',5,''c''
3,''a'',6,''c''
3,''a'',7,''b''
8,''a'',7,''b''
..
..
..
Now i need to generate 2 things by reading the file:
1) A dictionary with the numbers as keys and the letters as values.
e.g the above would give me a dictionary like
{1:''a'', 2:''b'', 3:''a'', 5:''c'', 6:''c'' ........}
2) A list containing pairs of numbers from each line.
The above formmat would give me the list as
[[1,2],[3,5],[3,6][3,7][8,7]......]

I wrote the following codes for both of these but the problem is that
lines returns a list like ["1,''a'',2,''b''","3,''a'',5,''c","3,''a'',6,''c''".....]
Now due to the "" around each line,it is treated like one object
and i cannot access the elements of a line.

Expand|Select|Wrap|Line Numbers


这篇关于连接字典值和键,以及进一步的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆