问题列表 - 第11704页

Python字典到URL参数

我试图将Python字典转换为字符串以用作URL参数.我确信有一种更好,更Pythonic的方式来做到这一点.它是什么?

x = ""
for key, val in {'a':'A', 'b':'B'}.items():
    x += "%s=%s&" %(key,val)
x = x[:-1]
Run Code Online (Sandbox Code Playgroud)

python dictionary url-parameters

113
推荐指数
3
解决办法
7万
查看次数

将元组列表转换为字典

我正在寻找一种方法来转换像这样的元组列表:

[(1,4),(2,4),(3,4),(4,15),(5,15),(6,23),(7,23),(8,23),(9,15),(10,23),(11,15),(12,15)]
Run Code Online (Sandbox Code Playgroud)

进入这样的字典:

{4:[1,2,3] ,15:[4,5,9,11,12], 23:[6,7,8,10]}
Run Code Online (Sandbox Code Playgroud)

来自每个元组的第二个元素变为字典键,并且与该键相关联的所有第一元组元素都存储在值列表中.

你能告诉我怎么做吗?

python dictionary list

32
推荐指数
2
解决办法
4万
查看次数

刷新表单上的所有控件

我需要刷新表单中的控件,因为我使用this.Refresh()但表单是闪烁的.如何顺利刷新控件?

使用Application.DoEvents()顺利,但刷新使用是个好主意 Application.DoEvents()吗?

c#

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

C#字符串重复问题

我使用的是VSTS 2008 + C#+ .Net 3.0.我有两个输入字符串,我认为它们是不同的.但是下面的C#代码认为它们是相同的,并抛出System.Data.ConstraintException,表示Column Name被限制为唯一,但值已经存在.有什么想法有什么不对?

这是我的代码和我的输入字符串,

我输入字符串的十六进制视图,

http://i30.tinypic.com/2anx2b.jpg

我的输入字符串的记事本视图,

http://i30.tinypic.com/2q03hn4.jpg

我的代码,

    static void Main(string[] args)
    {
        string[] buf = new string[] { "2ch", "???" };

        DataTable bulkInserTable = new DataTable("BulkTable");
        DataColumn column = null;
        DataRow row = null;

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "Name";
        column.ReadOnly = true;
        column.Unique = true;
        bulkInserTable.Columns.Add(column);

        foreach (string item in buf)
        {
            row = bulkInserTable.NewRow();
            row["Name"] = item;
            bulkInserTable.Rows.Add(row);
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑1:

我的困惑是,为什么C#Dictionary认为它们不同,但DataSet认为它们是相同的.任何使行为一致的解决方案?这是我的代码来证明C#Dictionary认为它们不同,返回buf数组有两个元素.

            Dictionary<string, bool> dic = new Dictionary<string, …
Run Code Online (Sandbox Code Playgroud)

.net c# string ado.net visual-studio-2008

2
推荐指数
3
解决办法
1175
查看次数

k-means在python中实现集群实现,内存不足

 注意:此问题底部的更新/解决方案

作为产品推荐引擎的一部分,我试图根据他们的产品偏好来分割我的用户,从使用k-means聚类算法开始.

我的数据是表格的字典:

prefs = {
    'user_id_1': { 1L: 3.0f, 2L: 1.0f, },
    'user_id_2': { 4L: 1.0f, 8L: 1.5f, },
}
Run Code Online (Sandbox Code Playgroud)

产品ID是多头,评级是浮动.数据稀少.我目前有大约60,000名用户,其中大多数只评价了少数产品.使用defaultdict(float)实现每个用户的值字典以简化代码.

我对k-means聚类的实现如下:

def kcluster(prefs,sim_func=pearson,k=100,max_iterations=100):
    from collections import defaultdict

    users = prefs.keys()       
    centroids = [prefs[random.choice(users)] for i in range(k)]

    lastmatches = None
    for t in range(max_iterations):
        print 'Iteration %d' % t
        bestmatches = [[] for i in range(k)]

        # Find which centroid is closest for each row        
        for j in users:
            row = prefs[j]
            bestmatch=(0,0)

            for i in range(k):
                d …
Run Code Online (Sandbox Code Playgroud)

python

6
推荐指数
1
解决办法
3744
查看次数

如何创建可变大小的UITableViewCell?

我似乎无法让文本实际跨越多行.高度看起来正确.我错过了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell =  [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];

    CGRect frame = cell.contentView.bounds;
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
    myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
    [cell.contentView addSubview:myLabel];

    [myLabel release];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
    CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];

    return size.height + 20;    
}
Run Code Online (Sandbox Code Playgroud)

