相关疑难解决方法(0)

修改Python列表中的所有元组

我有一个包含标准格式的元组的列表:

bar_list = [(bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4)...] 
Run Code Online (Sandbox Code Playgroud)

虽然我想迭代列表中的每个元组,并为每个元组进行特定的修改,例如:

foo0 = bar1
foo1 = get_foo(foo0) #get_foo(var) being a function
foo2 = bar2
foo3 = bar3/2
Run Code Online (Sandbox Code Playgroud)

然后在另一个列表中重新打包重估的元组:

foo_list = [(foo1, foo2, foo3), (foo1, foo2, foo3), (foo1, foo2, foo3)...]
Run Code Online (Sandbox Code Playgroud)

我怎么能做到这一点?

python tuples list

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

Python map vs list comprehension

比较这些时,您认为哪种更直观/更容易阅读?

>>> [ord(i) for i in 'some string']
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]

>>> map(ord,'some string')
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]
Run Code Online (Sandbox Code Playgroud)

lambda/map方式有什么好处吗?

python lambda loops

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

Python:获取数据的函数总是返回None

我有这个词典列表:

cust = [
        {"id": 1, "name": u"name 1", "bill_amount": 1000},
        {"id": 2, "name": u"name 2", "bill_amount": 5000},
        {"id": 3, "name": u"name 3", "bill_amount": 7600},
        {"id": 4, "name": u"name 4", "bill_amount": 30}
       ]
Run Code Online (Sandbox Code Playgroud)

我想得到一个名单的清单.

试试这个:

def getName(x): x["name"]
print map(getName, cust)
Run Code Online (Sandbox Code Playgroud)

返回此:

[None, None, None, None]
Run Code Online (Sandbox Code Playgroud)

为什么?我错过了一些明显的东西吗

python dictionary list map

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

删除列表中的元组

我有这些清单:

sqvaluelist = []
valuelist = [(10.5,), (20.5,), (21.5,), (70.0,), (34.5,)]
Run Code Online (Sandbox Code Playgroud)

我想在valuelist上应用此代码:

for value in valuelist:
    valuesquared = value*value
    sqvaluelist.append(valuesquared,)
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

TypeError: can't multiply sequence by non-int of type 'tuple'

我想我知道这个错误背后的原因,这是因为每个值都在一个单独的元组中.

我的问题是,是否有任何方法可以将这些值从各自的元组中删除,然后将它们转换为类似的列表

valuelist = [10.5, 20.5, 21.5, 70.0, 34.5]
Run Code Online (Sandbox Code Playgroud)

没有手动编辑现有列表的结构,以便可以执行for循环?

编辑:我道歉!他们实际上是元组!在每个值后添加逗号.抱歉!

python

0
推荐指数
2
解决办法
1615
查看次数

用lambda减少设置

给定一个集合,其中每个元素都是一个字符串,如何将集合减少为一个整数,这个整数是这些字符串长度的总和?

setA = ("hi", "hello", "bye")
reduce(lambda .... for word in setA)
Run Code Online (Sandbox Code Playgroud)

使用一些lambda函数调用reduce应该返回10(2 + 5 + 3).

我想,我可以用几个lambdas做到这一点,但必须有一个更清洁的方式.

python functional-programming

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

你如何在python中添加一系列数字

我有一个奇数列表但我仍然需要添加它们:

for n in range(100, 200):
   if n % 2 == 1:
       print sum([n])
Run Code Online (Sandbox Code Playgroud)

python

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

列表到单个字符串的列表

假设我有一份清单说明

A = [[1,1,1,1],[2,2,2,2]]
Run Code Online (Sandbox Code Playgroud)

我想创建两个字符串

'1111'

'2222'
Run Code Online (Sandbox Code Playgroud)

我们如何在python中执行此操作?

python string list

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

总结列表中的十进制数。Python

我试图打印训练结果,但是无法总结测试精度。

q=(['0.50000', '0.56250', '0.50000', '0.50000'])

sum(q)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)

python sum

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

如何对列表中的值求和?

我的代码:

distance = ['0.12', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.02', '0.12', '0.01', '0.13', '0.02', '0.12', '0.02', '0.12', '0.02', '0.13', '0.02', '0.13', '0.02', '0.12', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.02', '0.13', '0.01', '0.13', '0.02', '0.13', '0.01']
Run Code Online (Sandbox Code Playgroud)

值来自CSV文件并将其附加到距离列表.如何将它们相加以便我可以获得总距离?我已尝试在网站上给出的一些答案,但他们没有工作:(

python sum list

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

从特殊字符列表中提取整数

我在python中有以下列表

a = ['[', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', '0', ',', ' ', '1', ',', ' ', '1', ',', ' ', '0', ']'].

我想从中提取整数,以便我可以获得一个新列表 [1,0,0,0,0,1,0,1,1,0].我正在使用python 3.x,我已经尝试了链接中给出的解决方案如何从字符串列表中提取数字?但不幸的是我得到一个空列表[].有人能帮助我吗?

python list python-3.x

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

将python列表中的所有数字转换为1或0

我有一个整数列表,它看起来像这样:

[10, 8, 4, 4, 13, 1, 1, 1, 1, 6, 1, 2, 1, 1, 0, 1, 5, 1, 5, 5, 2, 1, 0, 0, 4]
Run Code Online (Sandbox Code Playgroud)

我需要这个列表,每0,它保持0和每个更高的数字为1.因此将上面的列表转换为:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)

我试着使用以下代码:

for numbers in list:
   if number==0:
      number=0
   if number>1:
      number=1
Run Code Online (Sandbox Code Playgroud)

但这给了我:

 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
Run Code Online (Sandbox Code Playgroud)

python

-5
推荐指数
3
解决办法
3890
查看次数