我需要帮助用python组织一个列表。
我需要的是:
我有一个这样的列表:[10,50,20,'STRING',5]
,我需要组织这个列表,不移动字符串,像这样:[5,'STRING'10,20,50]
。你能这样做吗?
谢谢!你知道吗
我需要创建一个链表来检查两个字符串中是否有交集,我在'def Intersecao'处检查,输出需要排序,如果交集为空,我需要打印字符串'VAZIO'。你知道吗
class No():
def __init__(self, valor = None, proximo = None):
self.valor = valor
self.proximo = proximo
def getValor(self):
return self.valor
def getProximo(self):
return self.proximo
def setProximo(self, novo_proximo):
self.proximo = novo_proximo
class lista():
def __init__(self, inicio = None):
self.inicio = inicio
def Inserir(self, valor):
novo_no = No(valor)
novo_no.setProximo(self.inicio)
self.inicio = novo_no
def Buscar(self, valor):
dados = self.inicio
while dados:
if dados.getValor() == valor:
return dados
else:
dados = dados.getProximo()
return None
def Intersecao (self, lista):
no = self.inicio
intersecao = []
while no != None:
if (not lista.Buscar(no.getValor())):
no = no.getProximo()
else:
if(no.getValor() == ''):
intersecao.append('VAZIO')
no = no.getProximo()
elif(no.getValor() in intersecao):
no = no.getProximo()
else:
intersecao.append(no.getValor())
no = no.getProximo()
return intersecao
def MostrarLista(self):
lista = []
dados = self.inicio
while dados:
lista.append(str(dados.getValor()))
dados = dados.getProximo()
print('->'.join(lista))
MyList = lista()
MySecondList = lista()
lista = []
lista2 = []
for i in range(40):
dado = input()
if i < 20:
lista.append(dado)
elif i >= 20:
lista2.append(dado)
for i in lista:
MyList.Inserir(i)
for i in lista2:
MySecondList.Inserir(i)
listaOrdenada = []
for elementos in sorted(MyList.Intersecao(MySecondList)):
print(elementos)
对于第一个问题,如果只是列表,一种方法是(可能有更好的方法,尽管):
输出:
或者,可以通过列表理解来完成,这样看起来更清晰:
输出相同。你知道吗
相关问题 更多 >
编程相关推荐