简体   繁体   English

Function 接受一个 Int 和一个 List

[英]Function that takes an Int and a List

I have the below code for a card game, it should remove the N number of cars from the given card list and return a tuple with 2 lists, the first list is the N objects from the original list and the secons list is the remaining cards not extracted from the original list.我有下面的纸牌游戏代码,它应该从给定的卡片列表中删除 N 辆汽车并返回一个包含 2 个列表的元组,第一个列表是原始列表中的 N 个对象,第二个列表是剩余的卡片未从原始列表中提取。

lista_cartas = [1,2,3,4,5,6,7,8]
lista_N = []
N = 5
for i in range(N):
    extracto = lista_cartas.pop(0)
    lista_N.append(extracto)
    lista_final = [lista_N, lista_cartas]
print(tuple(lista_final))

([1, 2, 3, 4, 5], [6, 7, 8])

it's working as I want but I need to trasform this into a function that takes the N number and the list as parameters, how can I achieve this?它可以按我的意愿工作,但是我需要将其转换为 function ,它将 N 数和列表作为参数,我该如何实现?

is this somethinhg valid?这东西有效吗? or how can I make the function to take a list?或者我怎样才能让 function 拿一个清单?

def sacar_cartas(N, lista=[]):
   for i in range(N):
       extracto = lista_cartas.pop(0)
       lista_N.append(extracto)
       lista_final = [lista_N, lista_cartas]
print(tuple(lista_final))

You can rework your solution entirely by using list slices :您可以使用list slices完全修改您的解决方案:

lista_cartas = [1,2,3,4,5,6,7,8]

def sacar_cartas(todas_cartas, N):
    return todas_cartas[:N], todas_cartas[N:]

such that sacar_cartas(lista_cartas, 5) results in the tuple :这样sacar_cartas(lista_cartas, 5)导致tuple

([1, 2, 3, 4, 5], [6, 7, 8])

Notice how we can avoid the explicit tuple call by instead returning comma-separated values.请注意我们如何通过返回逗号分隔值来避免显式tuple调用。

simple conversion:简单转换:

def sacar_cartas(N, lista):
    lista_N = []
    for i in range(N):
        extracto = lista.pop(0)
        lista_N.append(extracto)
    return tuple([lista_N, lista])
print(sacar_cartas(5, [1,2,3,4,5,6,7,8]))

Slightly reworked your code so that the function won't alter the passed in list (because of the lista_cartas.copy() call. As to your code and question, Python functions can accept a list as a variable without telling it that it is a list.稍微修改了您的代码,以便 function 不会更改传入的列表(因为 lista_cartas.copy() 调用。至于您的代码和问题,Python 函数可以接受列表作为变量而不告诉它它是列表。

lista_cartas = [1,2,3,4,5,6,7,8]

def sacar_cartas(N, lista_cartas):
    lista_N = []
    lista_ct = lista_cartas.copy()
    for i in range(N):
        extracto = lista_ct.pop(0)
        lista_N.append(extracto)
        lista_final = [lista_N, lista_ct]
    return lista_final    

sacar_cartas(5, lista_cartas)

Python is duck typed. Python 是鸭式。 You can pass a list in just like any other variable, without having to define the type:您可以像任何其他变量一样传递列表,而无需定义类型:

def function(some_variable):
    for element in some_variable:
        print(element)

function(['1st element', '2nd element', '3rd element'])
# prints:
# 1st element
# 2nd element
# 3rd element

Doing this:这样做:

def function(some_variable=[]):

Does NOT indicate that this variable is a list and is not needed.表示此变量是一个列表并且不需要。 It instead tells the function that if you do not pass this variable in, it will default to the value []相反,它告诉 function 如果你不传入这个变量,它将默认为值[]

You don't necessarily have to give a list a default value of empty.您不必为列表指定默认值为空。 You can pass a list to a function by mentioning its variable name:您可以通过提及其变量名称将列表传递给 function:

lista_cartas = [1,2,3,4,5,6,7,8]
lista_N = []

def sacar_cartas(N, lista_cartas, lista_N):
   for i in range(N):
       extracto = lista_cartas.pop(0)
       lista_N.append(extracto)
       lista_final = [lista_N, lista_cartas]   
   print(tuple(lista_final))

# Call the function for it to work
sacar_cartas(N = 5, lista_cartas, lista_N)

You can define variable within the function call if you want.如果需要,您可以在 function 调用中定义变量。 Thats optional because you can define it like a list before the call.这是可选的,因为您可以在调用之前将其定义为列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM