Pass a given sequence number of times in Python 3

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 entered, it would pass the last item to the beginning, being informed 2 would be:

{45,22,1,4,12,28}
    
asked by anonymous 13.12.2018 / 13:20

1 answer

2

Set is not orderly.

In your question you use type set to define the sequence, in python this type is a data structure that represents a sequence denoting a mathematical set, ie you can not impose or maintain any particular order of elements. The abstract concept of a mathematical set does not assume order, for example, if you create a type set from a list, python automatically changes the order of the elements to meet the internal% implementation-optimized directives 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 set , 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: list

    
13.12.2018 / 14:22