另外,我错过了哪些标签看起来比表格单元格更长? 替代文字http://i31.tinypic.com/15chnjd.gif

iphone cocoa-touch objective-c cs193p uitableview

9
推荐指数
1
解决办法
6382
查看次数

比较Boost Graph Library创建的2个图表

这可能是一个相当新手甚至是错误的问题所以请原谅.有没有办法比较使用Boost Graph Library创建的2个图表=>在内存中创建的1个图表和从存档中加载的第2个图表(即第2个是先前序列化的)?

我没有在BGL的文档中看到运算符==,但不确定这是否意味着我必须同时编写遍历和比较.任何指向教程,参考页面或示例的指针都会非常有用

在此先感谢Ganesh

c++ boost

5
推荐指数
2
解决办法
2176
查看次数

在哪里查看用户电子邮件尚不存在?

我有一个创建对象的帐户对象,就像这样;

public class Account
{
    public ICollection<User> Users { get; set; }

    public User CreateUser(string email)
    {
        User user = new User(email);
        user.Account = this;
        Users.Add(user);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的服务层中,创建新用户时,我调用此方法。但是,有一个规则,即用户电子邮件对于该帐户必须是唯一的,那么该去哪里呢?对我来说,它应该放在CreateUser方法中,并带有额外的一行,该行仅检查电子邮件对于帐户而言是唯一的。

但是,如果要这样做,则将需要加载该帐户的所有用户,这对我来说似乎有些负担。最好在数据库中查询用户的电子邮件-但在该方法中这样做将需要在account对象中存储一个库,不是吗?答案可能就是从存储库加载帐户而不是这样做。

var accountRepository.Get(12);
//instead do
var accountRepository.GetWithUserLoadedOnEmail(12, "someone@example.com");
Run Code Online (Sandbox Code Playgroud)

然后,该帐户对象仍可以检查Users集合中的电子邮件,并且如果找到该电子邮件对象,则将它急切地装入其中。

这样行吗?你会怎么做?

我正在使用NHibernate作为ORM。

domain-driven-design

5
推荐指数
1
解决办法
1623
查看次数

在Pipeline.Invoke抛出之后捕获C#中的Powershell输出

我正在从C#应用程序运行Powershell测试脚本.由于cmdlet错误导致pipe.Invoke()抛出异常,脚本可能会失败.

我能够捕获关于异常所需的所有信息,但我希望能够显示脚本的输出到那时为止.我没有运气,因为抛出异常时结果似乎为null.

有什么我想念的吗?谢谢!

m_Runspace = RunspaceFactory.CreateRunspace();
m_Runspace.Open();
Pipeline pipe = m_Runspace.CreatePipeline();
pipe.Commands.AddScript(File.ReadAllText(ScriptFile));
pipe.Commands.Add("Out-String");
try {
   results = pipe.Invoke();
}
catch (System.Exception)
{
   m_Runspace.Close();
   // How can I get to the Powershell output that comes before the exception?
}
Run Code Online (Sandbox Code Playgroud)

c# powershell powershell-1.0

11
推荐指数
2
解决办法
3万
查看次数

13
推荐指数
2
解决办法
11万
查看次数