How to represent "range" in python dict [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to create dictionary like this:
1,2 = "A"
3,4 = "B"
5,6,7・・・(>=5) = "C"
so I tried below, but didnt works well..
dict = {1:"A", 2:"A", 3:"B", 4:"B", >=5:"C"}
how can I create a dictionary like this?

A lookup of a value where the key is in a sorted range is capped at O(log n). Hence using a dictionary directly is not viable. You can either do this by:
storing the ranges in list, sorting them, and doing a binary search, or...
using a tree data structure.
There are other creative solutions such as storing all the numbers in the range in the dictionary as integer keys and have duplicate references to the same value (which looks like what you have above). This would be O(1) lookup, but would have the negative side effects such as:
cannot modify the ranges easily without updating all the keys
wasted space for large ranges
cannot do lookups with floats
...which might be okay depending on your use case.
In summary, there is no direct solution.

Related

When to use Lists, Sets, Dictionaries, or tuples in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm new to python, and I'm a bit confused about the use cases of data types in python
Can someone please explain in detail when to use each data type, with an example if possible
Thank you.
Lists are used when you have data you want to further modify, alter like sorting and all.
Dictionary is used when you have to sets of data where data of the first set corresponds to data of other set. And the position of the data doesn't matter only the relation of the two sets matters.
A tuple is used when position of the data is very important and you don't want to alter the position throughout.

Free function that gives a lexicographically bigger string each time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am implementing a toy db, and I need a free function that gives me a lexicophgraically bigger string each time it's called. It's for naming segment files.
Let's assume I have a max of 1000 files, and I'd prefer if the string was less than 10 characters long.
Could someone give me the easiest example of such a function in python? I'd really like to be a free function as I don't want to introduce complexity with state.
A function that returns a different value each time you call it will have to keep some sort of state. However, defining a generator makes that relatively simple to manage. Specifically, itertools.count will produce an infinite stream of increasing integers; you just need to produce a suitable string from each integer.
from itertools import count
next_label = map("{:010}".format, count()).__next__
Then
>>> next_label()
'0000000000'
>>> next_label()
'0000000001'
>>> next_label()
'0000000002'
and so on, for as many times as you need to call next_label.

Python: what is the best way to store numerical key value pairs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data which consist of numerical keys and values.
I need to increase all keys and values by N number.
While i am using dictionaries for big amout of data my code works very slow.
What is the best way to store this data and the best way to increase values of pairs?
Example:
N=2
{1:4,3:6,2:1}
expected result:
{3:6,5:8,4:2}
Thanks
We can not actually do something faster if you want to change the whole data of dictionary. Even If someone run a for loop we are not sure of O(N) complexity because there can be re-hashing operations internally.
Best thing is you can smartly use one extra variable in memory for updates.
Like initially
del=0 and d={1:4,3:6,2:1}
when you want to increase values and keys by N
update del+=N
While retrieving from dictionary for key value k
Use d[k-del]+del
Best you can do about this is O(N) whichever data structure you use, you will have to visit the values of each element and increment them.

Which data structure preserves order in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking for a data structure which allows fast search and also preserves the order of the data it received?
Lists and tuples preserve order, dictionaries and set don't. An available ordered version of a dictionary is collections.OrderedDict, which provides a faster search than a list.
Take a look at this
You have dictionaries and set:(unordered)
Dictionary is an unordered set of key: value pairs
set is an unordered collection with no duplicate elements
You can either use for preserving the order:
list
tuple
collections.OrderedDict
Choosing the data structure totally depends on your requirement.
List and tuple hold a basic difference. List is mutable and tuples is immutable
ordered dictionary is nothing but a key value pair.
So you can choose accordingly!

Which is more efficient and faster way to access an element? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Suppose i have a list of hundred natural numbers, set of hundred natural numbers and dictionary of hundred natural numbers (assuming both key and value are natural numbers). I want to access an element in these data types. Which will be the more efficient and faster way to access it? I know i can use some performance tools like timeit or cprofile etc to check the performance but how will i know which data type to choose and when?
At a high overview:
list lookups are O(n)
Where n is the length of the list.
dict lookups are O(1)
This is basic Big O notation or Complexity.

Categories