相关疑难解决方法(0)

将元组扩展为参数

有没有办法将Python元组扩展为函数 - 作为实际参数?

例如,这里expand()有魔力:

some_tuple = (1, "foo", "bar")

def myfun(number, str1, str2):
    return (number * 2, str1 + str2, str2 + str1)

myfun(expand(some_tuple)) # (2, "foobar", "barfoo")
Run Code Online (Sandbox Code Playgroud)

我知道可以定义myfunmyfun((a, b, c)),但当然可能有遗留代码.谢谢

python tuples parameter-passing

375
推荐指数
5
解决办法
20万
查看次数

用Python解压缩列表?

我认为'解包'可能是错误的词汇 - 道歉,因为我确信这是一个重复的问题.

我的问题很简单:在一个需要项目列表的函数中,如何在不出错的情况下传递Python列表项?

my_list = ['red', 'blue', 'orange']
function_that_needs_strings('red', 'blue', 'orange') # works!
function_that_needs_strings(my_list) # breaks!
Run Code Online (Sandbox Code Playgroud)

当然必须有一种方法来扩展列表,并'red','blue','orange'在蹄上传递函数?

python list argument-passing

189
推荐指数
3
解决办法
17万
查看次数

在Python中创建一个列表,在一行中包含给定对象的多个副本

假设我有一个给定的对象(一个字符串"a",一个数字 - 让我们说0,或者一个列表['x','y'])

我想创建包含此对象的许多副本的列表,但不使用for循环:

L = ["a", "a", ... , "a", "a"]

要么

L = [0, 0, ... , 0, 0]

要么

L = [['x','y'],['x','y'], ... ,['x','y'],['x','y']]

我对第三种情况特别感兴趣.谢谢!

python list

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

Python 3 - 在str.format()中使用元组

我正在尝试使用该str.format()方法,并且当我的值存储在元组中时遇到一些困难.例如,如果我这样做:

s = "x{}y{}z{}"
s.format(1,2,3)
Run Code Online (Sandbox Code Playgroud)

然后我明白了'x1y2z3'- 没问题.
但是,当我尝试:

s = "x{}y{}z{}"
tup = (1,2,3)
s.format(tup)
Run Code Online (Sandbox Code Playgroud)

我明白了

IndexError: tuple index out of range.
Run Code Online (Sandbox Code Playgroud)

那么如何将元组转换为单独的变量呢?或任何其他解决方法的想法?

python string tuples string-formatting

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

IndexError:位置参数元组的替换索引 1 超出范围

我正在学习教程,但不知道为什么会出现此错误:

    <ipython-input-61-d59f7a5a07ab> in extract_featuresets(ticker)
      2     tickers, df = process_data_for_labels(ticker)
      3     df['{}_target'.format(ticker)] = list(map(buy_sell_hold,
----> 4                                              df['{}_{}1d'.format(ticker)],
      5                                              df['{}_{}2d'.format(ticker)],
      6                                              df['{}_{}3d'.format(ticker)],

IndexError: Replacement index 1 out of range for positional args tuple
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

tickers, df = process_data_for_labels(ticker)
df['{}_target'.format(ticker)] = list(map(buy_sell_hold,
                                         df['{}_{}1d'.format(ticker)],
                                         df['{}_{}2d'.format(ticker)],
                                         df['{}_{}3d'.format(ticker)],
                                         df['{}_{}4d'.format(ticker)],
                                         df['{}_{}5d'.format(ticker)],
                                         df['{}_{}6d'.format(ticker)],
                                         df['{}_{}7d'.format(ticker)],))
Run Code Online (Sandbox Code Playgroud)

这是教程的链接:https : //www.youtube.com/watch?v=zPp80YM2v7k

python index-error

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

在 python 中使用带有 .format 的元组

使用.format()with 文本填充时,我遇到了这个错误。

我拥有的:

tuple = ('a', 'b', 'c')
text = "Hi {} hello {} ola {}"
#command I tried to run
text.format(tuple)
Run Code Online (Sandbox Code Playgroud)

我的目标输出:

 Hi a hello b ola c
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

IndexError:元组索引超出范围

不知道如何解决这个问题!

python format tuples

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

创建类似对象的简短方法是什么?

from tkinter import *

height = 600
width = 600
root = Tk()
canvas = Canvas(root, width = width, height = height, bg = 'red3')
canvas.pack()

# Code for Fries
canvas.create_polygon(150, 100, 160, 250, 170, 250, 160, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(160, 100, 170, 250, 180, 250, 170, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(170, 100, 180, 250, 190, 250, 180, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(180, 100, 190, 250, 200, 250, 190, 80, …
Run Code Online (Sandbox Code Playgroud)

python tkinter

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

从函数返回位置参数

类(人)需要3个名为"name,middle_name和surname"的参数,我试图给出这些参数如下:

    def argument():
        name = input("name : ")
        middle_name = input("middle name : ")
        last_name = input("last name : ")
        return name,middle_name,last_name
   person = people(argument())
Run Code Online (Sandbox Code Playgroud)

但不知何故它不起作用,我得到的错误信息:

TypeError: __init__() missing 2 required positional arguments
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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