Pass a given sequence of times in Python 3

Asked

Viewed 110 times

0

I have the following sequence:

{1,4,12,28,45,22}

I need a variable for example:

seguinte = input(num('Insira um número: '))

According to the number given, he would pass the last item to the beginning, being informed 2 would stay:

{45,22,1,4,12,28}
  • 1

    I could ask the question what you tried to do as well as the result you got?

1 answer

2


Set is not manageable.

In your question Voce uses the type set to define the sequence, in python this type is a data structure that represents a sequence denoting a mathematical set, i.e., Voce cannot impose or maintain any particular order of the elements. The abstract concept of a mathematical set does not presuppose order, for example, if you create a type set from a list, python automatically changes the order of the elements to meet the internal implementing directives of the set optimized to execute the proposed operations for this type.

See these examples:

set={'z','b','c'}
print(set)
{'c', 'b', 'z'}

set.add('a')
print(set)
{'c', 'b', 'z', 'a'}

set.add('e')
print(set)
{'a', 'b', 'c', 'e', 'z'}

# Obs. Novos testes com os mesmos valores podem gerar resultados diferentes

You could get what you want with list, see:

lst = [1,2,3,4,5]
lst.remove(5)
print(lst)
[1, 2, 3, 4]

lst.insert(0,5)
print(lst)
[5, 1, 2, 3, 4]

Tip:
To find the position of an element in a list, do: lst.index('elemento')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.