Hack Forums
[Python] Python shuffle list - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Python shuffle list (/showthread.php?tid=5208833)



Python shuffle list - 3yZtwglFSCUs - Mar 19, 2016

i need to randomly shuffle positioning of User-Agent,Cache-Control, Accept-Charset, Referer and keep alive but how? help python pro


def httpcall(url):
code = 0
try:
requests.get(
url,
params={buildblock(): buildblock()},
headers={
'User-Agent': choice(useragents),
'Cache-Control': choice(['no-cache', 'max-age=0']),
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
'Referer': get_referer() + buildblock(),
'Keep-Alive': randint(110, 120)}
)


* - movsx - Mar 19, 2016

You can't shuffle a dictionary in python


* - EwilDave - Mar 20, 2016

What do you mean shuffling the User-Agent, Cache-Control, Accept-Charset, etc...

Changing the order in a dictionary does not make a difference. I'll give you an example

PHP Code

<?php 
firstDict
= {"User": "Ewil", "Pass": "FooBar"}
secondDict = {"Pass": "Foobar", "User": "Ewil"}
print(
firstDict == secondDict) #True 

The order of the key-value pairs in a dictionary won't make a difference to the dictionary itself.


* - Goodies - Mar 20, 2016

Dictionaries are unrecorded containers in Python. This means that there is no way to change the ordering manually. There is a container in the collections module called OrderedDict, but that may not be a solution. You could, for example, do something like this

PHP Code

<?php 
from random import choice
, randint, shuffle
from collections import OrderedDict

data 
= {
    
'User-Agent': choice(["user-agent1", "user-agent2"]),
    
'Cache-Control': choice(['no-cache', 'max-age=0']),
    
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
    
'Referer': "foobar",
    
'Keep-Alive': randint(110, 120)
}
print(
data)
l = list(data.items())
shuffle(l)
od = OrderedDict(l)
print(
od) 

This prints
PHP Code

<?php 
{'Referer': 'foobar', 'Keep-Alive': 118, 'Cache-Control': 'max-age=0', 'User-Agent': 'user-agent2', 'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7'}
OrderedDict([('Cache-Control', 'max-age=0'), ('Accept-Charset', 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7'), ('User-Agent', 'user-agent2'), ('Referer', 'foobar'), ('Keep-Alive', 118)]) 

Which is good... However, requests may not iterate through the ordered dict correctly in a way that preserves this order. This is an active issue: https://github.com/kennethreitz/requests/issues/3038


* - EwilDave - Mar 20, 2016

(Mar 20, 2016 11:28 AM)Goodies Wrote:  
PHP Code

<?php 
from random import choice
, randint, shuffle
from collections import OrderedDict

data 
= {
    
'User-Agent': choice(["user-agent1", "user-agent2"]),
    
'Cache-Control': choice(['no-cache', 'max-age=0']),
    
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
    
'Referer': "foobar",
    
'Keep-Alive': randint(110, 120)
}
print(
data)
l = list(data.items())
shuffle(l)
od = OrderedDict(l)
print(
od) 

Thumbs up to Goodies for coming up with a solution rather than my feeble statement saying it was not possible.

Nothing is impossible, the word itself says 'I'm possible'!
- Audrey Hepburn


* - Goodies - Mar 20, 2016

(Mar 20, 2016 12:01 PM)EwilDave Wrote:  Thumbs up to Goodies for coming up with a solution rather than my feeble statement saying it was not possible.

Nothing is impossible, the word itself says 'I'm possible'!
- Audrey Hepburn

I'm honestly not sure if it is a solution :/ first if all, it's VERY slow to have to turn a dict to a list of tuples, then shuffling is pretty fast, but then turning it back into an OrderedDict O.o
For this situation, though, it's plenty fast. But secondly, I'm honestly not sure if it even works. I'll test it out when I get home and I'll report back.