How to assign multiple strings to one key in dictionary ? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on script which is going to run loop over a input file and extract and/or change some words in the text file. Depending on parameter from console values of all variables from nested loops will be modified accordingly.
My question is: what would be the best way of storing values of variables for all valid parameters? I was considering using dictionary, however I am unsure if I will be able to store more than one string per one key? Essentially I would like to be able to retrieve set of strings and assign their values to the set of variables - based on key value. Some sort of 'nested' dictionary, equivalent of two dimensional array in Java.
Please help me find a suitable (the best and fastest) solution.
Thank you.

Hope this one could help
Use tuple or list for the multiple string
I recommend use tuple, because I assume that the sentences will never change.
my_dict = {
1:("Hello", "World"),
2:("Stackoverflow", "is", "Great")
}

Maybe you can try to store the file in json format.
In order to meet the requirement of retrieving and assignment in a fast way, (k, v) and (v, k) should be store in the json value,eg.something like inverted-table. Code example,
json format(DataStructure):
{k1:[v1,v2], k2:[v1], v1:[k1,k2], v2:[k1]}
Two operations can be made in O(1) time.

Related

Abbreviate words in a string to comply with max length [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a vector of strings (phrases with several words).
For reasons out of the scope of this question I need to comply with a length limit of N characters per string.
The very first thing I thought was to splice each string, but unfortunately the result of the operation will be facing end user (the end users will have to read the truncated strings and make sense out of them).
That means that I can't just slice the strings, because if I did so the following:
This is a simple test with FOO
This is a simple test with BAR
will be converted to
This is a simple te...
This is a simple te...
Meaning that data will be lost and the users won't be able to distinguish between the two strings.
After thinking a little bit more I figured out the best possible solution is to abbreviate as little characters of as little words as possible, always in accordance with the max length constraint.
With such a behaviour the previous example would be converted to
This is a sim. te. with FOO
This is a sim. te. with BAR
I figured out I'll ask here for an alternative/better solution, before coding this.
Also, if there isn't any better alternative, what things should I keep in mind while implementing this? Can you give me any tips?
I have a few thoughts... which may or may not meet your needs. To begin, here are some additional forms of abbreviation that you may be able to programatically implement.
Remove Vowels
If you remove vowels, you may be able to abbreviate words within the desired lengths, and be slightly more readable. Removing vowels is an acceptable form of abbreviation. Keep in mind, you will need to keep the first and last letter of the word even if they are vowels. organization = orgnztn
Use Abbreviation API
https://Abbreviations.com has an API with abbreviations. This might be useful for abbreviating longer words. For example, to find the abbreviation of "organization": https://www.abbreviations.com/abbreviation/organization abbreviates as ORG
It appears this user has attempted to do this in python. If you know you will have frequent phrases, you can create a dictionary of the abbreviated form.
Unfortunately, no matter where you truncate the data, there is a chance that two strings will end up looking the same to the end user. You could do some string comparison to determine where the differences are, then write some logic to truncate characters in other locations.

How can I use Python to locate two files with identical names and check if they're equal? [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 6 years ago.
Improve this question
Summary: How can I use Python to locate two files with identical names (in known locations) and see if they are identical using hash.
I currently have two folders, Folder1 and Folder2. I am trying to figure out how I can have Python move file by file through Folder1. For each file (which is a .jpg image), I would like to use hash to get a unique integer for the image and copy the file name to a string. From this string, I plan to find the potential identical copy of that image in Folder2 and then use hash to see if they are identical.
I am new to Python and this is my first post on Stack Overflow. If there is any information I should include or area where I was unclear, please let me know and I will respond as soon as possible. Thanks, and thank you to Ares for recommending the use of hash.
Comparing file size really isn't enough, is it? You could easily have two files, a.jpg in folder A and a.jpg in folder B. They are both exactly 16 kb, except that one is a picture of a dog and one is a picture of a cat.
What may be valuable is to hash every image in the list. In Python, you can take anything - numbers, strings, images, etc - and call hash() on it.
A hash is a numeric representation of a specific set of data. Barring some rare exceptions, hashes are exactly unique to that data. No other sets of data, except those that are exactly the same, will have that hash.
Some examples:
> hash('test')
will output
2314058222102390712
> hash('Test')
-1504849438355502056
This question describes how to load an image in Python. Then, just call hash on each image. This describes what a hash is if I was unclear.

When is it correct to use Tuple, List, or Set [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 7 years ago.
Improve this question
In Python, there are 3 Array like datatypes (unless I'm forgetting one).
Those being Tuples ((1,2,6,3)), Lists ([1,2,6,3]), and Sets ({1,2,6,3}).
They each have different methods defined to them, but when is it correct to use one over the other? There are questions and answers already about these but none of them seem to say when to use one over the other.
They can each be converted to one another so it's possible to just use one over the other and convert when necessary. Though that probably isn't a preferred method.
So, I ask, under what situation would you choose one type over the other?
To summarize the documentation, a list is a mutable sequence that preserves its order and can have non-unique elements; a set is a mutable collection that does not preserve its order and cannot have non-unique elements, and a tuple is an immutable sequence that preserves its order and can have non-unique elements. Bonus: a dict is a mutable collection that does not preserve its order and cannot have non-unique keys. See the linked documentation for more details.

Is there a linked list predefined library in Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I know in c++ it already exist
#include <list>
Now I am curious to know if it exist in python also.
You can also take a look at llist python package, which provides some useful features that deque does not. There are not only doubly linked lists, but also single linked lists data structure in that package. IMHO, one of the biggest advantages of this package is the ability to store a reference to the llist elements.
It appears that collections.deque is a doubly-linked-list library in Python. According to the documentation, it should have approximately O(1) cost when appending or popping from the head or the tail, as well as O(n) for regular inserts (which matches what we'd expect from a linked list).
API: http://docs.python.org/2/library/collections.html#collections.deque
Source: https://stackoverflow.com/a/282238/2441252

In Python, how do you make a loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have started learning Python and was wondering how to make a loop, I cant find a website that has it so if you knew it would be great!
I assume you mean a for loop. You do it like so:
for i in range(10):
print i
This is the simplest way. You can use xrange rather than range for very long loops to help with memory management.
Also there are ways to iterate through iterable objects (like lists, arrays, strings etc.):
a=[1,2,3]
for something in a:
print something
You could also use comprehension to make the code shorter, but that's a bit more advanced and you probably don't need it just yet. Should you so wish to learn what it's about this looks OK for a start.
There are many ways.
while True:
That loops until you put break on a line.
The two Aleksander Lidtke mentioned.
for x in range(10): #Loops through the following code 10 times
And:
x = [1,2,3,4]
for items in x: #the variable items is assigned to
#each variable as it iterates through
To learn more about python, read this book.

Categories

Resources