TCL has a data structure called dict which maintains a collection of key-value pairs.
Is there another data structure which maintains a collection of keys (with no values)?
If no, then maybe someone already wrote a simple adapter on dict with empty values?
You could use the tcllib package ::struct::set.
http://tcllib.sourceforge.net/doc/struct_set.html
Just use a single list.
set example [list "key1" "key2" "key3"]
if {[lsearch -exact $example "key3"] != -1} {
puts "found your key!"
} else {
puts "your key is not in the list"
}
Maybe you should ask a more specific question to get a more accurate answer.
An alternative for dict is array which doesn't preserve the order of keys.
Another approach would be to accumulate everything into say, $bucket.
Then do:
set uniqueItems [lsort -unique $bucket]
Related
Number,IceCream
1,Chocolate
2,Vanilla
3,Mixed
Say if I
Number = input("Flavor?:")
I know that I need to index [0] because the numbers are on the first column. I also know that I will need to use .split(",") to remove the commas and to create a list.
Some assistance would be greatly appreciated!
It's confusing whether you plan to include integers in the list with the strings or not
Method 1: including integers with strings(flavor), create a list of tuples
icecream=[(1,'choc'),(2,'mix'),(3,'blueberry')]
print(icecream[0][1])
print(icecream[2][1])
Note: tuples are immutable
Method 2: I believe the best way to do this would be to use a dictionary instead of list. As dictionary has (Key, value) pairs, you could assign key(integer) to values(flavor), which then would make it easy accessing items just by keys(integers in your case) ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"} #dictionary
to access values, you could use methods available in dictionary use get(), items()
Note: items() returns a tuple for each key,value pair.
ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"}
new=Ice_cream_flavors.items()
for k,v in new:
if input==k:
print(v)
I have some data I need to process. It looks like a dictionary within a dictionary within a dictionary, all of which are being stored in a list! This is parsed JSON data so I have no control over the format of it.
Here is some of the data, I've deleted a lot of it as it's irrelevant and for brevity:
devices = [
{
'server.device.base.phyname': 'IEEE802.11',
'dot11.device': {
'dot11.device.associated_client_map': {
'68:96:1E:96:96:B5': '4202770DF206F63E_B5F4CE1EAB680000',
'60:30:CE:91:4A:96': '4202770DF206F63E_8D4A91D430600000',
'4C:32:75:66:96:10': '4202770DF206F63E_105F6675324C0000',
'50:6A:03:3E:0E:17': '4202770DF206F63E_170E3E036A500000',
'7C:C3:CE:A4:EC:86': '4202770DF206F63E_86ECA4A1C37C0000',
'2C:BE:08:F0:D5:A0': '4202770DF206F63E_A0D5F008BE2C0000',
'96:E7:96:76:9A:C7': '4202770DF206F63E_C79A762CE7700000',
'96:CE:75:57:E2:5A': '4202770DF206F63E_5AE2577510000000',
'34:68:95:96:3C:96': '4202770DF206F63E_C43C6A9568340000',
'6C:96:96:9D:CE:57': '4202770DF206F63E_57109DCF966C0000',
'CE:61:96:CE:B4:69': '4202770DF206F63E_69B4D2AE61900000',
'04:CE:CE:1C:CE:8C': '4202770DF206F63E_8CAF1CCE0C040000',
'2C:F0:CE:DC:D6:39': '4202770DF206F63E_39D6DCEEF02C0000'
}
}
}
]
I need to be able to extract the MAC addresses that are stored within the 'dot11.device' pair. I'm so far able to loop through the parent list and display all of the data:
for d in devices:
print d['dot11.device']['dot11.device.associated_client_map']
however this prints the whole nested dict.
What I'd really like to do is return a new list of just the MAC addresses (are they dictionary keys? I'm not sure).
I'm working with Python2 and any help is much appreciated!
Yes, they are indeed keys, and so the answer is quite simple:
for d in devices:
print d['dot11.device']['dot11.device.associated_client_map'].keys()
I have a list of tuples that I must change the values for in a map that contains those tuples. So if I have a list such as List((0,2), (0,3)) with a map that looks like this: Map((0,2) => List(1,2,3), (0,3) => List(1,2)), I need to access the matching map tuples with the tuples listed in the list, then remove a number from the mapping.
So in the example above, if I wanted to remove 2 from the mapping, I would get Map((0,2) => List(1,3), (0,3) => List(1)).
Design wise, I was thinking of pattern matching the map, but I've read some answers that said that may not be the best way. The tough part for me is that it has to be immutable, so I was thinking of pattern matching the list, getting the map value, change the value, then recreate the map and recursively call the function again. What do you think of this implementation?
This could be a way to remove 2 from your Map:
val newMap = oldMap.mapValues(list => list.filter(_ != 2))
Or more generally:
def filterInMap(element: Int, oldMap: Map[(Int,Int),List[Int]]) =
oldMap.mapValues(list => list.filter(_ != element))
This way there's no need to mutate anything at all. mapValues transforms just the values of your Map and returns a copy of the original without mutating it at all. filter then gets the job done by only allowing elements that don't match the element we would like to remove.
Bonus: even more generally:
def filterInMap[A](element: A, oldMap: Map[(A,A),List[A]]) =
oldMap.mapValues(list => list.filter(_ != element))
I am trying to crate a new array/list from an existing list of items. I am wanting to check if the item exist first, if it does not, create it then add a value to it. If it already exist just append a value. I also need a way to get the length of the total.
set Area {23401 23402 23403}
foreach Item $Area {
set ElExist [info exist ${Item}lst]
if {$ElExist == 0} {
set ${Item}lst {};
lappend ${Item}lst $TotalRecords
} else {
lappend ${Item}lst $TotalRecords
}
set CurrentOptinon [llength ${Item}lst]
}
If I was writing that code, I'd do it like this:
set Area {23401 23402 23403}
foreach Item $Area {
upvar 0 ${Item}lst lst
lappend lst $TotalRecords
set CurrentOptinon [llength $lst]
}
This will behave the same as your code, but it's so much shorter. Here's the tricks in use:
lappend creates a variable if it didn't already exist.
upvar 0 makes a local alias to a variable. So much simpler.
The alias removes the need for magic with llength, but otherwise you could have done:
set CurrentOptinon [llength [set ${Item}lst]]
The $ syntax is in many ways just a short-cut for a call to the single-argument version of set, which reads the named variable. Except if you write set then you can use substitutions in your variable name. As a rule of thumb, if you're extensively using variable names in variables without aliasing, you're probably doing something wrong (unless you really do need the name).
You're using weird variable names. Much better would be an array.
set Area {23401 23402 23403}
foreach Item $Area {
lappend lst($Item) $TotalRecords
set CurrentOptinon [llength $lst($Item)]
}
However, this is likely to require you to change code elsewhere.
I have the following 2 dictionaries,
d1={"aa":[1,2,3],"bb":[4,5,6],"cc":[7,8,9]}
d2={"aa":[1,2,3],"bb":[1,1,1,1,1,1],"cc":[7,8]}
How could I compare these two dictionaries and get the
positions(indexes) of UNMATCHED key value pairs? since I am dealing
with files of size around 2 GB, the dictionaries contain very large
data. How can this be implemented in optimized way?
def getUniqueEntry(dictionary1, dictionary2, listOfKeys):
assert sorted(dictionary1.keys()) == sorted(dictionary2.keys()), "Keys don't match" #check that they have the same keys
for key in dictionary1:
if dictionary1[key] != dictionary2[key]:
listOfKeys.append(key)
When calling the function, the third param listOfKeys is an empty list where you want the keys to be stored. Note that reading 2 gb worth of data into a dict requires alot of ram and will most likely fail.
and this is a more pythonic way: The list expansion will consider just the values that are not equal in both dictionaries:
diffrent_keys = [key for key in d1 if d1[key] != d2[key] ]